为了账号安全,请及时绑定邮箱和手机立即绑定

循环实例无限附加字符串

循环实例无限附加字符串

阿晨1998 2022-07-12 17:57:41
我试图在 OOP 中编写一个表,该表将返回一些算法的统计指标,并在 Python 中的 pandas DataFrame 中显示。我遇到的问题是,对于每个实例,列的名称都会在正在创建的 DataFrame 中附加一个“预测”的附加字符串(最后的示例)。我的代码:from sklearn.metrics import roc_auc_score, accuracy_score, cohen_kappa_score, recall_score, accuracy_score, precision_score, f1_scorefrom sklearn import metrics#----------------------------------------------------------------#        ####################### ROC metrics table #########################----------------------------------------------------------------# class roc_table:    def __init__(self, data):        self.data = data    def viewer():        #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这条线给我带来了麻烦:for i in data.iloc[:,1:]:            data['predicted_{}'.format(i)] = (data[i] >= threshold).astype('int')运行它时得到的输出示例:
查看完整描述

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)

这样原始数据就不会发生变异。


希望这可以帮助。


查看完整回答
反对 回复 2022-07-12
  • 1 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号