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

Python 如何从积极的未标记学习中计算roc auc分数?,python,numpy,machine-learning,scikit-learn,auc,Python,Numpy,Machine Learning,Scikit Learn,Auc,我正在尝试修改一些代码,以便从中进行积极的未标记学习,这与我的数据一起运行,但我还想计算我一直坚持的ROC AUC分数 我的数据分为阳性样本(data\U p)和未标记样本(data\U),每个样本只有2个特征/数据列,例如: #3 example rows: data_P [[-1.471, 5.766], [-1.672, 5.121], [-1.371, 4.619]] 我运行积极的未标记学习,如链接示例所示: known_labels_ratio

我正在尝试修改一些代码,以便从中进行积极的未标记学习,这与我的数据一起运行,但我还想计算我一直坚持的ROC AUC分数

我的数据分为阳性样本(
data\U p
)和未标记样本(
data\U
),每个样本只有2个特征/数据列,例如:

#3 example rows:
data_P

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

我运行积极的未标记学习,如链接示例所示:

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\u auc\u score()
,我一直在学习如何避免错误

目前我正在尝试:

y_pred = model.predict_proba(data_bootstrap)
roc_auc_score(train_label, y_pred)
ValueError: bad input shape (3, 2)
问题似乎是
y_pred
给出了一个包含两列的输出,如下所示:

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

我不确定为什么
y_pred
会这样结束,它是否给出了基于样本是否分为两组的概率?积极的还是其他的?我能不能过滤一下,每行选择得分最高的概率?或者我有没有办法改变这种或另一种计算AUCROC分数的方法?

y_pred
必须是一个数字,给出正类概率
p1
;目前,您的
y\u pred
由两种概率组成
[p0,p1]
(定义为
p0+p1=1.0

假设您的正类是class
1
(即
y\u 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\u pred
是一个Numpy数组(而不是Python列表),您可以将上面第一个命令中的列表替换为:

y_pred_pos  = y_pred[:,1]
y_pred_pos  = y_pred[:,1]