ada_train.py 770 B

123456789101112131415161718192021
  1. # -*- encoding:utf-8 -*-
  2. from sklearn.tree import DecisionTreeClassifier
  3. from sklearn.ensemble import RandomForestClassifier
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.ensemble import AdaBoostClassifier
  6. from sklearn.datasets import load_wine
  7. wine = load_wine()
  8. Xtrain, Xtest, Ytrain, Ytest = train_test_split(wine.data,wine.target,test_size=0.3)
  9. bdt1 = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),n_estimators=2)
  10. bdt1.fit(Xtrain, Ytrain)
  11. print("bdt1",bdt1.score(Xtest,Ytest))
  12. #分类器越多效果越好
  13. bdt2 = AdaBoostClassifier(DecisionTreeClassifier(max_depth=2, min_samples_split=20, min_samples_leaf=5),n_estimators=30)
  14. bdt2.fit(Xtrain, Ytrain)
  15. print("bdt2",bdt2.score(Xtest,Ytest))