目录
利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测
相关文章
ML之LiR:利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测
ML之LiR:利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测实现
利用LiR线性回归算法(自定义目标函数MSE和优化器GD)对Boston房价数据集(两特征+归一化)进行回归预测
设计思路
输出结果
1. train_boston_data.shape (1460, 81) 2. Id MSSubClass MSZoning ... SaleType SaleCondition SalePrice 3. 0 1 60 RL ... WD Normal 208500 4. 1 2 20 RL ... WD Normal 181500 5. 2 3 60 RL ... WD Normal 223500 6. 3 4 70 RL ... WD Abnorml 140000 7. 4 5 60 RL ... WD Normal 250000 8. 9. [5 rows x 81 columns] 10. train_t.head() LotFrontage GarageArea SalePrice 11. 0 65.0 548 208500 12. 1 80.0 460 181500 13. 2 68.0 608 223500 14. 3 60.0 642 140000 15. 4 84.0 836 250000 16. after scale,train_t.head() LotFrontage GarageArea SalePrice 17. 0 0.207668 0.386460 0.276159 18. 1 0.255591 0.324401 0.240397 19. 2 0.217252 0.428773 0.296026 20. 3 0.191693 0.452750 0.185430 21. 4 0.268371 0.589563 0.331126 22. LotFrontage GarageArea 23. 0 0.207668 0.386460 24. 1 0.255591 0.324401 25. 2 0.217252 0.428773 26. 3 0.191693 0.452750 27. 4 0.268371 0.589563 28. Id MSSubClass LotFrontage ... MoSold YrSold SalePrice 29. Id 1.000000 0.011156 -0.010601 ... 0.021172 0.000712 -0.021917 30. MSSubClass 0.011156 1.000000 -0.386347 ... -0.013585 -0.021407 -0.084284 31. LotFrontage -0.010601 -0.386347 1.000000 ... 0.011200 0.007450 0.351799 32. LotArea -0.033226 -0.139781 0.426095 ... 0.001205 -0.014261 0.263843 33. OverallQual -0.028365 0.032628 0.251646 ... 0.070815 -0.027347 0.790982 34. OverallCond 0.012609 -0.059316 -0.059213 ... -0.003511 0.043950 -0.077856 35. YearBuilt -0.012713 0.027850 0.123349 ... 0.012398 -0.013618 0.522897 36. YearRemodAdd -0.021998 0.040581 0.088866 ... 0.021490 0.035743 0.507101 37. MasVnrArea -0.050298 0.022936 0.193458 ... -0.005965 -0.008201 0.477493 38. BsmtFinSF1 -0.005024 -0.069836 0.233633 ... -0.015727 0.014359 0.386420 39. BsmtFinSF2 -0.005968 -0.065649 0.049900 ... -0.015211 0.031706 -0.011378 40. BsmtUnfSF -0.007940 -0.140759 0.132644 ... 0.034888 -0.041258 0.214479 41. TotalBsmtSF -0.015415 -0.238518 0.392075 ... 0.013196 -0.014969 0.613581 42. 1stFlrSF 0.010496 -0.251758 0.457181 ... 0.031372 -0.013604 0.605852 43. 2ndFlrSF 0.005590 0.307886 0.080177 ... 0.035164 -0.028700 0.319334 44. LowQualFinSF -0.044230 0.046474 0.038469 ... -0.022174 -0.028921 -0.025606 45. GrLivArea 0.008273 0.074853 0.402797 ... 0.050240 -0.036526 0.708624 46. BsmtFullBath 0.002289 0.003491 0.100949 ... -0.025361 0.067049 0.227122 47. BsmtHalfBath -0.020155 -0.002333 -0.007234 ... 0.032873 -0.046524 -0.016844 48. FullBath 0.005587 0.131608 0.198769 ... 0.055872 -0.019669 0.560664 49. HalfBath 0.006784 0.177354 0.053532 ... -0.009050 -0.010269 0.284108 50. BedroomAbvGr 0.037719 -0.023438 0.263170 ... 0.046544 -0.036014 0.168213 51. KitchenAbvGr 0.002951 0.281721 -0.006069 ... 0.026589 0.031687 -0.135907 52. TotRmsAbvGrd 0.027239 0.040380 0.352096 ... 0.036907 -0.034516 0.533723 53. Fireplaces -0.019772 -0.045569 0.266639 ... 0.046357 -0.024096 0.466929 54. GarageYrBlt 0.000072 0.085072 0.070250 ... 0.005337 -0.001014 0.486362 55. GarageCars 0.016570 -0.040110 0.285691 ... 0.040522 -0.039117 0.640409 56. GarageArea 0.017634 -0.098672 0.344997 ... 0.027974 -0.027378 0.623431 57. WoodDeckSF -0.029643 -0.012579 0.088521 ... 0.021011 0.022270 0.324413 58. OpenPorchSF -0.000477 -0.006100 0.151972 ... 0.071255 -0.057619 0.315856 59. EnclosedPorch 0.002889 -0.012037 0.010700 ... -0.028887 -0.009916 -0.128578 60. 3SsnPorch -0.046635 -0.043825 0.070029 ... 0.029474 0.018645 0.044584 61. ScreenPorch 0.001330 -0.026030 0.041383 ... 0.023217 0.010694 0.111447 62. PoolArea 0.057044 0.008283 0.206167 ... -0.033737 -0.059689 0.092404 63. MiscVal -0.006242 -0.007683 0.003368 ... -0.006495 0.004906 -0.021190 64. MoSold 0.021172 -0.013585 0.011200 ... 1.000000 -0.145721 0.046432 65. YrSold 0.000712 -0.021407 0.007450 ... -0.145721 1.000000 -0.028923 66. SalePrice -0.021917 -0.084284 0.351799 ... 0.046432 -0.028923 1.000000 67. 68. [38 rows x 38 columns] 69. coef and intercept: [0.21627565 0.41024884] 0.0543428481373919 70. cost after log: -3.850369422061899 -4.52343070892457 71. best w1 and w2 after GD: 0.10003438525600654 0.30004957896248946
核心代码
1. LiR = linear_model.LinearRegression() 2. LiR.fit(X_train, y_train) 3. print('coef and intercept: ',LiR.coef_,LiR.intercept_) 4. 5. def CalCostByW(train_df,slope): 6. w1_lists=[];w2_lists=[];cost_lists=[] 7. for i in range (30): 8. for j in range(30): 9. w1= slope*i+0.1 10. w2= slope*j+0.3 11. w1_lists.append(w1); w2_lists.append(w2) 12. cost_lists.append(cost(train_df,train_df.LotFrontage,train_df.GarageArea,w1,w2)) 13. # print (cost(train_df)) 14. return w1_lists,w2_lists,cost_lists 15. w1_lists,w2_lists,cost_lists=CalCostByW(train_t,0.01)