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
Answers:
Strip the column names and it should work:
df.columns=[c.strip() for c in df.columns]
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)
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
Strip the column names and it should work:
df.columns=[c.strip() for c in df.columns]
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)