【发布时间】:2019-08-10 09:08:42
【问题描述】:
我正在编写一些代码来使用机器学习和深度学习技术预测股市。但是,当我尝试使用线性回归时,我遇到了这个似乎无法修复的错误。 我的数据是某家公司的股票。这些功能包括日期、开盘价、最低价、最高价、收盘价、最后价、成交量和总交易量。
我已经在 stackoverflow 上查看了这个问题的类似答案,但他们使用的技术不起作用。
#setting index as date
df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
df.index = df['Date']
#sorting
data = df.sort_index(ascending=True, axis=0)
#creating a separate dataset
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date',
'Close'])
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
#split into train and test data
train = df[:130]
test = df[130:]
x_train = train.drop('Close', axis=1)
y_train = train['Close']
x_test = test.drop('Close', axis=1)
y_test = test['Close']
#implement linear regression
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train,y_train)
最后一行抛出这个错误:
TypeError: float() 参数必须是字符串或数字,而不是 '时间戳'
【问题讨论】:
-
尝试将数据类型 'timestamp' 更改为 'string' 只是为了实验目的。还发布
df.info()的输出,以便澄清事情。
标签: python scikit-learn