简介

对于房价的预测,采用多因子进行预测,例如房屋面积,人口密度等等。

参考链接

https://blog.csdn.net/weixin_46344368/article/details/105775078 (内涵数据集)

code

import pandas as pd
import numpy as np
data = pd.read_csv('usa_housing_price.csv')
data.head()

from matplotlib import pyplot as plt
fig = plt.figure(figsize=(10,10))
fig1 = plt.subplot(231)
plt.scatter(data.loc[:,'Avg. Area Income'],data.loc[:,'Price'])
plt.title("Price VS Income")

fig2 = plt.subplot(232)
plt.scatter(data.loc[:,'Avg. Area House Age'],data.loc[:,'Price'])
plt.title("Price VS House Age")

fig3 = plt.subplot(233)
plt.scatter(data.loc[:,'Avg. Area Number of Rooms'],data.loc[:,'Price'])
plt.title("Price VS Area Number of Rooms")

fig4 = plt.subplot(234)
plt.scatter(data.loc[:,'Area Population'],data.loc[:,'Price'])
plt.title("Price VS Area Population")

fig5 = plt.subplot(235)
plt.scatter(data.loc[:,'size'],data.loc[:,'Price'])
plt.title("Price VS size")

plt.show()


X = data.loc[:,'size']
Y = data.loc[:,'Price']
X.head()


X= np.array(X).reshape(-1,1)
Y= np.array(Y).reshape(-1,1)

from sklearn.linear_model import LinearRegression
LR1 = LinearRegression()
LR1.fit(X,Y)

y_predict_1 = LR1.predict(X)
print(y_predict_1)

# evaluate the model
from sklearn.metrics import mean_squared_error,r2_score
mean_squared_error_1 = mean_squared_error(Y,y_predict_1)
r2_score_1 = r2_score(Y,y_predict_1)
print(r2_score_1, mean_squared_error_1)


fig6 = plt.figure(figsize=(8,5))
plt.scatter(X,Y)
plt.plot(X,y_predict_1,'r')
plt.show()

#define X_multi
X_multi = data.drop(["Price"],axis=1)
X_multi

# set up 2nd linear model
LR_multi = LinearRegression() 
# train the model
LR_multi.fit(X_multi,Y)


y_predict_multi = LR_multi.predict(X_multi)
print(y_predict_multi)

mean_squared_error_multi = mean_squared_error(Y,y_predict_multi)
r2_score_multi = r2_score(Y,y_predict_multi)
print(r2_score_multi, mean_squared_error_multi)

fig7 = plt.figure(figsize=(8,5))
plt.scatter(Y,y_predict_multi)
plt.show()
# 单独变量的预测
X_test = [65000,5,5,30000,200]
X_test = np.array(X_test).reshape(1,-1)
print(X_test)

y_test_predict = LR_multi.predict(X_test)
print(y_test_predict)

image

对于多因子的预测y值对应图

posted on 2022-04-19 20:27  HDU李少帅  阅读(171)  评论(0编辑  收藏  举报