Machine learning Path: The python K-nearest neighbor regression predicts Boston rates

Source: Internet
Author: User

Python3 Learning Machine Learning API

Two k-Nearest neighbor regression models were used to predict the mean K nearest neighbor regression and distance weighted K-nearest neighbor regression.

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

Code:

1  fromSklearn.datasetsImportLoad_boston2  fromSklearn.cross_validationImportTrain_test_split3  fromSklearn.preprocessingImportStandardscaler4  fromSklearn.neighborsImportKneighborsregressor5  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  * #study and prediction of 42 K-Nearest neighbor regression lines $ #initializing K-Nearest neighbor regression model using average regression for predictionPanax NotoginsengUNI_KNR = Kneighborsregressor (weights="Uniform") - #Training the Uni_knr.fit (X_train, Y_train) + #forecast Save Forecast results AUni_knr_y_predict =uni_knr.predict (x_test) the  + #Multi-initialized K-nearest neighbor regression model using distance weighted regression -DIS_KNR = Kneighborsregressor (weights="Distance") $ #Training $ Dis_knr.fit (X_train, Y_train) - #forecast Save Forecast results -Dis_knr_y_predict =dis_knr.predict (x_test) the  - #5 Model EvaluationWuyi #evaluation of the average K-nearest neighbor regression model the Print("the default evaluation value for the average K-nearest neighbor regression is:", Uni_knr.score (X_test, y_test)) - Print("the r_squared value of the average K-nearest neighbor regression is:", R2_score (Y_test, uni_knr_y_predict)) Wu Print("the mean square error of the average K nearest neighbor regression is:", Mean_squared_error (Ss_y.inverse_transform (y_test), - Ss_y.inverse_transform (uni_knr_y_predict))) About Print("the average absolute error of the average K-nearest neighbor regression is:", Mean_absolute_error (Ss_y.inverse_transform (y_test), $ Ss_y.inverse_transform (uni_knr_y_predict))) - #evaluation of distance weighted K-nearest neighbor regression model - Print("the default evaluation value for distance weighted K-nearest neighbor regression is:", Dis_knr.score (X_test, y_test)) - Print("the r_squared value of the distance weighted K-nearest neighbor regression is:", R2_score (Y_test, dis_knr_y_predict)) A Print("the mean square error of the distance weighted K nearest neighbor regression is:", Mean_squared_error (Ss_y.inverse_transform (y_test), + Ss_y.inverse_transform (dis_knr_y_predict))) the Print("the average absolute error of the distance weighted K-nearest neighbor regression is:", Mean_absolute_error (Ss_y.inverse_transform (y_test), - Ss_y.inverse_transform (dis_knr_y_predict))) $  the " " the the default evaluation value for the average K-nearest neighbor regression is: 0.6903454564606561 the the r_squared value of the average K-nearest neighbor regression is: 0.6903454564606561 the Mean square error of average K nearest neighbor regression is: 24.01101417322835 - the average absolute error of the average K-nearest neighbor regression is: 2.9680314960629928 in the default evaluation value for distance weighted K-nearest neighbor regression is: 0.7197589970156353 the the r_squared value of the distance weighted K-nearest neighbor regression is: 0.7197589970156353 the the mean square error of the distance weighted K nearest neighbor regression is: 21.730250160926044 About the average absolute error of the distance weighted K-nearest neighbor regression is: 2.8050568785108005 the " "

Machine learning Path: The python K-nearest neighbor regression predicts 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.