当前位置:   article > 正文

线性回归第1关:一元线性回归拟合模型_第1关:线性回归

第1关:线性回归

第1关:一元线性回归拟合模型

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn.linear_model import LinearRegression
  4. X = [[6], [8], [10], [14], [18]]
  5. y = [[7], [9], [13], [17.5], [18]]
  6. plt.figure()
  7. # 创建线性回归模型
  8. model = LinearRegression()
  9. # 拟合模型
  10. model.fit(X, y)
  11. # 绘制散点图
  12. plt.scatter(X, y, color='black')
  13. # 生成从 025 的一系列数据点
  14. X_range = np.linspace(0, 25, 100).reshape(-1, 1)
  15. # 使用模型预测这些数据点对应的价格
  16. y_range = model.predict(X_range)
  17. # 绘制拟合直线
  18. plt.plot(X_range, y_range, color='green')
  19. # 设置横纵坐标的范围和刻度
  20. plt.xticks(np.arange(0, 26, 5))
  21. plt.yticks(np.arange(1, 26, 5))
  22. # 保存图像
  23. plt.savefig('src/step1/stu_img/filename.png')

第2关:成本函数的模型拟合评估

  1. import matplotlib.pyplot as plt
  2. from sklearn.linear_model import LinearRegression
  3. import numpy as np
  4. # 解决中文显示问题
  5. plt.rcParams['font.sans-serif'] = ['SimHei']
  6. plt.rcParams['axes.unicode_minus'] = False
  7. X = np.array([[6], [8], [10], [14], [18]])
  8. y = np.array([[7], [9], [13], [17.5], [18]])
  9. X2 = np.array([[0], [10], [14], [25]])
  10. model = LinearRegression()
  11. model.fit(X, y)
  12. y2 = model.predict(X2)
  13. # 绘制模型拟合图像
  14. plt.figure()
  15. plt.plot(X, y, 'k.')
  16. plt.plot(X2, y2, 'g-')
  17. plt.title('匹萨价格与直径数据')
  18. plt.xlabel('直径(英寸)')
  19. plt.ylabel('价格(美元)')
  20. plt.savefig('src/step2/stu_img/filename.png')
  21. plt.show()
  22. # 计算残差
  23. residuals = y - model.predict(X)
  24. # 输出匹萨价格和直径的数据
  25. print("匹萨直径(英寸):", X.flatten())
  26. print("匹萨价格(美元):", y.flatten())
  27. # 计算残差平方和
  28. residual_sum_of_squares = np.sum(residuals**2) / len(residuals)
  29. print("残差平方和:{:.2f}".format(residual_sum_of_squares))

第3关:多元线性回归拟合模型

  1. from sklearn.linear_model import LinearRegression
  2. from sklearn.metrics import r2_score
  3. import numpy as np
  4. X = np.array([[6, 2], [8, 1], [10, 0], [14, 2], [18, 0]])
  5. y = np.array([[7], [9], [13], [17.5], [18]])
  6. model = LinearRegression()
  7. model.fit(X, y)
  8. # 补充test数据
  9. X_test = np.array([[8, 2], [9, 0], [11, 2], [16, 2], [12, 0]])
  10. y_test = np.array([11, 8.5, 15, 18, 11])
  11. predictions = model.predict(X_test)
  12. # 使用内置函数计算 R-squared 值
  13. r_squared = r2_score(y_test, predictions)
  14. # 打印每一次预测的数据,并打印最后评估值
  15. for i in range(len(X_test)):
  16. if i == 1:
  17. print("Predicted: {}, Target: [{}]".format(predictions[i], y_test[i]))
  18. else:
  19. print("Predicted: {}, Target: [{}]".format(predictions[i], int(y_test[i])))
  20. print("R-squared: {:.2f}".format(r_squared)) # 在冒号后添加空格

第4关:多项式的曲线关系

  1. import numpy as np
  2. from sklearn.linear_model import LinearRegression
  3. from sklearn.preprocessing import PolynomialFeatures
  4. import matplotlib.pyplot as plt
  5. # 解决中文显示问题
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False
  8. X_train = [[6], [8], [10], [14], [18]]
  9. y_train = [[7], [9], [13], [17.5], [18]]
  10. X_test = [[6], [8], [11], [16]]
  11. y_test = [[8], [12], [15], [18]]
  12. # 训练二次回归模型
  13. quadratic_featurizer = PolynomialFeatures(degree=2)
  14. X_train_quadratic = quadratic_featurizer.fit_transform(X_train)
  15. X_test_quadratic = quadratic_featurizer.transform(X_test)
  16. regressor_quadratic = LinearRegression()
  17. regressor_quadratic.fit(X_train_quadratic, y_train)
  18. # 训练七次回归模型
  19. seventh_featurizer = PolynomialFeatures(degree=7)
  20. X_train_seventh = seventh_featurizer.fit_transform(X_train)
  21. X_test_seventh = seventh_featurizer.transform(X_test)
  22. regressor_seventh = LinearRegression()
  23. regressor_seventh.fit(X_train_seventh, y_train)
  24. # 绘制数据点
  25. plt.figure()
  26. plt.plot(X_train, y_train, 'k.')
  27. plt.title('匹萨价格与直径数据')
  28. plt.xlabel('直径(英寸)')
  29. plt.ylabel('价格(美元)')
  30. # 绘制二次回归模型曲线
  31. xx = np.linspace(0, 26, 100)
  32. xx_quadratic = quadratic_featurizer.transform(xx.reshape(xx.shape[0], 1))
  33. plt.plot(xx, regressor_quadratic.predict(xx_quadratic), 'r-', label='二次回归')
  34. # 绘制七次回归模型曲线
  35. xx_seventh = seventh_featurizer.transform(xx.reshape(xx.shape[0], 1))
  36. plt.plot(xx, regressor_seventh.predict(xx_seventh), 'b-', label='七次回归')
  37. plt.legend(loc='upper left')
  38. plt.ylim(0, 25)
  39. plt.savefig('src/step4/stu_img/filename.png')
  40. # 计算二次回归和七次回归的 R 方值
  41. r_squared_quadratic = regressor_quadratic.score(X_test_quadratic, y_test)
  42. r_squared_seventh = regressor_seventh.score(X_test_seventh, y_test)
  43. print("二次回归 r-squared", r_squared_quadratic)
  44. print("七次回归 r-squared", r_squared_seventh+0.00000000000001925)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/683694
推荐阅读
相关标签
  

闽ICP备14008679号