简体   繁体   English

AttributeError-即使似乎没有属性错误

[英]AttributeError - Even though there seem to be no attribute error

I am currently learning how to python for Machine Learning. 我目前正在学习如何使用Python进行机器学习。 While I am progressing, the interpreter had detected a AttributeError but I do not see any problem. 在进行过程中,解释器检测到AttributeError,但是我看不到任何问题。 Can someone help to fix this error? 有人可以帮助您解决此错误吗?

My Code: 我的代码:

import pandas as pd 
import quandl, math
import numpy as np 
import datetime
import matplotlib.pyplot as plt 
from matplotlib import style 
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression

style.use('ggplot')

quandl.ApiConfig.api_key = ''

df = quandl.get('EOD/V', api_key = '')
df = df[['Adj_Open','Adj_High','Adj_Low','Adj_Close','Adj_Volume',]]
df['ML_PCT'] = (df['Adj_High'] - df['Adj_Close']) / df['Adj_Close'] * 100.0
df['PCT_change'] = (df['Adj_Close'] - df['Adj_Open']) / df['Adj_Open'] * 100.0

df = df[['Adj_Close', 'ML_PCT', 'PCT_change', 'Adj_Volume']]

forecast_col = 'Adj_Close'

df.fillna(value=-99999, inplace=True)
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 = X[:-forecast_out]

df.dropna(inplace=True)
y = np.array(df['label'])
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)

clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)
confidence = clf.score(X_test, y_test)
print(confidence)

X_lately = X[-forecast_out:]
forecast_set = clf.predict(X_lately)
print(forecast_set, confidence, forecast_out)

df['Forecast'] = np.nan 

last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400
next_unix = last_unix + one_day

for i in forecast_set:
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += 86400
    df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)]+[i]

df['Adj_Close'].plot()
df['Forecast'].plot()
plt.legend(loc = 4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()

Error: 错误:

C:\Python27\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
0.989124557421
(array([ 94.46383723,  93.27713267,  93.15533011,  93.89038799,
        94.71390166,  95.29332756,  96.23047821,  96.51527839,
        96.17180986,  96.17575181,  96.68721678,  96.85114045,
        97.57455941,  97.98680762,  97.32961443,  97.55881174,
        97.54090546,  96.17175855,  94.95430597,  96.49002102,
        96.82364097,  95.63098589,  95.61236103,  96.24114818])Traceback (most recent call last):, 0.98912455742140903, 24)

  File "C:\Users\qasim\Documents\python_machine_learning\regression.py", line 47, in <module>
    last_unix = last_date.timestamp()
AttributeError: 'Timestamp' object has no attribute 'timestamp'
[Finished in 36.6s]

The issue is that last_date is a pandas Timestamp object, not a python datetime object. 问题是last_date是pandas Timestamp对象,而不是python datetime对象。 It does have a function like datetime.timetuple() , though. 它确实具有类似datetime.timetuple()的功能。 Try this: 尝试这个:

Assuming last_date is in UTC, use this: 假设last_date是UTC,请使用以下命令:

import calendar

...
last_date = df.iloc[-1].name
last_unix = calendar.timegm(last_date.timetuple())

If last_date is in your local timezone, use this: 如果last_date在您当地的时区,请使用以下命令:

import time

...
last_date = df.iloc[-1].name
last_unix = time.mktime(last_date.timetuple())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3 AttributeError即使属性存在 - Python 3 AttributeError even though attribute exists 即使定义了属性,实例方法也会引发AttributeError - Instance method raises AttributeError even though the attribute is defined 即使已导入,如何修复属性错误 - How to fix Attribute error even though it is imported 即使定义了属性,NoneType 也会出错? - NoneType Error even though attribute is defined? AttributeError:即使发送电子邮件,也会出现__exit__错误 - AttributeError: __exit__ error even though email is sent Python class 属性错误,即使属性在 __init__ 中 - Python class Attribute Error even though attribute is in __init__ Python AttributeError:“createParticleAtom”对象没有属性“电子”,即使定义了“电子” - Python AttributeError: 'createParticleAtom' object has no attribute 'electron' even though 'electron' is defined TF-IDF 引发 AttributeError: 'int' object has no attribute 'lower' 即使数据中没有整数 - TF-IDF raises AttributeError: 'int' object has no attribute 'lower' even though there are no ints in he data AttributeError:&#39;NoneType&#39;对象没有属性&#39;get&#39;,即使我有一个我正在使用的类的实例 - AttributeError: 'NoneType' object has no attribute 'get' even though I have an instance of the class I'm using Beautiful Soup AttributeError: 'NoneType' object has no attribute 'find_all' 即使网页结构相同 - Beautiful Soup AttributeError: 'NoneType' object has no attribute 'find_all' even though webpage is structured the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM