【无标题】

最优化线性回归分析(波士顿房价预测)和模型预测

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.preprocessing import StandardScaler
# 加载波士顿房价数据集
boston = load_boston()
# 获取特征和目标变量
X = boston.data
y = boston.target
# 特征标准化处理
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 添加偏置项
X = np.concatenate([np.ones((X.shape[0], 1)), X], axis=1)
# 定义梯度下降函数
def gradient_descent(X, y, learning_rate, num_iterations):
    num_samples, num_features = X.shape
    theta = np.zeros(num_features)
    loss_history = []
    for i in range(num_iterations):
        y_pred = np.dot(X, theta)  # 计算预测值
        loss = np.mean((y_pred - y) ** 2)# 计算损失函数
        loss_history.append(loss)
        gradient = np.dot(X.T, (y_pred - y)) / num_samples
        theta -= learning_rate * gradient# 更新参数
    return theta, loss_history
# 设置学习率和迭代次数
learning_rate = 0.01
num_iterations = 1000
# 使用梯度下降算法拟合线性模型
theta, loss_history = gradient_descent(X, y, learning_rate, num_iterations)
# 打印训练得到的参数
print("参数theta:", theta)

plt.plot(loss_history)
plt.xlabel("迭代次数")
plt.ylabel("损失函数")
plt.title("损失函数变化曲线")
plt.show()

优化代码

def gradient_descent(X, y, learning_rate, num_iterations):
    num_samples, num_features = X.shape
    theta = np.zeros(num_features)
    last_loss = np.inf
    loss_history = []
    for i in range(num_iterations):
        y_pred = np.dot(X, theta)  # 计算预测值
        loss = np.mean((y_pred - y) ** 2)  # 计算损失函数
        gradient = np.dot(X.T, (y_pred - y)) / num_samples
        theta -= learning_rate * gradient  # 更新参数
        # 保存损失函数值
        if i % 10 == 0:
            loss_history.append(loss)
        # 判断收敛条件
        if np.abs(last_loss - loss) < 1e-5:
            break
        last_loss = loss
    return theta, loss_history

模型预测

theta = np.array([22.53183355, -0.78102837, 0.81194215, -0.27316748, 0.74256629, -1.57697862,
                  2.88943522, -0.10349181, -2.74240875, 1.45982586, -0.88309816, -1.95217556,
                  0.87224133, -3.64278025])

X_new = np.array([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0],  # 示例数据点1
                  [14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0]])  # 示例数据点2
y_pred = np.dot(X_new, theta[1:]) + theta[0]  # theta[1:]是特征权重,theta[0]是偏置项
# 输出预测结果
print("预测值:", y_pred)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值