day03_machine_learning.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from sklearn.datasets import load_boston
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.preprocessing import StandardScaler
  4. from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
  5. from sklearn.metrics import mean_squared_error
  6. from sklearn.externals import joblib
  7. def linear1():
  8. """
  9. 正规方程的优化方法对波士顿房价进行预测
  10. :return:
  11. """
  12. # 1)获取数据
  13. boston = load_boston()
  14. # 2)划分数据集
  15. x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)
  16. # 3)标准化
  17. transfer = StandardScaler()
  18. x_train = transfer.fit_transform(x_train)
  19. x_test = transfer.transform(x_test)
  20. # 4)预估器
  21. estimator = LinearRegression()
  22. estimator.fit(x_train, y_train)
  23. # 5)得出模型
  24. print("正规方程-权重系数为:\n", estimator.coef_)
  25. print("正规方程-偏置为:\n", estimator.intercept_)
  26. # 6)模型评估
  27. y_predict = estimator.predict(x_test)
  28. print("预测房价:\n", y_predict)
  29. error = mean_squared_error(y_test, y_predict)
  30. print("正规方程-均方误差为:\n", error)
  31. return None
  32. def linear2():
  33. """
  34. 梯度下降的优化方法对波士顿房价进行预测
  35. :return:
  36. """
  37. # 1)获取数据
  38. boston = load_boston()
  39. print("特征数量:\n", boston.data.shape)
  40. # 2)划分数据集
  41. x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)
  42. # 3)标准化
  43. transfer = StandardScaler()
  44. x_train = transfer.fit_transform(x_train)
  45. x_test = transfer.transform(x_test)
  46. # 4)预估器
  47. estimator = SGDRegressor(learning_rate="constant", eta0=0.01, max_iter=10000, penalty="l1")
  48. estimator.fit(x_train, y_train)
  49. # 5)得出模型
  50. print("梯度下降-权重系数为:\n", estimator.coef_)
  51. print("梯度下降-偏置为:\n", estimator.intercept_)
  52. # 6)模型评估
  53. y_predict = estimator.predict(x_test)
  54. print("预测房价:\n", y_predict)
  55. error = mean_squared_error(y_test, y_predict)
  56. print("梯度下降-均方误差为:\n", error)
  57. return None
  58. def linear3():
  59. """
  60. 岭回归对波士顿房价进行预测
  61. :return:
  62. """
  63. # 1)获取数据
  64. boston = load_boston()
  65. print("特征数量:\n", boston.data.shape)
  66. # 2)划分数据集
  67. x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)
  68. # 3)标准化
  69. transfer = StandardScaler()
  70. x_train = transfer.fit_transform(x_train)
  71. x_test = transfer.transform(x_test)
  72. # 4)预估器
  73. # estimator = Ridge(alpha=0.5, max_iter=10000)
  74. # estimator.fit(x_train, y_train)
  75. # 保存模型
  76. # joblib.dump(estimator, "my_ridge.pkl")
  77. # 加载模型
  78. estimator = joblib.load("my_ridge.pkl")
  79. # 5)得出模型
  80. print("岭回归-权重系数为:\n", estimator.coef_)
  81. print("岭回归-偏置为:\n", estimator.intercept_)
  82. # 6)模型评估
  83. y_predict = estimator.predict(x_test)
  84. print("预测房价:\n", y_predict)
  85. error = mean_squared_error(y_test, y_predict)
  86. print("岭回归-均方误差为:\n", error)
  87. return None
  88. if __name__ == "__main__":
  89. # 代码1:正规方程的优化方法对波士顿房价进行预测
  90. linear1()
  91. # 代码2:梯度下降的优化方法对波士顿房价进行预测
  92. linear2()
  93. # 代码3:岭回归对波士顿房价进行预测
  94. linear3()