Python 梯度下降算法实现 - 线性回归代价函数计算与参数更新
import numpy as np import pandas as pd
计算代价函数J(θ)
def computeCost(X, y, theta): m = len(y) J = np.sum((X.dot(theta) - y) ** 2) / (2 * m) return J
批量梯度下降
返回参数 θ 的值和每次迭代后代价函数的值,梯度下降公式参考编程要求梯度下降公式
def gradientDescent(X, y, theta, alpha, epoch): m = len(y) cost = np.zeros(epoch) # 初始化一个 ndarray ,包含每次 epoch 的cost for i in range(epoch): theta = theta - alpha / m * X.T.dot(X.dot(theta) - y) cost[i] = computeCost(X, y, theta) return theta, cost
读取数据
data = pd.read_csv('ex1data1.txt', header=None, names=['Population', 'Profit'])
特征缩放
data = (data - data.mean()) / data.std()
添加一列全为1的列,对应θ0
data.insert(0, 'Ones', 1)
将X和y分别赋值
X = data.iloc[:, :-1].values y = data.iloc[:, -1].values.reshape(-1, 1)
初始化theta
theta = np.zeros((2, 1))
设置alpha和epoch
alpha = 0.01 epoch = 1000
进行梯度下降
theta, cost = gradientDescent(X, y, theta, alpha, epoch)
计算代价函数的值
J = computeCost(X, y, theta) print(J)
原文地址: https://www.cveoy.top/t/topic/jUM8 著作权归作者所有。请勿转载和采集!