# -*- coding: utf-8 -*-
"""
任务:房屋价格预测
绘制线性回归图片
"""
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
DATA_FILE = './data_ai/house_data.csv'
# 使用的特征列
FEAT_COLS = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_above', 'sqft_basement']
def plot_fitting_line(linear_reg_model, X, y, feat):
"""
绘制线型回归线
"""
w = linear_reg_model.coef_
b = linear_reg_model.intercept_
plt.figure() #打开一个空画布
# 样本点
plt.scatter(X, y, alpha=0.5) # scatter:散点图,alpha:"透明度"
# 直线
plt.plot(X, w * X + b, c='red')
plt.title(feat) # 设置标题
plt.show()
def main():
house_data = pd.read_csv(DATA_FILE, usecols=FEAT_COLS + ['price'])
for feat in FEAT_COLS:
X = house_data[feat].values.reshape(-1, 1)
y = house_data['price'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=10)
linear_reg_model = LinearRegression()
linear_reg_model.fit(X_train, y_train)
r2_score = linear_reg_model.score(X_test, y_test)
print('特征:{},R2值:{}'.format(feat, r2_score))
# 绘制拟合直线
plot_fitting_line(linear_reg_model, X_train, y_train, feat)
if __name__ == '__main__':
main()
达到的效果大概就是这样了:
reshape(-1,1)
把行变成列向量→X = house_data[feat].values.reshape(-1, 1)
注意这里:
w = linear_reg_model.coef_
b = linear_reg_model.intercept_
是直接用的model
5. 一个编程的思路:如果有需要,你先在main里面吧一个函数写出来()里面带上你需要的参数 ,然后后面再到上面去把函数写出来。
比如这次,在下面main里面写着,就想绘图了,然后先把
# 绘制拟合直线
plot_fitting_line(linear_reg_model, X_train, y_train, feat)
写出来,然后再回到上面写函数.