1
$\begingroup$
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]
New contributor
brijesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
2
  • $\begingroup$ This example references a variable X which is not defined. (The variable x is defined, but Python is case sensitive.) Is this a typo, or is the variable X 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$
    – Nick ODell
    May 21 at 22:54
  • $\begingroup$ Thanks Nick ODell you are right. X was not defined properly. $\endgroup$
    – brijesh
    May 22 at 17:08

2 Answers 2

0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks a lot Brewmaster321. $\endgroup$
    – brijesh
    May 22 at 17:13
0
$\begingroup$

Yes, please check xin 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

$\endgroup$
2
  • $\begingroup$ Thank you M_183 $\endgroup$
    – brijesh
    May 22 at 17:15
  • $\begingroup$ Upvotes or acceptance is always preferred to thanks @brijesh $\endgroup$
    – M__
    May 22 at 18:00

Your Answer

brijesh is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.