【问题标题】:"Too many indices for array" error in make_scorer function in SklearnSklearn中make_scorer函数中的“数组索引过多”错误
【发布时间】:2020-06-17 09:10:03
【问题描述】:

目标:使用 brier score loss 训练一个使用 GridSearchCV 的随机森林算法

问题:使用 make_scorer 时,目标“y”的概率预测维度错误。

在查看this question 之后,我正在使用其建议的代理函数来使用经过 brier score loss 训练的 GridSearchCV。下面是一个设置示例:

from sklearn.model_selection import GridSearchCV
from sklearn.metrics import brier_score_loss,make_scorer
from sklearn.ensemble import RandomForestClassifier
import numpy as np

def ProbaScoreProxy(y_true, y_probs, class_idx, proxied_func, **kwargs):
    return proxied_func(y_true, y_probs[:, class_idx], **kwargs)

brier_scorer = make_scorer(ProbaScoreProxy, greater_is_better=False, \
                           needs_proba=True, class_idx=1, proxied_func=brier_score_loss)

X = np.random.randn(100,2)
y = (X[:,0]>0).astype(int)

random_forest = RandomForestClassifier(n_estimators=10)

random_forest.fit(X,y)

probs = random_forest.predict_proba(X)

现在将probsy 直接传递给brier_score_lossProbaScoreProxy 不会导致错误:

ProbaScoreProxy(y,probs,1,brier_score_loss)

输出:

0.0006

现在通过brier_scorer:

brier_scorer(random_forest,X,y)

输出:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-28-1474bb08e572> in <module>()
----> 1 brier_scorer(random_forest,X,y)

~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/_scorer.py in __call__(self, estimator, X, y_true, sample_weight)
    167                           stacklevel=2)
    168         return self._score(partial(_cached_call, None), estimator, X, y_true,
--> 169                            sample_weight=sample_weight)
    170 
    171     def _factory_args(self):

~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/_scorer.py in _score(self, method_caller, clf, X, y, sample_weight)
    258                                                  **self._kwargs)
    259         else:
--> 260             return self._sign * self._score_func(y, y_pred, **self._kwargs)
    261 
    262     def _factory_args(self):

<ipython-input-25-5321477444e1> in ProbaScoreProxy(y_true, y_probs, class_idx, proxied_func, **kwargs)
      5 
      6 def ProbaScoreProxy(y_true, y_probs, class_idx, proxied_func, **kwargs):
----> 7     return proxied_func(y_true, y_probs[:, class_idx], **kwargs)
      8 
      9 brier_scorer = make_scorer(ProbaScoreProxy, greater_is_better=False,                            needs_proba=True, class_idx=1, proxied_func=brier_score_loss)

IndexError: too many indices for array

所以make_scorer 似乎发生了一些事情来改变其概率输入的维度,但我似乎看不出问题出在哪里。

版本: -sklearn:'0.22.2.post1' - numpy:'1.18.1'

请注意,这里的y 是正确的维度 (1-d),您可以通过摆弄它来发现 y_probs 的维度正在传递给 ProbaScoreProxy 导致问题。

这只是最后一个问题中写得很糟糕的代码吗? 最终有什么方法可以让像 GridSearchCV 这样的东西接受 make_score 对象来训练 RF?

【问题讨论】:

    标签: python machine-learning scikit-learn probability


    【解决方案1】:

    目标:使用 brier score loss 训练一个使用 GridSearchCV 的随机森林算法

    为此,您可以直接在GridSearchCVscoring参数中使用字符串值'neg_brier_score'

    例如:

    gc = GridSearchCV(random_forest,
                      param_grid={"n_estimators":[5, 10]},
                      scoring="neg_brier_score")
    
    gc.fit(X, y)
    print(gc.scorer_) 
    # make_scorer(brier_score_loss, greater_is_better=False, needs_proba=True)
    

    【讨论】:

    • 这解决了这个目标,所以要勾选它。希望也许有人会出现并找出“make_scorer”功能为何在它工作的那一天起作用,因为它可能与将来的某人有关。
    • @jhutch 我们可以这样使用make_scorer 函数:gc = GridSearchCV(random_forest, param_grid={"n_estimators":[5, 10]}, scoring=s),其中s = make_scorer(brier_score_loss, greater_is_better=False, needs_proba=True)。尚未调试您的代码以检查您的方法为何不起作用。
    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 2021-07-20
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2016-12-18
    • 2015-09-09
    • 2017-08-13
    相关资源
    最近更新 更多