【发布时间】:2016-12-14 05:23:16
【问题描述】:
我尝试使用 scikit-learn SVM 来预测 S&P500 的股票是否跑赢指数。 我有一个“样本”文件,从中提取特征 X 和标签(超过索引或不超过索引)Y。
当我第一次尝试时(没有重塑 X)我得到了以下折旧错误:
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17
and will raise ValueError in 0.19. Reshape your data either using
X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1)
if it contains a single sample.
因此,我尝试根据建议以及一些论坛帖子对 X 进行重塑。 但是现在我得到以下值错误,即 X 和 Y 的形状不同。
ValueError: X and y have incompatible shapes.
X has 4337 samples, but y has 393.
你可以在下面看到重塑前 X 和 Y 的形状:
('Shape of X = ', (493, 9))
('Shape of Y = ', (493,))
在重塑之后:
('Shape of X = ', (4437, 1))
('Shape of Y = ', (493,))
我也尝试重新整形以获得 (493,9) 形状,但这也不起作用,因为我收到以下错误。
ValueError: total size of new array must be unchanged.
我在下面发布了从 pandas DataFrame 和 SVM 分析中提取特征和标签的代码:
特征和标签选择:
X = np.array(sample[features].values)
X = preprocessing.scale(X)
X = np.array(X)
X = X.reshape(-1,1)
Y = sample['status'].values.tolist()
Y = np.array(Y)
Z = np.array(sample[['changemktvalue', 'benchmark']])
SVM 测试:
test_size = 50
invest_amount = 1000
total_invests = 0
if_market = 0
if_strat = 0
clf = svm.SVC(kernel="linear", C= 1.0)
clf.fit(X[:-test_size],Y[:-test_size])
correct_count = 0
for x in range(1, test_size+1):
if clf.predict(X[-x])[0] == Y[-x]:
correct_count += 1
if clf.predict(X[-x])[0] == 1:
invest_return = invest_amount + (invest_amount * (Z[-x][0]/100)) #zeroth element of z
market_return = invest_amount + (invest_amount * (Z[-x][1]/100)) #marketsp500 is at pos 1
total_invests += 1
if_market += market_return
if_strat += invest_return
print("Accuracy:", (float(correct_count)/test_size) * 100.00)
如果您对如何解决这个问题有任何意见,那就太好了。
【问题讨论】:
-
我认为您应该在
Y上使用reshape((-1,1))并阅读有关广播规则的信息... -
这正是问题所在,这种重塑不起作用,因为它导致了上面的值错误
-
你不应该重塑。仅当您具有从警告中明显看出的单个功能时才进行整形。但是
X中有 9 个功能。删除代码中的reshape并发布整个堆栈跟踪。 -
如果我不进行整形,我会收到 DepreciationWarning。堆栈跟踪到底是什么意思?
-
对不起,这是警告而不是错误,所以没有堆栈跟踪。
标签: python error-handling scikit-learn svm