Home > database >  Linear regression with one feature from Pandas dataframe
Linear regression with one feature from Pandas dataframe

Time:09-06

I have tried the code below

import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np

# Assign the dataframe to this variable.
# TODO: Load the data
bmi_life_data = pd.read_csv("bmi_and_life_expectancy.csv")
X= bmi_life_data['BMI'].values.reshape(-1,1)
y = bmi_life_data['Life expectancy'].values.reshape(-1,1)

# Make and fit the linear regression model
#TODO: Fit the model and Assign it to bmi_life_model
bmi_life_model = LinearRegression()
bmi_life_model.fit(X,y) 

# Mak a prediction using the model
# TODO: Predict life expectancy for a BMI value of 21.07931
laos_life_exp = bmi_life_model.predict(21.07931)

but it gives me the error

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.

Even after reshaping it. I have tried to not reshape it but it still gives me the same error.

CodePudding user response:

The error was in the prediction line

laos_life_exp = bmi_life_model.predict(21.07931)

should be

laos_life_exp = bmi_life_model.predict([[21.07931]])

to be of appropriate dimension

Thanks to @onyambu

  • Related