亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

循環實例無限附加字符串

循環實例無限附加字符串

阿晨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 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號