Multiple regression model using scikit learn in python

Question:

when I am taking the independent variable ‘age’ ….reg.fit() showing an error like ” [‘age’] not in index “…..can anyone please help me ?

import pandas as pd
import numpy as np
from sklearn import linear_model
df=pd.read_csv('homeprices_multivariate.csv')
import math
median_bedrooms=math.floor(df.bedrooms.median())
df.bedrooms=df.bedrooms.fillna(median_bedrooms)
reg=linear_model.LinearRegression()
reg.fit(df[['area','bedrooms','age']],df.price)

then the error

[‘age’] not in index

Asked By: Tanvir Ahmed

||

Answers:

Strip the column names and it should work:

df.columns=[c.strip() for c in df.columns]
Answered By: Binyamin Even

The age column heading has an extra space in your data set. Your last line should be changed to:

reg.fit(df[['area','bedrooms','age ']],df.price)
Answered By: user19077978
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.