-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path房价预测.py
115 lines (94 loc) · 3.05 KB
/
房价预测.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from sklearn.linear_model import LinearRegression,SGDRegressor,Ridge,RidgeCV
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error
def model():
"""
正规方程
获取基本数据
数据基本处理
特征工程
机器学习
模型评估
:return:
"""
data = load_boston()
print(data)
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=22)
#特征工程
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
#机器学习
estimator = LinearRegression()
estimator.fit(x_train,y_train)
#模型评估
print('目标与测试:\n', estimator.predict(x_test))
print('模型系数:\n', estimator.coef_)
print('模型偏置量:\n', estimator.intercept_)
#模型评价
print('模型方差:\n', mean_squared_error(y_test, estimator.predict(x_test)))
print('准确率:\n', estimator.score(x_test, y_test))
data = load_boston()
print(data['data'].shape)
def mode2():
"""
梯度下降法
获取基本数据
数据基本处理
特征工程
机器学习
模型评估
:return:
"""
data = load_boston()
print(data)
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=22)
#特征工程
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
#机器学习
estimator = SGDRegressor()
estimator.fit(x_train,y_train)
#模型评估
print('目标与测试:\n', estimator.predict(x_test))
print('模型系数:\n', estimator.coef_)
print('模型偏置量:\n', estimator.intercept_)
#模型评价
print('模型方差:\n', mean_squared_error(y_test, estimator.predict(x_test)))
print('准确率:\n', estimator.score(x_test, y_test))
def mode3():
"""
岭回归
获取基本数据
数据基本处理
特征工程
机器学习
模型评估
:return:
"""
data = load_boston()
print(data)
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=22)
#特征工程
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
#机器学习
estimator = Ridge(alpha=1)
# estimator = RidgeCV(alphas=(0.1, 1, 10))#可以进行交叉验证
estimator.fit(x_train,y_train)
#模型评估
print('目标与测试:\n', estimator.predict(x_test))
print('模型系数:\n', estimator.coef_)
print('模型偏置量:\n', estimator.intercept_)
#模型评价
print('模型方差:\n', mean_squared_error(y_test, estimator.predict(x_test)))
print('准确率:\n', estimator.score(x_test, y_test))
if __name__ == '__main__':
# model()
# mode2()
# mode3()
pass