python - 从 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.```
解决方案
在进一步阅读源代码后,我发现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_
推荐阅读
- spring-boot - Recaptcha v3 响应问题
- webpack - 在 Electron App 中启动 DevTools 后 HMR 中断
- c++ - C++二维数组的访问效率
- reactjs - 停止从 RTKQ 接收流式更新的正确方法是什么?
- reactjs - useQuery 和 useState
- elm - 通过在 elm 中将它们作为函数的参数输入来访问列表的列
- android - 从视图中的 url 加载图像
- typo3 - 如何将 TYPO3 的默认 RTE CK Editor 预设与 RTE CK 编辑器的完整预设交换?
- node.js - 未找到模块:导入 Clarifai API 后无法解析“http2”
- react-native - Stanza Jingle 无法在 React Native 中使用 XMPP 服务器进行 webRTC 信令