3

Python パッケージstatsmodelsLogitResults.pred_tableは、任意の任意のしきい値tについてLogit、フォームのモデルについて「混同行列」を取得するために便利に使用できます。

mod_fit = sm.Logit.from_formula('Y ~ a + b + c', train).fit() 
...
mod_fit.pred_table(t) 
#Conceptually: pred_table(t, predicted=mod_fit.predict(train), observed=train.Y)

テスト データの同等の情報を取得する方法はありますか? たとえば、

pred = mod_fit.predict(test)

に相当するものを取得するにはどうすればよいですか

mod_fit.pred_table(t, predicted=pred, observed=test.Y)

これを行う方法はありますか (たとえば、 andからインスタンスstatsmodelsを構築する方法)、または「手動で」行う必要がありますか? もしそうなら、どのように>LogitResultspredtrain.Y

4

2 に答える 2

4

これは良いアイデアで、簡単に追加できます。それについてgithubの問題を投稿できますか? 次のコードでこれを行うことができます

import numpy as np
pred = np.array(mod_fit.predict(test) > threshold, dtype=float)
table = np.histogram2d(test.Y, pred, bins=2)[0]
于 2014-03-20T03:56:38.707 に答える
0

を使用した別の方法を次に示しbincountます。

from __future__ import division
import numpy as np

def confusionmatrix( true, predicted, classnames="0 1", verbose=1 ):
    """ true aka y, observed class ids: ints [0 .. nclass) or bools
        predicted aka yhat: ints or bools, e.g. (probs > threshold)
    -> e.g.
        confusion matrix, true down, predicted across:
        [[0 2]  -- true 0, pred 0 1
         [7 1]  -- true 1, pred 0 1
    """
    true = np.asarray( true, dtype=int )
    pred = np.asarray( predicted, dtype=int )
    ntrue, npred = true.max() + 1, pred.max() + 1
    counts = np.bincount( npred * true + pred, minlength = ntrue * npred )  # 00 01 10 11
    confus = counts.reshape(( ntrue, npred ))
    if verbose:
        print "true counts %s:      %s" % (classnames, np.bincount(true))
        print "predicted counts %s: %s" % (classnames, np.bincount(pred))
        print "confusion matrix, true down, predicted across:\n", confus
    return confus

#...............................................................................
if __name__ == "__main__":
    n = 10
    np.random.seed( 7 )
    y = np.random.randint( 0, 2, n )
    p = np.random.randint( 0, 2, n )
    print "true:", y
    print "pred:", p
    confusionmatrix( y, p )
于 2015-08-03T17:15:23.167 に答える