首页 > 解决方案 > 从 SKLearn StackingClassifier 检索系数

问题描述

当尝试使用 SKlearn 从经过训练的 StackingClassifier 检索系数时,它说不coefs_存在。这意味着它认为它还没有被训练。我能够证明它已经通过在外部管道(获得正确的输出)和堆栈中的单个模型(说它尚未适合)上调用 predict 进行了训练。我怎样才能从这个模型中得到重量?

>>> a.named_steps['stackingclassifier'].named_estimators['mlp'].coefs_
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'MLPClassifier' object has no attribute 'coefs_'
>>> a.predict_proba([X])
array([[5.69909808e-05, 1.95316594e-05, 1.06791916e-08, 7.99719342e-06,
        4.16570282e-04, 9.78260604e-04, 7.43104846e-05, 1.15379667e-08,
        9.42032134e-06, 9.98103331e-01, 1.21851461e-04, 1.61161070e-04,
        1.97925333e-05, 3.46306792e-07, 3.04140522e-05]])
>>> a.named_steps['stackingclassifier'].named_estimators['mlp'].predict_proba([X])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jbiloki/anaconda3/envs/py36/lib/python3.6/site-packages/sklearn/neural_network/_multilayer_perceptron.py", line 1104, in predict_proba
    check_is_fitted(self)
  File "/home/jbiloki/anaconda3/envs/py36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 73, in inner_f
    return f(**kwargs)
  File "/home/jbiloki/anaconda3/envs/py36/lib/python3.6/site-packages/sklearn/utils/validation.py", line 1020, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This MLPClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.```

标签: pythonmachine-learningscikit-learn

解决方案


在进一步阅读源代码后,我发现clf.estimators由于某种原因使用是错误clf.estimators_的并且具有实际训练有素的估计器......我仍然可以为此使用一个原因,但要提取您可以使用的系数:

pipeline = joblib.load('yourmodel.model')
#Coefs for the first model, iterate over estimators_ for the rest
pipeline['stackingclassifier'].estimators_[0].coef_

推荐阅读