Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: Found input variables with inconsistent numbers of samples: [915, 229] in easy mashine learning sample

import pandas as pd
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

df=pd.read_csv('https://raw.githubusercontent.com/dataprofessor/data/master/delaney_solubility_with_descriptors.csv')

X = df.drop('logS', axis=1)
y = df['logS']

X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

lr = LinearRegression()
lr.fit(X_train, y_train)

y_lr_train_pred = lr.predict(X_train)
y_lr_test_pred = lr.predict(X_test)

lr_train_mse = mean_squared_error(y_train, y_lr_train_pred)
lr_train_r2 = r2_score(y_train, y_lr_train_pred)
lr_test_mse = mean_squared_error(y_test, y_lr_test_pred)
lr_test_r2 = r2_score(y_test, y_lr_test_pred)

print(lr_train_mse)

lr_results = pd.DataFrame(['Linear regression',lr_train_mse, lr_train_r2, lr_test_mse, lr_test_r2]).transpose()
lr_results.columns = ['Method','Training MSE','Training R2','Test MSE','Test R2']

I have a code that tries to predict the logS values. This code is not mine, but from the guide. There are no errors there. So what could be the problem?

full error message here:
Traceback (most recent call last):
  File "D:\Python\AI\test\main.py", line 14, in <module>
    lr.fit(X_train, y_train)
  File "D:\Python\AI\test\venv\lib\site-packages\sklearn\base.py", line 1152, in wrapper
    return fit_method(estimator, *args, **kwargs)
  File "D:\Python\AI\test\venv\lib\site-packages\sklearn\linear_model\_base.py", line 678, in fit
    X, y = self._validate_data(
  File "D:\Python\AI\test\venv\lib\site-packages\sklearn\base.py", line 622, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "D:\Python\AI\test\venv\lib\site-packages\sklearn\utils\validation.py", line 1164, in check_X_y
    check_consistent_length(X, y)
  File "D:\Python\AI\test\venv\lib\site-packages\sklearn\utils\validation.py", line 407, in check_consistent_length
    raise ValueError(
ValueError: Found input variables with inconsistent numbers of samples: [915, 229]

I changed the data-file, and values in ValueError was change too.
I change test_size value, and values in ValueError was change
For test_size = 0.4 :

ValueError: Found input variables with inconsistent numbers of samples: [686, 458]

:/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is an issue with how you are splitting your data using ‘train_test_split’.

You should swap ‘y_train’ with ‘X_test’. See below:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

The correct order is to first specify the features (‘X’) and then the target variable (‘y’) for both training and testing datasets.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading