コード例 #1
0
def cohen_kappa_multiclass(actual, predicted):
    assert len(actual) == len(predicted)

    ac = np.array(actual, dtype=np.float32).reshape((len(actual), -1))
    pr = np.array(predicted, dtype=np.float32).reshape((len(predicted), -1))

    try:
        na = np.argwhere([not np.any(np.isnan(i)) for i in ac]).ravel()
    except:
        for i in ac:
            print(i)

        for i in ac:
            print(np.any(np.isnan(i)))

    if len(na) == 0:
        return np.nan

    aci = np.argmax(np.array(np.array(ac[na]), dtype=np.int32), axis=1)
    pri = np.argmax(np.array(np.array(pr[na]), dtype=np.float32), axis=1)

    # for i in range(len(aci)):
    #     print(aci[i],'--',pri[i],':',np.array(pr[na])[i])

    return kpa(aci, pri)
コード例 #2
0
def cohen_kappa(actual, predicted, split=0.5, average_over_labels=True):
    assert len(actual) == len(predicted)

    ac = np.array(actual, dtype=np.float32).reshape((len(actual), -1))
    pr = np.array(predicted, dtype=np.float32).reshape((len(predicted), -1))

    na = np.argwhere([not np.any(np.isnan(i)) for i in ac]).ravel()

    if len(na) == 0:
        return np.nan

    ac = np.array(np.array(ac[na]) > split, dtype=np.int32)
    pr = np.array(np.array(pr[na]) > split, dtype=np.int32)

    label_kpa = []
    if hasattr(split, '__iter__'):
        assert len(split) == ac.shape[-1]
    else:
        split = np.ones(ac.shape[1]) * split

    for i in range(ac.shape[-1]):
        label_kpa.append(
            kpa(np.array(np.array(ac[:, i]) > split[i], dtype=np.int32),
                np.array(np.array(pr[:, i]) > split[i], dtype=np.int32)))

    if average_over_labels:
        return np.nanmean(label_kpa)
    else:
        return label_kpa