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
| """ 数据说明: Avg.AreaIncome:区域平均收入 Avg.Area House Age:平均房屋年龄 Avg.Area Number of Rooms:平均房间数量 Area Population:区域人口 size:尺寸 Price:价格 """
import pandas as pd import numpy as np import matplotlib import matplotlib.pyplot as plt matplotlib.use('TkAgg') from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error,r2_score data = pd.read_csv('usa_house_price.csv') data.head() fig = plt.figure(figsize = (10,10)) fig1 = plt.subplot(231) plt.scatter(data['AreaIncome'], data['Price'])
plt.show()
x = data.loc[:,'size'] y = data.loc[:,'Price'] x = np.array(x).reshape(-1,1)
LR1 = LinearRegression()
LR1.fit(x,y)
print(y_pred_1) mean_squared_error_1 = mean_squared_error(y,y_pred_1) r2_score_1 = r2_score(y,y_pred_1) print(mean_squared_error_1,r2_score_1) fig2 = plt.figure(figsize=(8,5)) plt.scatter(x,y) plt.plot(x,y_pred_1,'r') plt.show()
x_multi = data.drop(['Price'],axis=1)
LR_multi = LinearRegression()
LR_multi.fit(x_multi,y)
y_pred_multi = LR_multi.predict(x_multi) print(y_pred_multi) mean_squared_error_multi = mean_squared_error(y,y_pred_multi) r2_score_multi = r2_score(y,y_pred_multi) print(mean_squared_error_multi,r2_score_multi) fig3 = plt.figure(figsize=(8,5)) plt.scatter(y,y_pred_multi) plt.show() x_text = [65000,5,5,30000,200] x_text = np.array(x_text).reshape(-1,1) print(x_text) y_text_pred = LR_multi.predict(x_text) print(y_text_pred)
|