Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

Expected 2D array, got 1D array instead: for a simple linear regression model in Python- Jupyter Notebook

Asked by Ranjana Admin Jun 10, 2021 1.6K views 1 answer
Share

About this question

 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.

Your answer

1 Answer

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.