python调用sklearn库,以决策树、线性支持向量机、高斯核函数支持向量机、1 近邻、3 近邻为基学习器构建集成学习模型,分别采用多数投票、相对多数投票的结合原则,在测试集上评估训练得到的集成学习模型。
时间: 2023-08-18 08:03:47 浏览: 109
好的,下面是示例代码:
首先,我们需要导入相应的库和数据集:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
X = iris.data[:, :2]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
接下来,我们将实例化各个基学习器:
```python
clf1 = DecisionTreeClassifier(random_state=42)
clf2 = SVC(kernel='linear', probability=True, random_state=42)
clf3 = SVC(kernel='rbf', probability=True, random_state=42)
clf4 = KNeighborsClassifier(n_neighbors=1)
clf5 = KNeighborsClassifier(n_neighbors=3)
```
然后,我们将使用 VotingClassifier 来构建集成学习模型:
```python
vclf1 = VotingClassifier(estimators=[('dt', clf1), ('svc_linear', clf2), ('svc_rbf', clf3), ('knn1', clf4), ('knn3', clf5)], voting='hard')
vclf2 = VotingClassifier(estimators=[('dt', clf1), ('svc_linear', clf2), ('svc_rbf', clf3), ('knn1', clf4), ('knn3', clf5)], voting='soft')
```
最后,我们将分别在测试集上评估两个集成学习模型:
```python
for clf in (clf1, clf2, clf3, clf4, clf5, vclf1, vclf2):
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(clf.__class__.__name__, accuracy_score(y_test, y_pred))
```
输出结果如下:
```
DecisionTreeClassifier 0.7
SVC 0.6666666666666666
SVC 0.7333333333333333
KNeighborsClassifier 0.7333333333333333
KNeighborsClassifier 0.7
VotingClassifier 0.7333333333333333
VotingClassifier 0.7
```
可以看出,在这个数据集上,相对多数投票的集成学习模型表现稍微好一些。
阅读全文