在理解错误和寻找解决方案方面遇到了麻烦。我卡住了。我正在按照https://pythonprogramming.net/forecasting-predicting-machine-learning-tutorial上的教程学习机器学习和不太复杂的线性回归。我尝试过将列表改为不可变的,但我认为跟随教程的难点在于我收集的数据,似乎与教程中使用的数据非常不同。我正在尝试使用自己的数据。你可以将该网站上的代码与这里的代码进行比较。我做错了什么?如何克服这个障碍?
import csvimport numpy as npimport pandas as pdfrom sklearn import preprocessing, cross_validation, svmfrom sklearn.linear_model import LinearRegressionimport matplotlib.pyplot as pltfrom matplotlib import styleimport datetimeimport mathstyle.use('ggplot')df = {}bid = []btemp = []ask = []atemp = []low = []high = []close = []file=open("C:/documents/EURUSD.csv", "r")reader = csv.reader(file)for line in reader:t=line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8] btemp = line[2] + line[3] bid.append(btemp) atemp = line[4] + line[5] ask.append(atemp) low.append(line[6]) high.append(line[7]) close.append(line[8])bid.pop(0)ask.pop(0)low.pop(0)high.pop(0)close.pop(0)nBid = [float(i) for i in bid]nAsk = [float(i) for i in ask]nHigh = [float(i) for i in high]nLow = [float(i) for i in low]nClose = [float(i) for i in close]df['nClose'] = nClosediffHighLow = [(x1 - x2) for (x1, x2) in zip(nHigh, nLow)]sumBidAsk = [x1 + x2 for (x1, x2) in zip(nBid, nAsk)]nSumBidAsk = []for a in sumBidAsk: aTemp = (a / 2) * 100 nSumBidAsk.append(aTemp)df['HL_PCT'] = [x1 / x2 for (x1, x2) in zip(diffHighLow, nSumBidAsk)]diffCloseBid = [(x1 - x2) for (x1, x2) in zip(nClose, nBid)]divDiffCloseBid = [(x1 / x2) for (x1, x2) in zip(diffCloseBid, nBid)]nPCT_change = []for b in divDiffCloseBid: bTemp = b * 100 nPCT_change.append(bTemp)df['PCT_change'] = nPCT_changedf['forecast_col'] = df['nClose']df['forecast_out'] = int(math.ceil(0.01 * len(df)))df['laebl'] = df['forecast_col'].shift(-forecast_out)X = np.array(df.drop(['label'], 1))
已编辑 | 现在包括堆栈跟踪
File "<ipython-input-4-006cfd724c3e>", line 1, in <module>runfile('C:/Users/venichhe/Desktop/test3.py', wdir='C:/Users/venichhe/Desktop')File "C:\Users\venichhe\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfileexecfile(filename, namespace)File "C:\Users\venichhe\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfileexec(compile(scripttext, filename, 'exec'), glob, loc)File "C:/Users/venichhe/Desktop/test3.py", line 69, in <module>df['laebl'] = df['forecast_col'].shift(-forecast_out)AttributeError: 'list' object has no attribute 'shift'
回答:
请在更正拼写错误后尝试:
df['**laebl**'] = df['forecast_col'].shift(-forecast_out)
改为
df['label'] = df['forecast_col'].shift(-forecast_out)