实现线性回归模型

下载房屋数据

点击下载 房屋数据house_data.csv ,作为案例使用

启动Jupyter

所有的操作将在jupyter notebook中完成

1
jupyter notebook

导入类库

1
2
3
4
5
import numpy as np                               #数值计算
import pandas as pd #数据分析
import seaborn as sns #函数绘图
import matplotlib.pyplot as plt #函数绘图
from sklearn.preprocessing import StandardScaler #数据预处理

通过梯度下降计算回归参数,实现线性回归模型

定义类LinearRegressionByMySelf,并实现方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class LinearRegressionByMySelf(object):
## 构造函数
# Learning_rate: 学习率
# epoch: 训练迭代次数
def __init__(self, Learning_rate=0.001, epoch=20):
self.Learning_rate = Learning_rate
self.epoch = epoch

## 训练方法
# x: 训练数据x,这里代指影响房价的特征
# y: 训练数据y,这里代指房价
def 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)
error = (y - output)
self.w[1:] += self.Learning_rate * x.T.dot(error)
self.w[0] += self.Learning_rate * error.sum()
cost = (error ** 2).sum() / 2.0
self.cost_list.append(cost)
return self

def Regression_input(self, x):
return np.dot(x, self.w[1:] + self.w[0])

def predict(self, x):
return self.Regression_input(x)

建立模型二

1
2
3
4
def Regression_plot(x, y, model):
plt.scatter(x, y, c='blue')
plt.plot(x, model.predict(x), color='red')
return None

导入数据并处理

1
2
3
4
5
6
7
8
9
10
11
12
df = pd.read_csv('data/house_data.csv')
sns.set(context = 'notebook')
cols = ['LSTAT', 'AGE', 'DIS', 'CRIM', 'MEDV', 'TAX', 'RM']
sns.pairplot(df[cols], size=2.5)
x = df[['LSTAT']].values
y = df['MEDV'].values
StandardScaler_x = StandardScaler()
StandardScaler_y = StandardScaler()
x_Standard = StandardScaler_x.fit_transform(x)
y_Standard = StandardScaler_y.fit_transform(y.reshape(-1, 1))
model = LinearRegressionByMySelf()
model.fit(x_Standard, y_Standard.ravel())

输出结果

1
2
3
4
plt.plot(range(1, model.epoch+1), model.cost_list)
plt.ylabel('SSE')
plt.xlabel('Epoch')
plt.show()

002

输出结果

1
2
3
plt.xlabel('percentage of the populcation')
plt.ylabel('house price')
plt.show()

003

打印结果

1
2
3
4
5
6
Rercentage_standard = StandardScaler_x.transform(2.5)
Price_standard = model.predict(Rercentage_standard)
print("House price %.3f" % \
StandardScaler_y.inverse_transform(Price_standard))
print('Intercept:' % model.w[0])
print('Intercept: %.3f' % model.w[0])

001

分享到