ValueError:发现的输入变量,样品数量不一致:[4,3255],Python3



尝试执行我的代码时,我会收到以下错误

value error:发现的输入变量,示例数不一致:[4,3255],python3

我知道这是某种格式错误,但我不知道如何解决。

import pandas as pd
import quandl, math
import numpy as np
from sklearn import preprocessing, svm, cross_validation
from sklearn.linear_model import LinearRegression
df = quandl.get('WIKI/GOOGL')
df = df [['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume',]]
df['HL_PCT'] = (df['Adj. High'] - df['Adj. Open']) / df['Adj. Open'] *  100
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] *  100
df = df[['Adj. Close','HL_PCT','PCT_change','Adj. Volume']]
forecast_col = 'Adj. Close'
df.fillna(-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]
x_lately = x[-forecast_out:]

df.dropna(inplace=True)
y = np.array(df['label'])
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()
clf.fit(x_train, y_train)
accuracy = clf.score(x_test, y_test)
print(accuracy)

追溯:

Traceback (most recent call last):
  File "main.py", line 33, in <module>
    x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.2)
  File "C:UsersUserPythonlibsite-packagessklearncross_validation.py", line 2059, in train_test_split
    arrays = indexable(*arrays)
  File "C:UsersUserPythonlibsite-packagessklearnutilsvalidation.py", line 198, in indexable
    check_consistent_length(*result)
  File "C:UsersUserPythonlibsite-packagessklearnutilsvalidation.py", line 173, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [4, 3255]

我发现了问题。这就是应该格式化代码的方式:

X = np.array(df.drop(['label'],1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out:]

相关内容

  • 没有找到相关文章

最新更新