【发布时间】:2017-05-16 08:54:50
【问题描述】:
我是学习机器学习的新手,所以在 YouTube 上学习了一个很棒的教程。但是下面的代码给了我一个错误。我在这里读到了一个类似的问题,但timetuple() 并没有解决我的问题,视频中也没有任何解决方案。
这是我的代码:
import pandas as pd
import quandl, math
from datetime import datetime, date, time, timedelta
import time
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt #plot stuff, how to plot in graph
from matplotlib import style #nice looking thing
style.use('ggplot') #which nice-looking-thing i wanna use
quandl.ApiConfig.api_key = '...' #erased my key for secrecy
df = quandl.get_table('WIKI/PRICES')
## ... ##other irrelevant code snippets
forecast_out = int(math.ceil(0.01*len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)
X = np.array(df.drop(['label'],1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]
df.dropna(inplace=True)
y = np.array(df['label'])
y = np.array(df['label'])
# ... #other irrelevant code snippets
forecast_set = clf.predict(X_lately)
df['Forecast'] = np.nan
last_date = df.iloc[-1].name
last_unix = last_date.timestamp() ###MAIN attribute error found here
one_day = 86400
next_unix = last_unix + one_day
对于上面的代码,我收到以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-8-4a1a193ea81d> in <module>()
1 last_date = df.iloc[-1].name
----> 2 last_unix = last_date.timestamp()
3 one_day = 86400
4 next_unix = last_unix + one_day
AttributeError: 'numpy.int64' object has no attribute 'timestamp'
尽管互联网上有很多解决方案,但我无法找出解决方案,但对我没有任何帮助。我在 anaconda 中使用 Python 3.5。 timetuple() 对我不起作用,并且发生相同的属性错误。
【问题讨论】:
-
你想用
last_date = df.iloc[-1].name做什么?这可以让您获得数据框中最后一行的 index,大概是一些np.int64,这对于索引是有意义的。为什么你期望它有一个timestamp属性? -
@SekarRaj:感谢您想要编辑。我拒绝了您的编辑,因为
inline code formatting用于代码和 IO,而不是任何东西的荧光笔。 “code”、“error”和“Python”之类的词本身不是code或IO,所以请不要格式化。 -
医学博士。 E. S. C. - 请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
@halfer:感谢您的编辑。下次我会记住这些事情
-
@juanpa.arrvillag,教程显示整数具有 timestamp() 属性,但没有明确给出实际原因。根据代码,next_unix实际上是某种日期格式的第二天。现在如何实现呢?