繁体   English   中英

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

[英]ValueError: Found input variables with inconsistent numbers of samples: [4, 3255], Python3

尝试执行我的代码时出现以下错误

ValueError:发现样本数量不一致的输入变量:[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:\Users\User\Python\lib\site-packages\sklearn\cross_validation.py", line 2059, in train_test_split
    arrays = indexable(*arrays)
  File "C:\Users\User\Python\lib\site-packages\sklearn\utils\validation.py", line 198, in indexable
    check_consistent_length(*result)
  File "C:\Users\User\Python\lib\site-packages\sklearn\utils\validation.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:]

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM