0
$\begingroup$

I tried creating a simple linear regression model on just 30 rows of data. I got this error while trying to fit the model:

dataset = pd.read_csv('Salary_Data.csv')
x=dataset.iloc[:, :-1]
y=dataset.iloc[:, 1]

from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3, random_state = 0)
regressor = LinearRegression()
regressor.fit(x_train, y_test)

Here is the error message I got:

ValueError                                Traceback (most recent call last)
<ipython-input-53-8dc82dc6fe8b> in <module>()
----> 1 regressor.fit(x_train, y_test)

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in fit(self, X, y, sample_weight)
    480         n_jobs_ = self.n_jobs
    481         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482                          y_numeric=True, multi_output=True)
    483 
    484         if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    581         y = y.astype(np.float64)
    582 
--> 583     check_consistent_length(X, y)
    584 
    585     return X, y

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
    202     if len(uniques) > 1:
    203         raise ValueError("Found input variables with inconsistent numbers of"
--> 204                          " samples: %r" % [int(l) for l in lengths])
    205 
    206 

ValueError: Found input variables with inconsistent numbers of samples: [20, 10]
$\endgroup$

closed as off-topic by Mark.F, Toros91, Sean Owen Feb 15 at 15:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about data science, within the scope defined in the help center." – Mark.F, Toros91, Sean Owen
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Whenever this happens, always, always print the shapes of x and y. print(x_train.shape) and print(y_train.shape) $\endgroup$ – Juan Antonio Gomez Moriano Jan 15 at 4:18
  • $\begingroup$ It looks like an error in your code or data; we'd generally close debugging questions here. $\endgroup$ – Sean Owen Feb 15 at 15:06
1
$\begingroup$

Your code has two typos:

1. Error when selecting data for the target variables.

In the second line, you are using [:, :-1] which means that you select all rows, and all columns except the last one.

In the third line, you are using [:, 1] which means that you select all rows, and only the first column instead of the last. Instead, you want to select the columns of your target variable, i.e. the last one:

y=dataset.iloc[:, -1]

2. Error when fitting the model

You are fitting your model using the X training set and the Y validation set. They have different length. Use this instead:

regressor.fit(x_train, y_train)
$\endgroup$
0
$\begingroup$

It seems there is a problem in the way you did split X variable. You can use the below format

X = df.loc[:, df.columns != 'Dependent Variable']
y = df.loc[:, df.columns == 'Dependent Variable']
$\endgroup$
0
$\begingroup$

x=dataset.iloc[:, :-1] :: It should be the Y actually, because you are selecting all rows and throwing one column out that is your target Y which is last column(-1).

Y=dataset.iloc[:, 1] :: Here again you are choosing second column only, i don't know why?

In X you need all rows & columns exceplt last column which is actually your target variable. Please revisit these two code-lines.

To choose columns & rows together :: use this.

x= data.iloc[:, 0:2] # first two columns of data frame with all rows

finally, thes two code line will be like:

x= data.iloc[:, 0:2]
y= dataset.iloc[:, :-1]

$\endgroup$

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