Python Forum
ValueError: Found input variables with inconsistent numbers of samples: [0, 3] - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: ValueError: Found input variables with inconsistent numbers of samples: [0, 3] (/thread-14359.html)



ValueError: Found input variables with inconsistent numbers of samples: [0, 3] - ayaz786amd - Nov-26-2018

Hi

import pandas as pd
import quandl, math
import numpy as np
import sys
from sklearn import preprocessing, model_selection, svm   # model_selection replaces cross_validation
from sklearn.linear_model import LinearRegression
sys.path.insert(0, 'C:\Program Files\Scripts')
df = quandl.get('FINRA/FORF_TLLTD')

df['PCT']= df['ShortVolume']/df['TotalVolume']*100
df = df[['ShortVolume','TotalVolume', 'PCT']]
forecast_col = 'ShortVolume'
df.fillna(-99999, inplace= True)
forecast_out = int(math.ceil(0.01*len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)
df.dropna(inplace=True)

X = np.array(df.drop(['label'], 1))
y = np.array(df['label'])
X= preprocessing.scale(X)
X= X[:-forecast_out+1]
y = np.array(df['label'])
X_train,X_test,y_train,y_test= model_selection.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)
Error:
Traceback (most recent call last): File "C:/Program Files/PycharmProjects/code examples/code practice.py", line 39, in <module> X_train,X_test,y_train,y_test= model_selection.train_test_split(X, y, test_size=0.2) File "C:\Users\Ayaz\AppData\Roaming\Python\Python37\site-packages\sklearn\model_selection\_split.py", line 2184, in train_test_split arrays = indexable(*arrays) File "C:\Users\Ayaz\AppData\Roaming\Python\Python37\site-packages\sklearn\utils\validation.py", line 260, in indexable check_consistent_length(*result) File "C:\Users\Ayaz\AppData\Roaming\Python\Python37\site-packages\sklearn\utils\validation.py", line 235, in check_consistent_length " samples: %r" % [int(l) for l in lengths]) ValueError: Found input variables with inconsistent numbers of samples: [0, 3]



RE: ValueError: Found input variables with inconsistent numbers of samples: [0, 3] - Larz60+ - Nov-26-2018

The error occurs on line 39, you are only showing 27 lines of code.


RE: ValueError: Found input variables with inconsistent numbers of samples: [0, 3] - ayaz786amd - Nov-27-2018

i have some notes on above lines not relevant to this code.
Code works when i removed
X= X[:-forecast_out+1]