In [1]:
% matplotlib inline
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import sklearn
In [2]:
from sklearn import datasets
boston = datasets.load_boston()
In [3]:
boston.keys()
Out[3]:
In [5]:
boston.data.shape
Out[5]:
In [6]:
boston.feature_names
Out[6]:
In [8]:
print(boston.DESCR)
In [9]:
df = pd.DataFrame(boston.data)
In [11]:
df.head()
Out[11]:
In [12]:
df.columns = boston.feature_names
In [13]:
df.head()
Out[13]:
In [14]:
boston.target[:5]
Out[14]:
In [18]:
from sklearn import linear_model
lm = linear_model.LinearRegression()
In [20]:
X = df
Important functions: fit(), predict() and score()
In [22]:
lm.fit(X, boston.target)
Out[22]:
In [23]:
lm.intercept_
Out[23]:
In [25]:
lm.coef_
Out[25]:
In [27]:
pd.DataFrame(zip(X.columns, lm.coef_), columns=['features', 'coeffecients'])
Out[27]:
In [31]:
plt.scatter(df.RM, boston.target)
plt.xlabel("Avg num of rooms")
plt.ylabel("Housing price")
Out[31]:
In [30]:
lm.predict(X)[:5]
Out[30]:
In [33]:
plt.scatter(boston.target, lm.predict(X))
plt.xlabel("Price")
plt.ylabel("Predicted Price")
Out[33]: