【问题标题】:ValueError: Found input variables with inconsistent numbers of samples: [4, 3255], Python3ValueError:发现样本数量不一致的输入变量:[4, 3255],Python3
【发布时间】:2019-11-15 23:17:20
【问题描述】:

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

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]

【问题讨论】:

  • 你能显示 Traceback 吗?
  • 我对此很陌生,不知道如何显示回溯
  • Traceback 是您收到此错误消息的输出:ValueError: Found input variables with contrast numbers of samples: [4, 3255]
  • 你能缩小问题的范围吗?
  • 好了,我提交了上面的 Tracebacka

标签: python python-3.x machine-learning scikit-learn


【解决方案1】:

我发现了问题。这就是代码应该被格式化以使其工作的方式:

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

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2019-10-20
    • 2021-06-20
    • 2018-06-25
    • 2018-12-04
    • 2021-05-31
    • 2020-08-19
    • 2021-09-12
    • 2021-08-06
    相关资源
    最近更新 更多