机器学习-多因子线性回归

以Income、House age、Numbers Of Rooms、Population、Area为输入变量,建立多因子模型,预测合理房价price,评估模型表现。

代码如下:

python 复制代码
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import  mean_squared_error,r2_score
from matplotlib import pyplot as plt

data=pd.read_csv('usa_housing_price.csv')
fig = plt.figure(figsize=[10,10])

fig1=plt.subplot(2,3,1)  # 表示2行3列,第1个图
plt.scatter(data.loc[:,'Avg.Area Income'],data.loc[:,'Price'])
plt.title('Price vs Area Income')

fig2=plt.subplot(2,3,2)  # 表示2行3列,第2个图
plt.scatter(data.loc[:,'Avg.Area House Age'],data.loc[:,'Price'])
plt.title('Price vs House Age')

fig3=plt.subplot(2,3,3)  # 表示2行3列,第3个图
plt.scatter(data.loc[:,'Avg.Area Number of Rooms'],data.loc[:,'Price'])
plt.title('Price vs Avg.Area Number of Rooms')

# fig4=plt.subplot(2,3,4)  # 表示2行3列,第4个图
# plt.scatter(data.loc[:,'Area Population'],data.loc[:,'Price'])
# plt.title('Price vs Area Population')

fig5=plt.subplot(2,3,5)  # 表示2行3列,第5个图
plt.scatter(data.loc[:,'size'],data.loc[:,'Price'])
plt.title('Price vs size')
plt.show()

Y=data.loc[:,'Price']
# define X_multi
X_multi =data.drop(['Price'],axis=1) # 除了Price的变量都放入X_multi,不需要reshape
# set up 2nd linear model
LR_multi=LinearRegression()

LR_multi.fit(X_multi,Y)
# make prediction
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(mean_squared_error_multi,r2_score_multi)

fig6=plt.figure(figsize=[8,5])
plt.scatter(Y,y_predict_multi) # 拟合真实Y和预测Y关系图像
plt.show()

# 使用模型进行预测Price
x_test=[65000,5,5,30000,200]
x_test=np.array(x_test).reshape(1,-1)
y_test_predict=LR_multi.predict(x_test)
print("预测房价",y_test_predict)

数据如图:


输出结果:

单因子与价格(Price)关系图:

真实Y和预测Y关系图:

相关推荐
Leslie165几秒前
注意力不是全连接层换名字:多头自注意力的张量实验
人工智能
Postkarte不想说话几秒前
vLLM自定义对话模板
人工智能
具身AGI1 分钟前
宇树打新:物理AI 国产的「本体」先跑通了商业化
人工智能
Json____2 分钟前
AI内容创作平台项目源码
人工智能·ai·agent·内容创作·wwwoop.com
Jay-r3 分钟前
DeepSeek Harness 极简上手:装好、玩熟、让它自己长新能力
人工智能·windows·ai·github·ai编程·deepseek·harness
CodeBlog-star17 分钟前
LLM能力与边界:多模态、幻觉、上下文窗口及开源模型对比
人工智能·python·开源·llm
COOLMO研究AI20 分钟前
Python 如何实现 AI API 的动态路由与多通道负载均衡:多账号与多供应商的高可用调度
人工智能·python·负载均衡
不懂的浪漫27 分钟前
吴恩达《AI Engineering Skills Map》译读:四项核心能力与持续学习底座
人工智能·学习
冬奇Lab34 分钟前
Code Agent 解剖(02):agent 是怎么一轮一轮思考和行动的?
人工智能·llm·agent
菜冻鱼37 分钟前
Python-sklearn-降维
开发语言·人工智能·python·机器学习·支持向量机·sklearn