【问题标题】:Retrieving coefficients from SKLearn StackingClassifier从 SKLearn StackingClassifier 检索系数
【发布时间】:2020-08-31 21:37:15
【问题描述】:

当尝试使用 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.```

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    在进一步阅读源代码后,我发现由于某种原因使用 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_
    

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 2023-01-04
      • 2012-04-08
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多