线性回归之波士顿房价预测

本文通过使用sklearn库中的波士顿房价数据集,实现了线性回归模型的从头构建。通过对特征进行缩放,利用梯度下降法优化权重和偏置,最终评估了模型的性能。展示了成本函数随迭代次数的变化,以及模型对训练数据的预测效果。
from sklearn.datasets import load_boston
import numpy as np
import matplotlib.pyplot as plt


def feature_scalling(X):
    mean = X.mean(axis=0)
    std = X.std(axis=0)
    return (X - mean) / std


def load_data(shuffled=False):
    data = load_boston()
    # print(data.DESCR)# 数据集描述
    X = data.data
    y = data.target
    X = feature_scalling(X)
    y = np.reshape(y, (len(y), 1))
    if shuffled:
        shuffle_index = np.random.permutation(y.shape[0])
        X = X[shuffle_index]
        y = y[shuffle_index]  # 打乱数据
    return X, y


def costJ(X, y, w, b):
    m, n = X.shape
    J = 0.5 * (1 / m) * np.sum((y - np.dot(X, w) - b) ** 2)
    return J


X, y = load_data()
m, n = X.shape  # 506,13
w = np.random.randn(13, 1)
b = 0.1
alpha = 0.01
cost_history = []
for i in range(5000):
    y_hat = np.dot(X, w) + b
    grad_w = -(1 / m) * np.dot(X.T, (y - y_hat))
    grad_b = -(1 / m) * np.sum(y - y_hat)
    w = w - alpha * grad_w
    b = b - alpha * grad_b
    if i % 100 == 0:
        cost_history.append(costJ(X, y, w, b))

# plt.plot(np.arange(len(cost_history)),cost_history)
# plt.show()
# print(cost_history)

y_pre = np.dot(X, w) + b
numerator = np.sum((y - y_pre) ** 2)
denominator= np.sum((y - y.mean()) ** 2)
print(1 - (numerator / denominator))

来源:https://github.com/TolicWang/MachineLearningWithMe/blob/master/Lecture_01/LinearRegression.py

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值