from sklearn import datasets运行错误:ImportError: DLL load failed: 找不到指定的程序------解决办法

  • 在运行集成学习的多数投票分类代码时,出现错误
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

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:https://blog.csdn.net/a593651986/article/details/72178463

posted on 2019-12-02 16:51  星辰之衍  阅读(4240)  评论(1编辑  收藏  举报

导航