From 1e30c07171c5fd99e60d5beb5e2cc2ca9e2b5976 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 5 Jul 2024 22:21:17 +0800 Subject: [PATCH] maqi --- pppp.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 pppp.py diff --git a/pppp.py b/pppp.py new file mode 100644 index 0000000..ecea69f --- /dev/null +++ b/pppp.py @@ -0,0 +1,31 @@ +import numpy as np + +# 步骤 1: 数据准备 +# 为了简化,我们这里构造一个假想的数据集 +np.random.seed(0) +X = np.random.rand(100, 1) * 2000 # 假定房屋面积范围为0-2000平方英尺 +true_slope = 100 # 真实世界中每平方英尺的价格影响因子 +true_intercept = 50000 # 基础价格 +noise = np.random.randn(100, 1) * 5000 # 添加一些随机噪声 +y = true_slope * X + true_intercept + noise # 房屋价格 + +# 步骤 2: 初始化模型参数和超参数 +slope = 0 +intercept = 0 +learning_rate = 0.01 +epochs = 1000 + +# 步骤 3: 梯度下降 +for epoch in range(epochs): + y_pred = slope * X + intercept # 预测值 + error = y_pred - y # 误差 + D_slope = -2 * (X.T.dot(error)) / len(X) # 对斜率的梯度 + D_intercept = -2 * np.sum(error) / len(X) # 对截距的梯度 + slope -= learning_rate * D_slope # 更新斜率 + intercept -= learning_rate * D_intercept # 更新截距 + +# 步骤 4: 使用模型进行预测 +# 假设我们要预测一个面积为1500平方英尺的房屋价格 +new_house_area = np.array([[1500]]) +predicted_price = slope * new_house_area + intercept +print(f"预测的房屋价格(面积为1500平方英尺): ${predicted_price[0][0]:.2f}") \ No newline at end of file -- Gitee