机器学习mlxtend_01
程序员文章站
2022-04-15 11:05:56
运行结果: ......
# -*- coding: utf-8 -*- """ created on wed oct 24 09:53:29 2018 @author: user """ import numpy as np import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import itertools from sklearn.linear_model import logisticregression from sklearn.svm import svc from sklearn.ensemble import randomforestclassifier from mlxtend.classifier import ensemblevoteclassifier from mlxtend.data import iris_data from mlxtend.plotting import plot_decision_regions clf1 = logisticregression(random_state = 0) clf2 = randomforestclassifier(random_state=0) clf3 = svc(random_state = 0, probability=true) eclf = ensemblevoteclassifier(clfs=[clf1, clf2, clf3], weights=[2, 1, 1], voting='soft') x, y =iris_data() x=x[:, [0, 2]] gs = gridspec.gridspec(2, 2) fig = plt.figure(figsize=(10, 8)) labels = ['logistic regression', 'random forest', 'rbf kernel svm', 'ensemble'] for clf, lab, grd in zip([clf1, clf2, clf3, eclf], labels, itertools.product([0, 1], repeat=2)): clf.fit(x, y) ax = plt.subplot(gs[grd[0], grd[1]]) fig = plot_decision_regions(x=x, y=y, clf=clf, legend=2) plt.title(lab) plt.show()
运行结果: