【发布时间】:2021-07-30 14:07:18
【问题描述】:
我正在尝试预测股票趋势,其中 1 是股票上涨,0 是股票下跌。我的输入特征是收盘价、成交量、当天趋势,我的输出是第二天的趋势。应用 XGBClassifier() 时遇到错误:
ValueError Traceback (most recent call last)
<ipython-input-101-d14cdb520e55> in <module>
1 val = np.array(test[0, 0]).reshape(1, -1)
2
----> 3 pred = model.predict(val)
4 print(pred[0])
~/opt/anaconda3/lib/python3.8/site-packages/xgboost/sklearn.py in predict(self, data, output_margin, ntree_limit, validate_features, base_margin)
968 if ntree_limit is None:
969 ntree_limit = getattr(self, "best_ntree_limit", 0)
--> 970 class_probs = self.get_booster().predict(
971 test_dmatrix,
972 output_margin=output_margin,
~/opt/anaconda3/lib/python3.8/site-packages/xgboost/core.py in predict(self, data, output_margin, ntree_limit, pred_leaf, pred_contribs, approx_contribs, pred_interactions, validate_features, training)
1483
1484 if validate_features:
-> 1485 self._validate_features(data)
1486
1487 length = c_bst_ulong()
~/opt/anaconda3/lib/python3.8/site-packages/xgboost/core.py in _validate_features(self, data)
2058 ', '.join(str(s) for s in my_missing))
2059
-> 2060 raise ValueError(msg.format(self.feature_names,
2061 data.feature_names))
2062
ValueError: feature_names mismatch: ['f0', 'f1', 'f2', 'f3'] ['f0']
expected f2, f1, f3 in input data
我的代码如下:
def xgb_predict(train, val):
train = np.array(train)
x, y = train[:, :-1], train[:, -1]
model = XGBClassifier()
model.fit(x, y)
val = np.array(val).reshape(1, -1)
pred = model.predict(val)
return pred[0]
xgb_predict(train, test[0, 0])
我在第 8 行收到错误消息。非常感谢您的帮助:)
【问题讨论】:
-
欢迎来到 SO!就您发布的内容而言,我会说您需要从测试数据中选择更多列。为了确保是这种情况,您能否编辑您的问题以添加您的数据样本?我的猜测是将最后一行更改为
xgb_predict(train, test[0, :]) -
您好,感谢您的欢迎!正如你所说,我已经改变了我的最后一行,但现在错误显示“训练数据没有以下字段:f3”。感谢您的帮助
-
有道理,因为最后一列是目标列。我已经发布了答案。请检查一下。
标签: python time-series xgboost stock