计算损失
def compute_cost_multi(x, y, theta): m = len(y) hypothesis = np.dot(x, theta) err = (hypothesis - y) ** 2 return (1 / (2 * m)) * (np.sum(err))
特征缩放
def feature_normalize(x): x_norm = x.copy() # np.shape(x)[1] is the column of x mu = np.mean(x, axis=0) #计算每列的平均值,生成1行两列 sigma = np.std(x, axis=0, ddof=1) #样本的标准差 for i in range(np.shape(x)[0]): x_norm[i] = (x[i] - mu) / sigma return x_norm, mu, sigma
多变量梯度下降
def gradient_descent_multi(x, y, theta, alpha, num_iters): m = len(y) j_history = np.zeros((num_iters, 1)) for i in range(num_iters): hypothesis = np.dot(x, theta) sub = hypothesis - y theta = theta - (alpha / m) * (np.dot(x.T, sub)) j_history[i] = compute_cost_multi(x, y, theta) return theta, j_history
特征方法计算参数
def normal_eqn(x, y): temp_x = np.dot(x.T, x) matrix_x_inverse = np.mat(temp_x).I.getA() #.mat 从ndarray转换为numpy矩阵。.I求逆,.getA从numpy转换为ndarray temp_x = np.dot(matrix_x_inverse, x.T) return np.dot(temp_x, y)