1 回答

TA贡献1824条经验 获得超8个赞
问题在于您的 OOP 实现。您正在改变传递给“ roc_table ”类的原始数据。
请尝试以下方法:
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
然后像这样实例化类并使用:
rt = roc_table(data)
threshold=0.5
rt.viewer(threshold)
threshold=0.75
rt.viewer(threshold)
这样原始数据就不会发生变异。
希望这可以帮助。
添加回答
举报