赞
踩
分类器统计每个子分类器的预测类别数,再用「多数投票」原则得到最终预测。
回归器计算每个子回归器的预测平均值。
最常用的 Ensemble 估计器排列如下:
AdaBoostClassifier
: 逐步提升分类器
AdaBoostRegressor
: 逐步提升回归器
BaggingClassifier
: 装袋分类器
BaggingRegressor
: 装袋回归器
GradientBoostingClassifier
: 梯度提升分类器
GradientBoostingRegressor
: 梯度提升回归器
RandomForestClassifier
: 随机森林分类器
RandomForestRegressor
: 随机森林回归器
VotingClassifier
: 投票分类器
VotingRegressor
: 投票回归器
1、含同质估计器 RandomForestClassifier
2、含异质估计器 VotingClassifier
RandomForestClassifier 测试:
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- from sklearn import metrics
- from sklearn.ensemble import RandomForestClassifier
-
- iris = load_iris()#加载数据
- X_train, X_test, y_train, y_test= train_test_split( iris['data'], iris['target'], test_size=0.2 )#按照80:20的比例切分数据
-
- RF = RandomForestClassifier( n_estimators=4, max_depth=5 )#有4颗树组成,每棵树最大深度为5
- RF.fit( X_train, y_train )#拟合
-
- print( RF.n_estimators )#元估计器有多少估计器(随即森林有多少树)
-
- print ( "RF - Accuracy (Train): %.4g" % metrics.accuracy_score(y_train, RF.predict(X_train)) )
- print ( "RF - Accuracy (Test): %.4g" % metrics.accuracy_score(y_test, RF.predict(X_test)))
测试结果:
- F:\开发工具\pythonProject\tools\venv\Scripts\python.exe F:/开发工具/pythonProject/tools/python的sklear学习/sklearndudu.py
- 4
- RF - Accuracy (Train): 0.975
- RF - Accuracy (Test): 0.9667
-
- Process finished with exit code 0
VotingClassifier 测试:
- from sklearn.linear_model import LogisticRegression #逻辑回归分类器
- from sklearn.naive_bayes import GaussianNB #高斯贝叶斯分类器
- from sklearn.ensemble import RandomForestClassifier #随机森林分类器
- from sklearn.ensemble import VotingClassifier #投片分类器
- from sklearn.model_selection import train_test_split #拆分数据类
- from sklearn.datasets import load_iris #加载数据
- from sklearn import metrics#metrics 来计算各种性能指标
- iris = load_iris()#加载数据
- X_train, X_test, y_train, y_test= train_test_split( iris['data'], iris['target'], test_size=0.2 )#按照80:20的比例拆分数据
-
- LR=LogisticRegression(solver="lbfgs",multi_class="multinomial")#逻辑回归分类器
- RF=RandomForestClassifier(n_estimators=5)#随即森林分类
- GNB=GaussianNB()#高斯贝叶斯
-
- Ensemble=VotingClassifier(estimators=[("lr",LR),("rf",RF),("gnb",GNB)],voting="hard")#里面装在三个分类器
-
- Ensemble.fit(X_train,y_train)#拟合训练数据
-
- print( len(Ensemble.estimators_) )#元估计器的个数
- #分别查看各个数据分类器的拟合
- LR.fit( X_train, y_train )
- RF.fit( X_train, y_train )
- GNB.fit( X_train, y_train )
-
- #分别查看预测得分情况
- print("LR:",metrics.accuracy_score(y_train,LR.predict(X_train)))
- print("RF:",metrics.accuracy_score(y_train,RF.predict(X_train)))
- print("GNB:",metrics.accuracy_score(y_train,GNB.predict(X_train)))
- print("Ensemble:",metrics.accuracy_score(y_train,Ensemble.predict(X_train)))
-
- print("-----------------------------------------------------------------\n")
- #测试数据的拟合预测情况
- print("LR:",metrics.accuracy_score(y_test,LR.predict(X_test)))
- print("RF:",metrics.accuracy_score(y_test,RF.predict(X_test)))
- print("GNB:",metrics.accuracy_score(y_test,GNB.predict(X_test)))
- print("Ensemble:",metrics.accuracy_score(y_test,Ensemble.predict(X_test)))
测试结果:
- F:\开发工具\pythonProject\tools\venv\Scripts\python.exe F:/开发工具/pythonProject/tools/python的sklear学习/sklearnfff.py
- 3
- LR: 0.9916666666666667
- RF: 1.0
- GNB: 0.95
- Ensemble: 0.9833333333333333
- -----------------------------------------------------------------
-
- LR: 0.9
- RF: 0.9333333333333333
- GNB: 0.9333333333333333
- Ensemble: 0.9
-
- Process finished with exit code 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。