计算损失

  1. def compute_cost_multi(x, y, theta):
  2. m = len(y)
  3. hypothesis = np.dot(x, theta)
  4. err = (hypothesis - y) ** 2
  5. return (1 / (2 * m)) * (np.sum(err))

特征缩放

  1. def feature_normalize(x):
  2. x_norm = x.copy()
  3. # np.shape(x)[1] is the column of x
  4. mu = np.mean(x, axis=0) #计算每列的平均值,生成1行两列
  5. sigma = np.std(x, axis=0, ddof=1) #样本的标准差
  6. for i in range(np.shape(x)[0]):
  7. x_norm[i] = (x[i] - mu) / sigma
  8. return x_norm, mu, sigma

多变量梯度下降

  1. def gradient_descent_multi(x, y, theta, alpha, num_iters):
  2. m = len(y)
  3. j_history = np.zeros((num_iters, 1))
  4. for i in range(num_iters):
  5. hypothesis = np.dot(x, theta)
  6. sub = hypothesis - y
  7. theta = theta - (alpha / m) * (np.dot(x.T, sub))
  8. j_history[i] = compute_cost_multi(x, y, theta)
  9. return theta, j_history

特征方法计算参数

  1. def normal_eqn(x, y):
  2. temp_x = np.dot(x.T, x)
  3. matrix_x_inverse = np.mat(temp_x).I.getA() #.mat 从ndarray转换为numpy矩阵。.I求逆,.getA从numpy转换为ndarray
  4. temp_x = np.dot(matrix_x_inverse, x.T)
  5. return np.dot(temp_x, y)