1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題在于您的 OOP 實(shí)現(xiàn)。您正在改變傳遞給“ roc_table ”類的原始數(shù)據(jù)。
請嘗試以下方法:
class roc_table:
def __init__(self, data):
self.org_data = data
def viewer(self, threshold):
#make a copy of initial data
data = self.org_data.copy()
#count columns in dataframe
count_algo = len(data.columns)
for i in data.iloc[:,1:]:
data['predicted_{}'.format(i)] = (data[i] >= threshold).astype('int')
rock_table = {
"AUC":[round(roc_auc_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]],
"Accuracy":[round(accuracy_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]],
"Kappa":[round(cohen_kappa_score(data.actual_label, data[i]),2)for i in data.iloc[:,count_algo:]],
"Sensitivity (Recall)": [round(recall_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]],
"Specificity": [round(accuracy_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]],
"Precision": [round(precision_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]],
"F1": [round(f1_score(data.actual_label, data[i]),2) for i in data.iloc[:,count_algo:]]
}
rock_table = pd.DataFrame.from_dict(rock_table, orient = 'index').reset_index()
col = ['metrics']
col.extend([x for x in data.iloc[:,count_algo:]])
rock_table.columns = col
return rock_table
然后像這樣實(shí)例化類并使用:
rt = roc_table(data)
threshold=0.5
rt.viewer(threshold)
threshold=0.75
rt.viewer(threshold)
這樣原始數(shù)據(jù)就不會發(fā)生變異。
希望這可以幫助。
添加回答
舉報(bào)