【发布时间】:2021-04-10 11:17:15
【问题描述】:
我有一个关于投票分类器的简单问题。据我了解,投票分类器应该比那些构建它的个人预测器(群众的智慧)具有最高的准确性。这是代码
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
# import dataset
X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
# split the dataset into train/test sets
X_train, X_test, y_train, y_test = train_test_split(X, y)
rnd_clf = RandomForestClassifier(n_estimators=10, random_state=42)
log_clf = LogisticRegression(solver='liblinear', random_state=42)
svm_clf = SVC(gamma='auto', random_state=42)
voting_clf = VotingClassifier(
estimators= [('lr', log_clf), ('rf', rnd_clf), ('svc', svm_clf)],
voting='hard')
voting_clf = voting_clf.fit(X_train, y_train)
predictors_list= [log_clf, rnd_clf, svm_clf, voting_clf]
for clf in predictors_list:
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_pred, y_test)
print(clf.__class__.__name__, accuracy)
我得到的准确度如下:
逻辑回归 0.776 随机森林分类器 0.88 SVC 0.864 投票分类器 0.864
正如您在本次运行中看到的那样,随机森林预测器的准确度略高于 VotingClassifier!
对此有何解释?
非常感谢提前
费提
【问题讨论】:
标签: python classification voting