Linear Regression Error 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)
&lt;ipython-input-53-8dc82dc6fe8b&gt; in &lt;module&gt;()
----&gt; 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'],
--&gt; 482                          y_numeric=True, multi_output=True)
    483 
    484         if sample_weight is not None and np.atleast_1d(sample_weight).ndim &gt; 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 
--&gt; 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) &gt; 1:
    203         raise ValueError("Found input variables with inconsistent numbers of"
--&gt; 204                          " samples: %r" % [int(l) for l in lengths])
    205 
    206 

ValueError: Found input variables with inconsistent numbers of samples: [20, 10]
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)