在Andrew Ng的机器学习课程中,当使用Python进行多变量的梯度下降时,可以按照以下步骤进行操作:
import numpy as np # 处理矩阵运算
import pandas as pd # 数据处理
import matplotlib.pyplot as plt # 绘图
data = pd.read_csv('data.csv') # 读取数据文件
X = data.iloc[:, :-1] # 输入特征
y = data.iloc[:, -1] # 目标变量
m = len(y) # 样本数量
def feature_normalize(X):
"""
对输入特征进行标准化处理
"""
mean = np.mean(X, axis=0)
std = np.std(X, axis=0)
X_norm = (X - mean) / std
return X_norm, mean, std
X, mean, std = feature_normalize(X)
X = np.hstack((np.ones((m, 1)), X)) # 在X矩阵的第一列添加一列全为1的偏置项
theta = np.zeros(X.shape[1]) # 初始化参数theta为0向量
iterations = 1500 # 迭代次数
alpha = 0.01 # 学习率
def compute_cost(X, y, theta):
"""
计算代价函数J的值
"""
predictions = X.dot(theta)
sqr_errors = np.power(predictions - y, 2)
J = 1 / (2 * m) * np.sum(sqr_errors)
return J
initial_cost = compute_cost(X, y, theta)
def gradient_descent(X, y, theta, alpha, iterations):
"""
梯度下降算法更新参数theta
"""
J_history = []
for _ in range(iterations):
predictions = X.dot(theta)
errors = np.dot(X.transpose(), (predictions - y))
theta -= alpha * (1 / m) * errors
J_history.append(compute_cost(X, y, theta))
return theta, J_history
theta, J_history = gradient_descent(X, y, theta, alpha, iterations)
plt.plot(range(iterations), J_history)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.title('Cost function convergence')
plt.show()
这些代码示例演示了如何使用Python实现Andrew Ng机器学习课程中的多变量梯度下降算法,并绘制出代价函数的收敛曲线。希望对你解决问题有所帮助!