0
$\begingroup$

I have loaded a dataset and converted into data frame while I am using linear regression I am receiving the following value error as shown in my code below. I am at the moment doing a tutorial and could not figure out how come the arrays are 1-D as the error shows.

from sklearn.datasets import load_boston
boston_dataset=load_boston()

#create a pandas dataframe and store the data
df_boston=pd.DataFrame(boston_dataset.data)
df_boston.columns=boston_dataset.feature_names

#append price, target as a new column in the dataset
df_boston['Price']=boston_dataset.target

#print first five observations
df_boston.head()

CRIM    ZN  INDUS   CHAS    NOX RM  AGE DIS RAD TAX PTRATIO B   LSTAT   Price
0   0.00632 18.0    2.31    0.0 0.538   6.575   65.2    4.0900  1.0 296.0   15.3    396.90  4.98    24.0
1   0.02731 0.0 7.07    0.0 0.469   6.421   78.9    4.9671  2.0 242.0   17.8    396.90  9.14    21.6
2   0.02729 0.0 7.07    0.0 0.469   7.185   61.1    4.9671  2.0 242.0   17.8    392.83  4.03    34.7
3   0.03237 0.0 2.18    0.0 0.458   6.998   45.8    6.0622  3.0 222.0   18.7    394.63  2.94    33.4
4   0.06905 0.0 2.18    0.0 0.458   7.147   54.2    6.0622  3.0 222.0   18.7    396.90  5.33    36.2


#assign features on x-axis
X_features=boston_dataset.data

#assign target on y-axis
Y_target=boston_dataset.target

#import linear model-the estimator
from sklearn.linear_model import LinearRegression
lineReg=LinearRegression()

#fit data into the estimator
lineReg.fit(X_features,Y_target)

ValueError                                Traceback (most recent call last)
<ipython-input-16-4b5b068e587b> in <module>()
      1 #fit data into the estimator
----> 2 lineReg.fit(X_features,Y_target)

/usr/local/lib/python3.4/dist-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:
$\endgroup$
1
  • $\begingroup$ Works for me wirth: Python 3.6 Sklearn: 0.19.1 Panda: 0.22.0 Check your libraries first. $\endgroup$ Commented Jan 26, 2018 at 8:30

2 Answers 2

1
$\begingroup$

It seems in your code you build a pandas dataframe but you do not use it. I recreated your code line by line and was unable to get the same error. Try the following

from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
boston = load_boston()
X = boston.data
Y = boston.target

lineReg = LinearRegression()
lineReg.fit(X, Y)
lineReg.score(X, Y)

This results in an error of $0.7406$. Of course, this result is kind of meaningless because you should split your data into a training and testing set in order to accurately test your results.

You should do the following

from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

boston = load_boston()
X = boston.data
Y = boston.target
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, shuffle= True)

lineReg = LinearRegression()
lineReg.fit(X_train, y_train)
lineReg.score(X_test, y_test )

This will always give a different score because the data is shuffled before being split into your two sets. You MUST separate your training data are the start of your algorithm and do not pollute your training set with the testing set otherwise you risk biasing your model! This is very important.

$\endgroup$
0
0
$\begingroup$

It is solved with this:

X_features=boston_dataset.data.values.reshape(-1,1)
Y_target=boston_dataset.target.values.reshape(-1,1)
$\endgroup$

Your Answer

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

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