本文先手动实现一个线性回归模型, 然后用sklearn的线性回归模型作对比
import pandas as pddf = pd.read_csv('house_data.csv') #数据集可到网上下载,波士顿房价df.head()
Out[1]:
In[2]:
import matplotlib.pyplot as pltimport seaborn as snsimport numpy as npsns.set(context='notebook')cols = ['MEDV', 'LSTAT', 'AGE', 'DIS', 'CRIM', 'TAX', 'RM']sns.pairplot(df[cols], height=2.5)
Out[2]:
<seaborn.axisgrid.PairGrid at 0x111c676d8>
In[3]:
from sklearn import datasetsdataset = datasets.load_boston() #也可以通过sklearn加载数据集
实现线性回归模型
In[4]:
class LinearRegressionByMyself(object):def __init__(self, Learning_rate=0.001, epoch=20):self.Learning_rate = Learning_rateself.epoch = epochdef fit(self, X, y):self.w = np.zeros(1 + X.shape[1])self.cost_list = []for i in range(self.epoch):output = self.Regression_input(X)#shape重置, 这里需要注意矩阵的运算问题output = output.T.reshape(y.shape)error = (y - output)#shape重置self.w[1:] += self.Learning_rate * X.T.dot(error).reshape(-1,)self.w[0] += self.Learning_rate * error.sum()cost = (error ** 2).sum() / 2.0self.cost_list.append(cost)return selfdef Regression_input(self, X):return np.dot(X, self.w[1:]) + self.w[0]def predict(self, X):return self.Regression_input(X)
模型写完了, 开始准备数据, 并且做数据的预处理 fit_transform
In[5]:
from sklearn.preprocessing import StandardScalerX = df[['LSTAT']].values # 这里只去了一维数据y = df[['MEDV']].valuesy = y.reshape(-1,1)StandardScaler_x = StandardScaler()StandardScaler_y = StandardScaler()X_standard = StandardScaler_x.fit_transform(X)y_standard = StandardScaler_y.fit_transform(y)
实际开始跑模型
In[6]:
model = LinearRegressionByMyself()model.fit(X_standard, y_standard)
Out[6]:
<__main__.LinearRegressionByMyself at 0x1172c0898>
画出模型的损失值随着epoch变化的折线图
可见前三次变化很大, 后面趋于稳定
In[7]:
plt.plot(range(1, model.epoch+1), model.cost_list)plt.ylabel('SSE')plt.xlabel('Epoch')
Out[7]:
<matplotlib.text.Text at 0x11626e470>
In[8]:
def Regression_plot(X, y, model):plt.scatter(X, y, c='blue')plt.plot(X, model.predict(X), color='red')return None
In[8]:
Regression_plot(X_standard, y_standard, model)
模型已经完成了, 上图看起来效果还不错, 接下来做个实际的预测吧
In[9]:
#这里要注意手动实现的数据, 是做了transform预处理的, 输出实际预测值是还要inverse_transform还原预测值Rercentage_standard = StandardScaler_x.transform([[23]])Price_standard = model.predict(Rercentage_standard)StandardScaler_y.inverse_transform(Price_standard)
Out[9]:
array([ 12.70271311])
使用sklearn构建线性模型
In[10]:
from sklearn.linear_model import LinearRegressionsk_model = LinearRegression()sk_model.fit(X,y)print(sk_model.coef_, sk_model.intercept_)
[[-0.95004935]] [ 34.55384088]
sklearn 只需要3行代码就能实现预测了, 很方便, 还不用数据预处理
In[11]:
Regression_plot(X, y, sk_model)
In[12]:
sk_model.predict(23) #实际预测结果
Out[13]:
array([[ 12.70270574]])