如何从积极的未标记学习中计算 roc auc 分数?

How to calculate roc auc score from positive unlabeled learning?

我正在尝试调整一些代码以从 this example 中进行积极的未标记学习,其中 运行 与我的数据有关,但我还想计算我遇到问题的 ROC AUC 分数上。

我的数据分为正样本(data_P)和未标记样本(data_U),每个只有2features/columns的数据如:

#3 example rows:
data_P

[[-1.471,  5.766],
       [-1.672,  5.121],
       [-1.371,  4.619]]

#3 example rows:
data_U

[[1.23,  6.26],
       [-5.72,  4.1213],
       [-3.1,  7.129]]

我 运行 链接示例中的正向未标记学习:

known_labels_ratio = 0.5

NP = data_P.shape[0]
NU = data_U.shape[0]

T = 1000
K = NP
train_label = np.zeros(shape=(NP+K,))
train_label[:NP] = 1.0
n_oob = np.zeros(shape=(NU,))
f_oob = np.zeros(shape=(NU, 2))
for i in range(T):
    # Bootstrap resample
    bootstrap_sample = np.random.choice(np.arange(NU), replace=True, size=K)
    # Positive set + bootstrapped unlabeled set
    data_bootstrap = np.concatenate((data_P, data_U[bootstrap_sample, :]), axis=0)
    # Train model
      model = DecisionTreeClassifier(max_depth=None, max_features=None, 
                                   criterion='gini', class_weight='balanced')
    model.fit(data_bootstrap, train_label)
    # Index for the out of the bag (oob) samples
    idx_oob = sorted(set(range(NU)) - set(np.unique(bootstrap_sample)))
    # Transductive learning of oob samples
    f_oob[idx_oob] += model.predict_proba(data_U[idx_oob])
    n_oob[idx_oob] += 1
    
predict_proba = f_oob[:, 1]/n_oob

这一切都 运行 没问题,但我想要的是 运行 roc_auc_score(),我正在思考如何做到不出错。

目前我正在尝试:

y_pred = model.predict_proba(data_bootstrap)
roc_auc_score(train_label, y_pred)
ValueError: bad input shape (3, 2)

问题似乎是 y_pred 给出了一个包含 2 列的输出,看起来像:

y_pred
array([[0.00554287, 0.9944571 ],
       [0.0732314 , 0.9267686 ],
       [0.16861796, 0.83138204]])

我不确定为什么 y_pred 会变成这样,它是根据样本是否属于 2 组给出概率吗?积极的还是其他本质上的?我可以将这些过滤为每行 select 得分最高的概率吗?或者有没有办法让我改变这个或其他方法来计算 AUCROC 分数?

y_pred必须是单数,给出正数classp1的概率;目前你的 y_pred 包含两个概率 [p0, p1] (根据定义 p0+p1=1.0)。

假设你的正class是class1(即y_pred中每个数组的第二个元素),你应该做的是:

y_pred_pos = [y_pred[i, 1] for i in range(len(y_pred))]
y_pred_pos # inspect
# [0.9944571, 0.9267686, 0.83138204]

roc_auc_score(train_label, y_pred_pos)

如果您的 y_pred 是一个 Numpy 数组(而不是 Python 列表),您可以将上面第一个命令中的列表理解替换为:

y_pred_pos  = y_pred[:,1]