Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to learn data science with python by browsing on the net and going through random topics and websites.

After a long time of learning, I tried to get hands-on experience, so I started coding the linear regression model and after coding, I am getting stuck with an error.

I am mentioning the code which I used to build the linear regression model and also the error below. Can anyone help me out with solving the error?

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from numpy.random import randn

np.random.seed(101)

df3=pd.DataFrame(randn(5,2),index ='0 1 2 3 4'.split(), columns='Test Price'.split())

y= df3['Price']

x= df3['Test']

import sklearn.model_selection as model_selection

X_train, X_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2, random_state=101)

from sklearn.linear_model import LinearRegression

lm2= LinearRegression()

lm2.fit(X_train,y_train)

ERROR

ValueError: Expected 2D array, got 1D array instead:

array=[-2.01816824  0.65111795  0.90796945 -0.84807698].

Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

1 Answer

0 votes
by (36.8k points)

As shown the error mentions that you are passing a single dimension array but you need to pass the two-dimensional array. So you need to reshape the array using the reshape ()

In your case you need to reshape X using the code as shown below:

lm2.fit(X_train.values.reshape(-1,1),y_train)

Here you are reshaping the X to (n_samples,1). in your case -1 represents the n_sample.

 If you wish to learn Data Science visit this Data Science course and gain knowledge

Browse Categories

...