Machine learning path: Python linear regression linearregression, stochastic parametric regression sgdregressor forecast Boston rates

Source: Internet
Author: User

Python3 Learning using the API

Linear regression, and stochastic parametric regression

Git:https://github.com/linyi0604/machinelearning

1  fromSklearn.datasetsImportLoad_boston2  fromSklearn.cross_validationImportTrain_test_split3  fromSklearn.preprocessingImportStandardscaler4  fromSklearn.linear_modelImportlinearregression, Sgdregressor5  fromSklearn.metricsImportR2_score, Mean_squared_error, Mean_absolute_error6 ImportNumPy as NP7 8 #1 Preparing Data9 #Read the Boston area rate informationTenBoston =Load_boston () One #View Data Description A #Print (Boston. DESCR) # A total of 506 Boston area rate information, each 13 numerical description and target rate - #view differences in Data - #print ("max rate:", Np.max (Boston.target)) # the #print ("Minimum rate:", Np.min (Boston.target)) # 5 - #print ("Average price:", Np.mean (Boston.target)) # 22.532806324110677 -  -x =Boston.data +y =Boston.target -  + #2 split training data and test data A #random sample 25% as test 75% as training atX_train, X_test, y_train, y_test = Train_test_split (x, Y, test_size=0.25, random_state=33) -  -  - #3 Standardized processing of training data and test data -Ss_x =Standardscaler () -X_train =ss_x.fit_transform (X_train) inX_test =ss_x.transform (x_test) -  toSs_y =Standardscaler () +Y_train = Ss_y.fit_transform (Y_train.reshape (-1, 1)) -Y_test = Ss_y.transform (Y_test.reshape (-1, 1)) the  * #4 Training and forecasting using two linear regression models $ #Initialize linearregression linear regression modelPanax NotoginsengLR =linearregression () - #Training the Lr.fit (X_train, Y_train) + #forecast Save Forecast results ALr_y_predict =lr.predict (x_test) the  + #initializing Sgdrregressor stochastic gradient regression model -SGDR =sgdregressor () $ #Training $ Sgdr.fit (X_train, Y_train) - #forecast Save Forecast results -Sgdr_y_predict =sgdr.predict (x_test) the  - #5 Model EvaluationWuyi #evaluation of the linear model theLr_score =Lr.score (x_test, y_test) - Print("the default evaluation value for linear is:", Lr_score) Wulr_r_squared =R2_score (y_test, lr_y_predict) - Print("the r_squared value of the linear is:", lr_r_squared) AboutLr_mse =Mean_squared_error (Ss_y.inverse_transform (y_test), Ss_y.inverse_transform (lr_y_predict)) $ Print("the mean square error of the linear is:", Lr_mse) -Lr_mae =Mean_absolute_error (Ss_y.inverse_transform (y_test), Ss_y.inverse_transform (lr_y_predict)) - Print("the average absolute error of the linear is:", Lr_mae) -  A #evaluation of the SGD model +Sgdr_score =Sgdr.score (x_test, y_test) the Print("the default evaluation value for SGD is:", Sgdr_score) -sgdr_r_squared =R2_score (y_test, sgdr_y_predict) $ Print("the r_squared value for SGD is:", sgdr_r_squared) theSgdr_mse =Mean_squared_error (Ss_y.inverse_transform (y_test), Ss_y.inverse_transform (sgdr_y_predict)) the Print("the mean square error of SGD is:", Sgdr_mse) theSgdr_mae =Mean_absolute_error (Ss_y.inverse_transform (y_test), Ss_y.inverse_transform (sgdr_y_predict)) the Print("the average absolute error of SGD is:", Sgdr_mae) -  in " " the the default evaluation value for linear is: 0.6763403830998702 the the r_squared value for linear is: 0.6763403830998701 About the mean square error of linear is: 25.09698569206773 the the average absolute error of the linear is: 3.5261239963985433 the  the the default evaluation value for SGD is: 0.659795654161198 + the r_squared value for SGD is: 0.659795654161198 - the mean square error of SGD is: 26.379885392159494 the the average absolute error of SGD is: 3.5094445431026413Bayi " "

Machine learning path: Python linear regression linearregression, stochastic parametric regression sgdregressor forecast Boston rates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.