Decision Tree (branch depth of decision tree and important feature detection)

Source: Internet
Author: User

ImportMatplotlib.pyplot as Plt fromSklearnImportDatasetsImportNumPy as NP fromSklearn.treeImportDecisiontreeclassifiern_features= 200X, y= Datasets.make_classification (200,n_informative=5,)#The back of P, is the ratio of positive and negativeTraining = Np.random.choice ([True, False], p=[.75,. 25],size=Len (y)) C=0 forXinchTraining:if(x = =True): C= C+1Print(c,c/750) accuracies= [] forXinchNp.arange (1, n_features+1): DT= Decisiontreeclassifier (max_depth=x) Dt.fit (x[training], y[training]) Preds= Dt.predict (x[~Training]) Accuracies.append ((Preds= = y[~training]). Mean ()) F, Ax= Plt.subplots (figsize= (7, 5)) Ax.plot (range (1, n_features+1), accuracies, color='k') Ax.set_title ("decision Tree Accuracy") Ax.set_ylabel ("% Correct") Ax.set_xlabel ("Max Depth") f.show () N= 15F, Ax= Plt.subplots (figsize= (7, 5)) Ax.plot (range (1, n_features+1) [: n], accuracies[:n], color='k') Ax.set_title ("decision Tree Accuracy") Ax.set_ylabel ("% Correct") Ax.set_xlabel ("Max Depth") f.show ()" "The old version has no this parameter, this parameter is very good, can check important features dt_ci = Decisiontreeclassifier (compute_importances=true) dt.fit (X, y) ne0 = dt.feature_ Importances_! = 0y_comp = Dt.feature_importances_[ne0]x_comp = Np.arange (len (Dt.feature_importances_)) [ne0]f, ax = Plt.subplots (figsize= (7, 5)) Ax.bar (X_comp, Y_comp) f.show ()" "

Decision Tree (branch depth of decision tree and important feature detection)

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.