多元回归损失函数

image.png

二元回归图解

image.png

损失函数公式推导

image.png
此时,损失函数和 m 有关,因此需要 除以m
image.png

编码实现

准备数据

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. np.random.seed(666)
  4. x = 2 * np.random.random(size=100)
  5. y = x * 3. + 4. + np.random.normal(size=100)
  6. x
  1. array([1.40087424, 1.68837329, 1.35302867, 1.45571611, 1.90291591,
  2. 0.02540639, 0.8271754 , 0.09762559, 0.19985712, 1.01613261,
  3. 0.40049508, 1.48830834, 0.38578401, 1.4016895 , 0.58645621,
  4. 1.54895891, 0.01021768, 0.22571531, 0.22190734, 0.49533646,
  5. 0.0464726 , 1.45464231, 0.68006988, 0.39500631, 1.81835919,
  6. 1.95669397, 1.06560509, 0.5182637 , 1.16762524, 0.65138131,
  7. 1.77779863, 1.25280905, 1.63774738, 1.09469084, 0.83342401,
  8. 1.48609438, 0.73919276, 0.15033309, 1.55038596, 0.43881849,
  9. 0.15868425, 0.97356104, 0.3073478 , 1.65693027, 0.38273714,
  10. 0.54081791, 1.12206884, 1.80476078, 1.70357668, 0.83616392,
  11. 0.78695254, 0.03244103, 0.59842674, 0.70755644, 1.78700533,
  12. 1.57227314, 1.54277385, 0.84010971, 1.55205028, 0.92861629,
  13. 0.36354033, 1.76805121, 1.43758454, 1.3437626 , 0.51312727,
  14. 0.86160364, 0.03290715, 0.46998765, 1.02234262, 0.58401848,
  15. 1.00378702, 0.99654626, 0.20754305, 0.89288623, 1.93837834,
  16. 1.47694224, 1.43910122, 1.78608677, 1.92534936, 0.39410046,
  17. 1.42917993, 0.32384788, 1.73250954, 1.24764049, 1.91891025,
  18. 1.04828408, 0.07286576, 1.45374316, 0.00781969, 0.100588 ,
  19. 1.98398463, 0.424515 , 1.89474133, 0.9030811 , 1.99758935,
  20. 1.29500298, 1.40448142, 0.85916353, 0.33554952, 0.23626619])
  1. X = x.reshape(-1, 1) # 转二维,方便处理多维数据
  2. X[:20]
  1. array([[1.40087424],
  2. [1.68837329],
  3. [1.35302867],
  4. [1.45571611],
  5. [1.90291591],
  6. [0.02540639],
  7. [0.8271754 ],
  8. [0.09762559],
  9. [0.19985712],
  10. [1.01613261],
  11. [0.40049508],
  12. [1.48830834],
  13. [0.38578401],
  14. [1.4016895 ],
  15. [0.58645621],
  16. [1.54895891],
  17. [0.01021768],
  18. [0.22571531],
  19. [0.22190734],
  20. [0.49533646]])
  1. y[:20]
  1. array([8.91412688, 8.89446981, 8.85921604, 9.04490343, 8.75831915,
  2. 4.01914255, 6.84103696, 4.81582242, 3.68561238, 6.46344854,
  3. 4.61756153, 8.45774339, 3.21438541, 7.98486624, 4.18885101,
  4. 8.46060979, 4.29706975, 4.06803046, 3.58490782, 7.0558176 ])
  1. plt.scatter(x, y)
  2. plt.show()

image.png

使用梯度下降法训练

  1. def J(theta, X_b, y):
  2. try:
  3. return np.sum((y - X_b.dot(theta))**2) / len(X_b)
  4. except:
  5. return float('inf')
  1. def dJ(theta, X_b, y):
  2. res = np.empty(len(theta))
  3. res[0] = np.sum(X_b.dot(theta) - y)
  4. for i in range(1, len(theta)):
  5. res[i] = (X_b.dot(theta) - y).dot(X_b[:,i])
  6. return res * 2 / len(X_b)
  1. def gradient_descent(X_b, y, initial_theta, eta, n_iters = 1e4, epsilon=1e-8):
  2. theta = initial_theta
  3. cur_iter = 0
  4. while cur_iter < n_iters:
  5. gradient = dJ(theta, X_b, y)
  6. last_theta = theta
  7. theta = theta - eta * gradient
  8. if(abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):
  9. break
  10. cur_iter += 1
  11. return theta
  1. X_b = np.hstack([np.ones((len(x), 1)), x.reshape(-1,1)])
  2. initial_theta = np.zeros(X_b.shape[1])
  3. eta = 0.01
  4. theta = gradient_descent(X_b, y, initial_theta, eta)
  5. theta # array([4.02145786, 3.00706277])

封装模型

  1. # LinearRegression.py
  2. import numpy as np
  3. from .metrics import r2_score
  4. class LinearRegression:
  5. def __init__(self):
  6. """初始化Linear Regression模型"""
  7. self.coef_ = None
  8. self.intercept_ = None
  9. self._theta = None
  10. def fit_normal(self, X_train, y_train):
  11. """根据训练数据集X_train, y_train训练Linear Regression模型"""
  12. assert X_train.shape[0] == y_train.shape[0], \
  13. "the size of X_train must be equal to the size of y_train"
  14. X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  15. self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
  16. self.intercept_ = self._theta[0]
  17. self.coef_ = self._theta[1:]
  18. return self
  19. def fit_gd(self, X_train, y_train, eta=0.01, n_iters=1e4):
  20. """根据训练数据集X_train, y_train, 使用梯度下降法训练Linear Regression模型"""
  21. assert X_train.shape[0] == y_train.shape[0], \
  22. "the size of X_train must be equal to the size of y_train"
  23. def J(theta, X_b, y):
  24. try:
  25. return np.sum((y - X_b.dot(theta)) ** 2) / len(y)
  26. except:
  27. return float('inf')
  28. def dJ(theta, X_b, y):
  29. res = np.empty(len(theta))
  30. res[0] = np.sum(X_b.dot(theta) - y)
  31. for i in range(1, len(theta)):
  32. res[i] = (X_b.dot(theta) - y).dot(X_b[:, i])
  33. return res * 2 / len(X_b)
  34. def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
  35. theta = initial_theta
  36. cur_iter = 0
  37. while cur_iter < n_iters:
  38. gradient = dJ(theta, X_b, y)
  39. last_theta = theta
  40. theta = theta - eta * gradient
  41. if (abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):
  42. break
  43. cur_iter += 1
  44. return theta
  45. X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  46. initial_theta = np.zeros(X_b.shape[1])
  47. self._theta = gradient_descent(X_b, y_train, initial_theta, eta, n_iters)
  48. self.intercept_ = self._theta[0]
  49. self.coef_ = self._theta[1:]
  50. return self
  51. def predict(self, X_predict):
  52. """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
  53. assert self.intercept_ is not None and self.coef_ is not None, \
  54. "must fit before predict!"
  55. assert X_predict.shape[1] == len(self.coef_), \
  56. "the feature number of X_predict must be equal to X_train"
  57. X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
  58. return X_b.dot(self._theta)
  59. def score(self, X_test, y_test):
  60. """根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
  61. y_predict = self.predict(X_test)
  62. return r2_score(y_test, y_predict)
  63. def __repr__(self):
  64. return "LinearRegression()"

编码实现

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # 准备数据
  4. np.random.seed(666)
  5. x = 2 * np.random.random(size=100)
  6. y = x * 3. + 4. + np.random.normal(size=100)
  7. X = x.reshape(-1, 1) # 为了将数据转换为一列
  8. from playML.LinearRegression import LinearRegression
  9. lin_reg = LinearRegression()
  10. lin_reg.fit_gd(X, y)
  11. lin_reg.coef_ # array([ 3.00706277])
  12. lin_reg.intercept_ # 4.021457858204859

模型优化

提升性能:向量化代替for循环(计算机的算法优化)

  1. import numpy as np
  2. from sklearn import datasets
  3. # 准备数据
  4. boston = datasets.load_boston()
  5. X = boston.data
  6. y = boston.target
  7. X = X[y < 50.0]
  8. y = y[y < 50.0]
  9. from playML.model_selection import train_test_split
  10. X_train, X_test, y_train, y_test = train_test_split(X, y, seed=666)
  11. # 线性回归
  12. from playML.LinearRegression import LinearRegression
  13. lin_reg1 = LinearRegression()
  14. %time lin_reg1.fit_normal(X_train, y_train) # Wall time: 997 µs
  15. lin_reg1.score(X_test, y_test) # 0.812980260265854

使用梯度下降法

使用梯度下降法前进行数据归一化

  1. from sklearn.preprocessing import StandardScaler
  2. standardScaler = StandardScaler()
  3. standardScaler.fit(X_train)
  4. X_train_standard = standardScaler.transform(X_train)
  5. lin_reg3 = LinearRegression()
  6. %time lin_reg3.fit_gd(X_train_standard, y_train) # Wall time: 123 ms
  7. X_test_standard = standardScaler.transform(X_test)
  8. lin_reg3.score(X_test_standard, y_test) # 0.8129880620122235

梯度下降法的优势

  1. m = 1000
  2. n = 5000
  3. big_X = np.random.normal(size=(m, n))
  4. true_theta = np.random.uniform(0.0, 100.0, size=n+1)
  5. big_y = big_X.dot(true_theta[1:]) + true_theta[0] + np.random.normal(0., 10., size=m)
  6. big_reg1 = LinearRegression() # Wall time: 3.88 s
  7. %time big_reg1.fit_normal(big_X, big_y)
  8. big_reg2 = LinearRegression()
  9. %time big_reg2.fit_gd(big_X, big_y) # Wall time: 3.72 s

随机梯度下降法

优点:比原始梯度下降速度更快,但是没有那么精确。

模拟实现

image.png

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # 准备数据
  4. m = 100000
  5. x = np.random.normal(size=m)
  6. X = x.reshape(-1,1)
  7. y = 4.*x + 3. + np.random.normal(0, 3, size=m)
  8. plt.scatter(x, y)
  9. plt.show()

image.png

  1. def J(theta, X_b, y):
  2. try:
  3. return np.sum((y - X_b.dot(theta)) ** 2) / len(y)
  4. except:
  5. return float('inf')
  6. def dJ(theta, X_b, y):
  7. return X_b.T.dot(X_b.dot(theta) - y) * 2. / len(y)
  8. def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
  9. theta = initial_theta
  10. cur_iter = 0
  11. while cur_iter < n_iters:
  12. gradient = dJ(theta, X_b, y)
  13. last_theta = theta
  14. theta = theta - eta * gradient
  15. if (abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):
  16. break
  17. cur_iter += 1
  18. return theta
  19. %%time # Wall time: 884 ms
  20. X_b = np.hstack([np.ones((len(X), 1)), X])
  21. initial_theta = np.zeros(X_b.shape[1])
  22. eta = 0.01
  23. theta = gradient_descent(X_b, y, initial_theta, eta) # array([3.00317559, 4.01432714])
  1. def dJ_sgd(theta, X_b_i, y_i):
  2. return 2 * X_b_i.T.dot(X_b_i.dot(theta) - y_i)
  3. def sgd(X_b, y, initial_theta, n_iters):
  4. t0, t1 = 5, 50
  5. def learning_rate(t):
  6. return t0 / (t + t1)
  7. theta = initial_theta
  8. for cur_iter in range(n_iters):
  9. rand_i = np.random.randint(len(X_b))
  10. gradient = dJ_sgd(theta, X_b[rand_i], y[rand_i])
  11. theta = theta - learning_rate(cur_iter) * gradient
  12. return theta
  13. %%time # Wall time: 226 ms
  14. X_b = np.hstack([np.ones((len(X), 1)), X])
  15. initial_theta = np.zeros(X_b.shape[1])
  16. theta = sgd(X_b, y, initial_theta, n_iters=m//3)
  17. theta # array([ 3.04732375, 4.03214249])

封装SGD模型

  1. import numpy as np
  2. from .metrics import r2_score
  3. class LinearRegression:
  4. def __init__(self):
  5. """初始化Linear Regression模型"""
  6. self.coef_ = None
  7. self.intercept_ = None
  8. self._theta = None
  9. def fit_normal(self, X_train, y_train):
  10. """根据训练数据集X_train, y_train训练Linear Regression模型"""
  11. assert X_train.shape[0] == y_train.shape[0], \
  12. "the size of X_train must be equal to the size of y_train"
  13. X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  14. self._theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y_train)
  15. self.intercept_ = self._theta[0]
  16. self.coef_ = self._theta[1:]
  17. return self
  18. def fit_bgd(self, X_train, y_train, eta=0.01, n_iters=1e4):
  19. """根据训练数据集X_train, y_train, 使用梯度下降法训练Linear Regression模型"""
  20. assert X_train.shape[0] == y_train.shape[0], \
  21. "the size of X_train must be equal to the size of y_train"
  22. def J(theta, X_b, y):
  23. try:
  24. return np.sum((y - X_b.dot(theta)) ** 2) / len(y)
  25. except:
  26. return float('inf')
  27. def dJ(theta, X_b, y):
  28. return X_b.T.dot(X_b.dot(theta) - y) * 2. / len(y)
  29. def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):
  30. theta = initial_theta
  31. cur_iter = 0
  32. while cur_iter < n_iters:
  33. gradient = dJ(theta, X_b, y)
  34. last_theta = theta
  35. theta = theta - eta * gradient
  36. if (abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):
  37. break
  38. cur_iter += 1
  39. return theta
  40. X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  41. initial_theta = np.zeros(X_b.shape[1])
  42. self._theta = gradient_descent(X_b, y_train, initial_theta, eta, n_iters)
  43. self.intercept_ = self._theta[0]
  44. self.coef_ = self._theta[1:]
  45. return self
  46. def fit_sgd(self, X_train, y_train, n_iters=50, t0=5, t1=50):
  47. """根据训练数据集X_train, y_train, 使用梯度下降法训练Linear Regression模型"""
  48. assert X_train.shape[0] == y_train.shape[0], \
  49. "the size of X_train must be equal to the size of y_train"
  50. assert n_iters >= 1
  51. def dJ_sgd(theta, X_b_i, y_i):
  52. return X_b_i * (X_b_i.dot(theta) - y_i) * 2.
  53. def sgd(X_b, y, initial_theta, n_iters=5, t0=5, t1=50):
  54. def learning_rate(t):
  55. return t0 / (t + t1)
  56. theta = initial_theta
  57. m = len(X_b)
  58. for i_iter in range(n_iters):
  59. indexes = np.random.permutation(m)
  60. X_b_new = X_b[indexes,:]
  61. y_new = y[indexes]
  62. for i in range(m):
  63. gradient = dJ_sgd(theta, X_b_new[i], y_new[i])
  64. theta = theta - learning_rate(i_iter * m + i) * gradient
  65. return theta
  66. X_b = np.hstack([np.ones((len(X_train), 1)), X_train])
  67. initial_theta = np.random.randn(X_b.shape[1])
  68. self._theta = sgd(X_b, y_train, initial_theta, n_iters, t0, t1)
  69. self.intercept_ = self._theta[0]
  70. self.coef_ = self._theta[1:]
  71. return self
  72. def predict(self, X_predict):
  73. """给定待预测数据集X_predict,返回表示X_predict的结果向量"""
  74. assert self.intercept_ is not None and self.coef_ is not None, \
  75. "must fit before predict!"
  76. assert X_predict.shape[1] == len(self.coef_), \
  77. "the feature number of X_predict must be equal to X_train"
  78. X_b = np.hstack([np.ones((len(X_predict), 1)), X_predict])
  79. return X_b.dot(self._theta)
  80. def score(self, X_test, y_test):
  81. """根据测试数据集 X_test 和 y_test 确定当前模型的准确度"""
  82. y_predict = self.predict(X_test)
  83. return r2_score(y_test, y_predict)
  84. def __repr__(self):
  85. return "LinearRegression()"

模拟实现封装的SGD模型

模拟数据

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. m = 100000
  4. x = np.random.normal(size=m)
  5. X = x.reshape(-1,1)
  6. y = 4.*x + 3. + np.random.normal(0, 3, size=m)
  7. from playML.LinearRegression import LinearRegression
  8. lin_reg = LinearRegression()
  9. lin_reg.fit_bgd(X, y)
  10. print(lin_reg.intercept_, lin_reg.coef_) # 3.01644183437 [ 3.99995653]
  11. lin_reg = LinearRegression()
  12. lin_reg.fit_sgd(X, y, n_iters=2)
  13. print(lin_reg.intercept_, lin_reg.coef_) # 2.99558568395 [ 4.02610767]

数据归一化

梯度下降是一种搜索算法,过程中涉及各个特征,这就导致了每次的梯度值的大小受各个特征数值规模的影响,因此把数据归一化可以达到大大减小每次梯度值的效果,从而加速梯度下降。

真实实现封装的SGD模型

真实数据

  1. from sklearn import datasets
  2. # 准备数据
  3. boston = datasets.load_boston()
  4. X = boston.data
  5. y = boston.target
  6. X = X[y < 50.0]
  7. y = y[y < 50.0]
  8. # 数据预处理
  9. ## 数据分割:训练集、测试集
  10. from playML.model_selection import train_test_split
  11. X_train, X_test, y_train, y_test = train_test_split(X, y, seed=666)
  12. ## 数据归一化
  13. from sklearn.preprocessing import StandardScaler
  14. standardScaler = StandardScaler()
  15. standardScaler.fit(X_train)
  16. X_train_standard = standardScaler.transform(X_train)
  17. X_test_standard = standardScaler.transform(X_test)
  18. # 建模
  19. from playML.LinearRegression import LinearRegression
  20. lin_reg = LinearRegression()
  21. %time lin_reg.fit_sgd(X_train_standard, y_train, n_iters=2) # Wall time: 3.99 ms
  22. lin_reg.score(X_test_standard, y_test) # 0.78651716204682975
  23. ## 调整n_iters
  24. %time lin_reg.fit_sgd(X_train_standard, y_train, n_iters=50) # Wall time: 78.8 ms
  25. lin_reg.score(X_test_standard, y_test) # 0.8085728716573835
  26. %time lin_reg.fit_sgd(X_train_standard, y_train, n_iters=100) # Wall time: 157 ms
  27. lin_reg.score(X_test_standard, y_test) # 0.8129484613272351

scikit-learn中的SGD

  1. from sklearn.linear_model import SGDRegressor
  2. sgd_reg = SGDRegressor()
  3. %time sgd_reg.fit(X_train_standard, y_train) # Wall time: 185 ms
  4. sgd_reg.score(X_test_standard, y_test) # 0.8047845970157302
  5. sgd_reg = SGDRegressor(n_iter=50)
  6. %time sgd_reg.fit(X_train_standard, y_train) # Wall time: 1.99 ms
  7. sgd_reg.score(X_test_standard, y_test) # 0.8119907393187841