import seaborn as sns
import pandas as pd
import numpy as nm
import matplotlib.pyplot as plt
df=sns.load_dataset('fmri')
x=df[['timepoint','subject']]
y=df['signal']
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_5388\1414350166.py in <module>
----> 1 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
~\Anaconda3\envs\MACHINELEARNING\lib\site-packages\sklearn\model_selection\_split.py in train_test_split(test_size, train_size, random_state, shuffle, stratify, *arrays)
2415 raise ValueError("At least one array required as input")
2416
-> 2417 arrays = indexable(*arrays)
2418
2419 n_samples = _num_samples(arrays[0])
~\Anaconda3\envs\MACHINELEARNING\lib\site-packages\sklearn\utils\validation.py in indexable(*iterables)
376
377 result = [_make_indexable(X) for X in iterables]
--> 378 check_consistent_length(*result)
379 return result
380
~\Anaconda3\envs\MACHINELEARNING\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
332 raise ValueError(
333 "Found input variables with inconsistent numbers of samples: %r"
--> 334 % [int(l) for l in lengths]
335 )
336
ValueError: Found input variables with inconsistent numbers of samples: [53940, 1064]
2 Answers
In addition to what Nick ODell pointed out in the comments about X not being defined, the ValueError you're seeing is because you are calling train_test_split on two different sized arrays. Your X and y inputs to train_test_split need to be the same sizes, here they are 53940 and 1064 respectively. In other words, for every input, you need exactly one output. hth.
Yes, please check x
in x=df[['timepoint','subject']]
versus X
in train_test_split(X...
However,
df=sns.load_dataset('fmri')
This should be
df=pd.read_csv('fmri') # but I don't know the format for this data set
Just be sure that the script is run from the same directory as fmri
A small example data set would be cool. You are using both sns
and matplotlib
for plotting, but ins
will not load a data set thats pandas
-
-
$\begingroup$ Upvotes or acceptance is always preferred to thanks @brijesh $\endgroup$– M__May 22 at 18:00
X
which is not defined. (The variablex
is defined, but Python is case sensitive.) Is this a typo, or is the variableX
defined somewhere? (If you're using jupyter, you may want to press "Restart Kernel" to make sure there are not any variables hanging around from previous versions of this code.) $\endgroup$