-
Notifications
You must be signed in to change notification settings - Fork 2
/
price_prediction_of_house.py
64 lines (57 loc) · 2.06 KB
/
price_prediction_of_house.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
'''
Created on July 3, 2019
@author: Terry
@email:terryluohello@qq.com
'''
import time
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
def housePrediction():
""" 波士顿房价预测
描述:scikit-learn机器学习:常用算法原理及编程实战 示例5.5 测算房价
INPUT:
无
OUPUT:
无
"""
# 导入数据
boston = load_boston()
x = boston.data
y = boston.target
# 拆分数据集为训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=3)
# 使用线性回归算法进行预测
model = LinearRegression(normalize=True)
# 计算时间
start = time.clock()
# 训练模型并且计算得分
model.fit(x_train, y_train)
train_score = model.score(x_train, y_train)
cv_score = model.score(x_test, y_test)
print('elaspe: {0:.6f};train_score: {1:0.6f}; cv_score: {2:.6f}'.format(time.clock()-start, train_score, cv_score))
# 使用二阶多项式来你和数据
model = polynomial_model(degree=2)
# 计算时间
start = time.clock()
# 训练模型并且计算得分
model.fit(x_train, y_train)
train_score = model.score(x_train, y_train)
cv_score = model.score(x_test, y_test)
print('elaspe: {0:.6f};train_score: {1:0.6f}; cv_score: {2:.6f}'.format(time.clock()-start, train_score, cv_score))
def polynomial_model(degree=1):
""" 多项式模型函数
INPUT:
degree:多项式的项数,默认为1
OUPUT:
pipeline:多项式模型
"""
polynomial_features = PolynomialFeatures(degree=degree, include_bias=False)
linear_regression = LinearRegression(normalize=True)
pipeline = Pipeline([("polynomial_features",polynomial_features),
("linear_regression",linear_regression)])
return pipeline
if __name__ == "__main__":
housePrediction()