第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

循環(huán)實(shí)例無限附加字符串

循環(huán)實(shí)例無限附加字符串

阿晨1998 2022-07-12 17:57:41
我試圖在 OOP 中編寫一個(gè)表,該表將返回一些算法的統(tǒng)計(jì)指標(biāo),并在 Python 中的 pandas DataFrame 中顯示。我遇到的問題是,對于每個(gè)實(shí)例,列的名稱都會在正在創(chuàng)建的 DataFrame 中附加一個(gè)“預(yù)測”的附加字符串(最后的示例)。我的代碼: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這條線給我?guī)砹寺闊篺or i in data.iloc[:,1:]:            data['predicted_{}'.format(i)] = (data[i] >= threshold).astype('int')運(yùn)行它時(shí)得到的輸出示例:
查看完整描述

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ā)生變異。


希望這可以幫助。


查看完整回答
反對 回復(fù) 2022-07-12
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號