Issue
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)
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]
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.
Answered By - Isa-Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.