免费在线Ai工具箱 /机器学习
阅读主题
正文字体
字体大小

01.房价预测-线性回归模型训练

本节797字2025-05-22 11:39:42
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
# #预测房价
# # 距离地铁(米) 附近学校(个) 小区绿化率(0.6)
# # 根据已知的数据,预测新房子的房价
x = np.array([[500., 3., 0.1],  [1000., 1.0, 0.6], [750., 2.0, 0.3], [600.0, 5.0, 0.2],  [1200.0, 1.0, 0.6]])
y = np.array([20000.,  18000., 19000., 19500., 16500.])
lr = LinearRegression()
lr.fit(x, y)
x_new = np.array([[800., 4, 0.1]]) # 新房子的特征
print(lr.predict(x_new)) ## 结果为 17764.63963964
print("===================分割线===================")
data = pd.read_csv(r"D:\shaoxiao\文档\机器学习\boston.csv")
x = data.iloc[:, 1:14].values
y = data.iloc[:, -1].values
lr = LinearRegression()
lr.fit(x, y)
x_new = np.array([[0.00632,18.0,2.31,0.0,0.538,6.575,65.2,4.09,1.0,296.0,15.3,396.9,4.98]])
print(lr.predict(x_new)) ## 结果为 30.00384338


网友评论

相关作品