在运行集成学习的多数投票分类代码时, 出现错误
- from sklearn import datasets
- from sklearn.model_selection import cross_val_score
- from sklearn.linear_model import LogisticRegression
- from sklearn.naive_bayes import GaussianNB
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.ensemble import VotingClassifier
- iris = datasets.load_iris()
- X, y = iris.data[:, 1:3], iris.target
- clf1 = LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=1)
- clf2 = RandomForestClassifier(n_estimators=50, random_state=1)
- clf3 = GaussianNB()
- ensemble_clf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb',clf3)], voting='hard')
- for clf, label in zip([clf1, clf2, clf3, ensemble_clf], ['Logistic Regression', 'Random Forest', 'naive Bayes', 'Ensemble']):
- scores = cross_val_score(clf, X, y, cv = 5, scoring='accuracy')
- # scoring 里面的有效参数有 ['accuracy', 'adjusted_rand_score', 'average_precision', 'f1', 'f1_macro', 'f1_micro', 'f1_samples',
- # 'f1_weighted', 'neg_log_loss', 'neg_mean_absolute_error', 'neg_mean_squared_error', 'neg_median_absolute_error',
- # 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted',
- # 'r2', 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'roc_auc']
- print('Accuracy: %0.2f (+/- %0.2f) ------- %s' %(scores.mean(), scores.std(), label))
运行结果
出错原因
如果遇到错误: ImportError: DLL load failed: 找不到指定的模块
出现错误原因: 安装包的来源问题, 也可以理解为包版本兼容问题, 有的包使用官方出版, 有的包使用 whl 文件安装
解决方案: 将所有包都统一来源, 要么全部使用官方出版的包, 要么全部使用 whl 里面的包, 问题就解决了
解决方法
(1) 先卸载原始版本 Scikit-Learn,Numpy 和 Scipy
- pip uninstall scikit-learn
- pip uninstall numpy
- pip uninstall scipy
(2) 安装自己电脑对应的版本
whl 包下载: https://www.lfd.uci.edu/~gohlke/pythonlibs/# 下载需要的 scikit-learn,numpy,scipy 三个 whl 文件
然后依次安装 numpy,scipy 和 scikit-learn 的轮子
- pip install numpy-1.16.5+mkl-cp36-cp36m-win_amd64.whl
- pip install scipy-1.3.3-cp36-cp36m-win_amd64.whl
- pip install scikit_learn-0.21.3-cp36-cp36m-win_amd64.whl
重新运行上述代码 成功运行
参考资料 1: https://www.cnblogs.com/hamish26/p/10985139.html
参考资料 2:
来源: http://www.bubuko.com/infodetail-3313248.html