gpt4 book ai didi

python - 如何修复 "float() argument must be a string or a number, not ' 时间戳“”错误?

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:00 26 4
gpt4 key购买 nike

我正在编写一些代码来使用机器学习和深度学习技术来预测股票市场。但是,当我尝试使用线性回归时,我收到了这个似乎无法修复的错误。我的数据是某家公司的股票。这些功能包括日期、开盘价、最低价、最高价、收盘价、最后交易量、成交量和总交易量。

我已经在 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() argument must be a string or a number, not 'Timestamp'

最佳答案

数据帧 df 的索引设置为等于日期。尝试在完成 sort_index() 步骤后将其删除。

在 sort_index 步骤之后,执行以下操作:

df = df.reset_index(drop=True)

然后将 df 拆分为训练测试并继续进行回归步骤。也许这会有所帮助。

关于python - 如何修复 "float() argument must be a string or a number, not ' 时间戳“”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com