git 参考(课程+代码+作业)

代码不包括画图部分

1 单特征线性回归

import numpy as np

# 代价函数计算
def computeCost(x, y, theta):
    m = np.size(y)
    return (x.dot(theta) - y).dot((x.dot(theta) - y)) / m/2

# 迭代函数
def gradientDescent(x, y, theta, alpha, num_iters):
    m = np.size(y)
    J_history = np.zeros(num_iters)

    for i in range(num_iters):
        deltaJ = (x.T).dot(x.dot(theta)-y)/m
        theta = theta - alpha*deltaJ
        J_history[i] = computeCost(x, y, theta)
    return theta, J_history

data = np.loadtxt('ex1data1.txt', delimiter=',')
X = data[:, 0]
# print(X.shape)
Y = data[:, 1]
theta = np.zeros(2)
m = np.size(Y)
# X = np.vstack((np.ones(m), X)).T
# np.row_stack((a,b))
X = np.column_stack((np.ones(m), X))
# print(X.shape)

alpha = 0.01
num_iters = 1500 

theta, J_history = gradientDescent(X, Y, theta, alpha, num_iters)

print('theta:', theta)
print('J_history:', J_history)

2 多特征线性回归

2.1 归一化+梯度下降

import numpy as np

# 归一化函数
def featureNormalize(x):
    mu = np.mean(x, axis=0) #平均值
    # axis = 0:对各列求均值
    # axis = 1:对各行求均值
    # 当axis=None时,表示求全局均值

    sigma = np.std(x, axis=0) #标准差
    # axis=0时,表示求每一列标准差,
    # axis=1时,表示求每一行标准差,
    # 当axis=None时,表示求全局标准差

    x_norm = np.divide(x-mu, sigma) #矩阵除法
    return x_norm, mu, sigma

def computeCost(x, y, theta):
    m = np.size(y)
    return (x.dot(theta)-y).dot((x.dot(theta)-y))/m/2

def gradientDescent(x, y, theta, alpha, num_iters):
    m = np.size(y)
    J_history = np.zeros(num_iters)

    for i in range(num_iters):
        deltaJ = (x.T).dot(x.dot(theta)-y)
        theta = theta - alpha*deltaJ/m
        J_history[i] = computeCost(x, y, theta)
    return theta, J_history


data = np.loadtxt('ex1data2.txt', delimiter=',')
X = data[:, 0:2]
Y = data[:, 2]
m = np.size(Y)
theta = np.zeros(3)
X, mu, sigma = featureNormalize(X)
X = np.column_stack((np.ones(m), X))

alpha = 0.01
num_iters = 400

theta, J_history = gradientDescent(X, Y, theta, alpha, num_iters)
print('theta:', theta)
print('J_history:', J_history)
#theta: [334302.06399328  99411.44947359   3267.01285407]

2.2 正规方程

import numpy as np

data = np.loadtxt('ex1data2.txt', delimiter=',')
X = data[:, 0:2]
Y = data[:, 2]
m = np.size(Y)
X = np.column_stack((np.ones(m), X))
theta = np.linalg.inv(X.T@X)@X.T@Y #X.T@X 等价于 X.T.dot(X)
# np.linalg.inv求矩阵的逆, pinv求伪逆(当逆矩阵不存在也可有结果)

print('theta:', theta)
#theta: [89597.90954355   139.21067402 -8738.01911255]
#theta: [89597.9095428    139.21067402 -8738.01911233]
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐