Machine learning path: Python regression tree decisiontreeregressor forecast Boston Rates

Source: Internet
Author: User

Use of the Python3 learning API

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

Code:

1  fromSklearn.datasetsImportLoad_boston2  fromSklearn.cross_validationImportTrain_test_split3  fromSklearn.preprocessingImportStandardscaler4  fromSklearn.treeImportDecisiontreeregressor5  fromSklearn.metricsImportR2_score, Mean_squared_error, Mean_absolute_error6 ImportNumPy as NP7 8 " "9 regression tree:Ten strictly speaking, the return tree is not a return . One The leaf node is a group of training data mean is not a continuous specific predicted value A      - solving the problem of characteristic nonlinearity - no requirement for standardization of features and uniform quantification the      - easy to be too complex to lose generalization ability - poor stability, subtle changes can lead to significant changes in tree structure -      + " " -  + #1 Preparing Data A #Read the Boston area rate information atBoston =Load_boston () - #View Data Description - #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)) # - #print ("Minimum rate:", Np.min (Boston.target)) # 5 in #print ("Average price:", Np.mean (Boston.target)) # 22.532806324110677 -  tox =Boston.data +y =Boston.target -  the #2 split training data and test data * #random sample 25% as test 75% as training $X_train, X_test, y_train, y_test = Train_test_split (x, Y, test_size=0.25, random_state=33)Panax Notoginseng  -  the #3 Standardized processing of training data and test data +Ss_x =Standardscaler () AX_train =ss_x.fit_transform (X_train) theX_test =ss_x.transform (x_test) +  -Ss_y =Standardscaler () $Y_train = Ss_y.fit_transform (Y_train.reshape (-1, 1)) $Y_test = Ss_y.transform (Y_test.reshape (-1, 1)) -  - #4 using regression trees for training and forecasting the #initializing K-Nearest neighbor regression model using average regression for prediction -DTR =decisiontreeregressor ()Wuyi #Training the Dtr.fit (X_train, Y_train) - #forecast Save Forecast results WuDtr_y_predict =dtr.predict (x_test) -  About #5 Model Evaluation $ Print("the default evaluation value for the regression tree is:", Dtr.score (X_test, y_test)) - Print("the r_squared values for the flat regression tree are:", R2_score (Y_test, dtr_y_predict)) - Print("the mean square error of the regression tree is:", Mean_squared_error (Ss_y.inverse_transform (y_test), - Ss_y.inverse_transform (dtr_y_predict))) A Print("the average absolute error of the regression tree is:", Mean_absolute_error (Ss_y.inverse_transform (y_test), + Ss_y.inverse_transform (dtr_y_predict))) the  - " " $ the default evaluation value for the regression tree is: 0.7066505912533438 the the r_squared value of the flat regression tree is: 0.7066505912533438 the the mean square error of the regression tree is: 22.746692913385836 the the average absolute error of the regression tree is: 3.08740157480315 the " "

Machine learning path: Python regression tree decisiontreeregressor forecast Boston Rates

Related Article

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.