diff --git a/work2.py b/work2.py new file mode 100644 index 0000000000000000000000000000000000000000..9d88b8ee4ac33e4b5b6515bd5ed6d0973927a68a --- /dev/null +++ b/work2.py @@ -0,0 +1,36 @@ +from sklearn.linear_model import LinearRegression +import numpy as np + +# 房价数据集 +datas = [ + {"city": "吕梁", "area": 100, "rooms": 2, "school": 1, "style": 1, "price": 8000}, + {"city": "吕梁", "area": 100, "rooms": 2, "school": 1, "style": 1, "price": 7800}, + {"city": "吕梁", "area": 140, "rooms": 3, "school": 1, "style": 1, "price": 8600}, + {"city": "吕梁", "area": 135, "rooms": 3, "school": 1, "style": 1, "price": 8300}, + {"city": "吕梁", "area": 140, "rooms": 3, "school": 0, "style": 0, "price": 6500}, + {"city": "吕梁", "area": 135, "rooms": 3, "school": 0, "style": 0, "price": 6300}, + {"city": "太原", "area": 100, "rooms": 2, "school": 1, "style": 1, "price": 10000}, + {"city": "太原", "area": 100, "rooms": 2, "school": 1, "style": 1, "price": 9800}, + {"city": "太原", "area": 140, "rooms": 3, "school": 1, "style": 1, "price": 10600}, + {"city": "太原", "area": 135, "rooms": 3, "school": 1, "style": 1, "price": 10300}, + {"city": "太原", "area": 140, "rooms": 3, "school": 0, "style": 0, "price": 8500}, + {"city": "太原", "area": 135, "rooms": 3, "school": 0, "style": 0, "price": 8300}, +] + +# 将城市名转换为数值表示 +datas = [{**d, "city": 1 if d["city"] == "吕梁" else 2} for d in datas] + +# 转换为适合训练的格式 +X = np.array([[d["city"], d["area"], d["rooms"], d["school"], d["style"]] for d in datas]) +y = np.array([d["price"] for d in datas]) + +# 创建并拟合线性回归模型 +model = LinearRegression() +model.fit(X, y) + +# 假设要预测的房屋数据 +new_data = np.array([[2, 100, 2, 1, 1]]) # 假设城市为太原,面积100,2个房间,附近有学校,风格为1 + +# 进行预测 +predicted_price = model.predict(new_data) +print("预测的房屋价格为:", predicted_price)