<jupyter_start><jupyter_text>疑似ラベルに使う用に確信度の高いtestデータだけ抽出する<jupyter_code>import glob
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
#pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
inf_cv_csvs = glob.glob(f"inf_cv/*_inf_cv_seed*")
inf_test_csvs = glob.glob(f"inf_test/*_inf_test_seed*")
logit_th = 0.7<jupyter_output><empty_output><jupyter_text>oof check<jupyter_code>logit_sum = None
pred_onehot_sum = None
for c in inf_cv_csvs:
    df = pd.read_csv(c).sort_values(by="image_id")
    
    logit = df[[str(i) for i in range(NUM_CLASSES)]].values
    pred = logit.argmax(1)
    
    logit_sum = logit if logit_sum is None else logit_sum + logit
    pred_onehot_sum = np.eye(NUM_CLASSES)[pred] if pred_onehot_sum is None else pred_onehot_sum + np.eye(NUM_CLASSES)[pred]
    
logit_sum = pd.DataFrame(logit_sum)
pred_onehot_sum = pd.DataFrame(pred_onehot_sum)

label = df["label"].values
logit_sum["big_logit"] = logit_sum.apply(lambda row: 1 if max(row) >=  logit_th * len(inf_cv_csvs) else 0 , axis=1)
logit_sum
pred_label = pred_onehot_sum.apply(lambda row: row.argmax(), axis=1)
pred_onehot_sum["pred_all_same"] = pred_onehot_sum.apply(lambda row: 1 if max(row) == len(inf_cv_csvs) else 0, axis=1)
pred_onehot_sum["pred_label"] = pred_label
pred_onehot_sum
df = pd.DataFrame({"label": label})
df = df.join(logit_sum[["big_logit"]])
df = df.join(pred_onehot_sum[["pred_label", "pred_all_same"]])
df
accuracy_score(df["label"], df["pred_label"])
_df = df[df["pred_all_same"] == 0]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[df["pred_all_same"] == 1]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[df["big_logit"] == 1]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[(df["big_logit"] == 0) & (df["pred_all_same"] == 1)]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[(df["big_logit"] == 1) & (df["pred_all_same"] == 1)]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[(df["big_logit"] == 1) & 
         (df["pred_all_same"] == 1) & 
         (~df["pred_label"].isin([1,3,4]))
        ]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])
_df = df[(df["big_logit"] == 1) & 
         (df["pred_all_same"] == 1) & 
         (df["pred_label"].isin([0,6,7,8,9,11]))
        ]
print(_df.shape)
accuracy_score(_df["label"], _df["pred_label"])<jupyter_output>(132, 4)<jupyter_text>test check<jupyter_code>logit_sum = None
pred_onehot_sum = None
for c in inf_test_csvs:
    df = pd.read_csv(c).sort_values(by="image_id")
    
    logit = df[[str(i) for i in range(NUM_CLASSES)]].values
    pred = logit.argmax(1)
    
    logit_sum = logit if logit_sum is None else logit_sum + logit
    pred_onehot_sum = np.eye(NUM_CLASSES)[pred] if pred_onehot_sum is None else pred_onehot_sum + np.eye(NUM_CLASSES)[pred]
    
logit_sum = pd.DataFrame(logit_sum)
pred_onehot_sum = pd.DataFrame(pred_onehot_sum)

logit_sum["big_logit"] = logit_sum.apply(lambda row: 1 if row.max() >= logit_th * len(inf_test_csvs)  else 0 , axis=1)
#display(logit_sum.style.background_gradient(cmap="Blues"))

pred_label = pred_onehot_sum.apply(lambda row: row.argmax(), axis=1)
pred_onehot_sum["pred_all_same"] = pred_onehot_sum.apply(lambda row: 1 if max(row) == len(inf_test_csvs) else 0, axis=1)
pred_onehot_sum["pred_label"] = pred_label
#display(pred_onehot_sum.style.background_gradient(cmap="Blues"))

df = logit_sum[["big_logit"]]
df = df.join(pred_onehot_sum[["pred_label", "pred_all_same"]])
df
_df = df[df["pred_all_same"] == 0]
print(_df.shape)
_df = df[df["pred_all_same"] == 1]
print(_df.shape)

df_tmp = pd.read_csv(inf_test_csvs[0]).sort_values(by="image_id")[["image_id", "file_path"]]

_df = _df.reset_index().rename(columns={'index':'image_id'})
_df = pd.merge(df_tmp, _df, on="image_id")
_df[["image_id", "file_path", "pred_label"]].to_csv(f"{NAME}_big_logit_test_pred_all_same1.csv", index=False)

#_df[["image_id", "file_path", "pred_label"]]
_df = df[df["big_logit"] == 1]
print(_df.shape)
_df = df[(df["big_logit"] == 0) & (df["pred_all_same"] == 1)]
print(_df.shape)
_df = df[(df["big_logit"] == 1) & (df["pred_all_same"] == 1)]
print(_df.shape)

df_tmp = pd.read_csv(inf_test_csvs[0]).sort_values(by="image_id")[["image_id", "file_path"]]

_df = _df.reset_index().rename(columns={'index':'image_id'})
_df = pd.merge(df_tmp, _df, on="image_id")
_df[["image_id", "file_path", "pred_label"]].to_csv(f"{NAME}_big_logit_test.csv", index=False)

#_df[["image_id", "file_path", "pred_label"]]
_df = df[(df["big_logit"] == 1) & 
         (df["pred_all_same"] == 1) & 
         (~df["pred_label"].isin([1,3,4]))
        ]
print(_df.shape)

df_tmp = pd.read_csv(inf_test_csvs[0]).sort_values(by="image_id")[["image_id", "file_path"]]

_df = _df.reset_index().rename(columns={'index':'image_id'})
_df = pd.merge(df_tmp, _df, on="image_id")
_df[["image_id", "file_path", "pred_label"]].to_csv(f"{NAME}_big_logit_test_isnot134.csv", index=False)

#_df[["image_id", "file_path", "pred_label"]]
_df = df[(df["big_logit"] == 1) & 
         (df["pred_all_same"] == 1) & 
         (df["pred_label"].isin([0,6,7,8,9,11]))
        ]
print(_df.shape)

df_tmp = pd.read_csv(inf_test_csvs[0]).sort_values(by="image_id")[["image_id", "file_path"]]

_df = _df.reset_index().rename(columns={'index':'image_id'})
_df = pd.merge(df_tmp, _df, on="image_id")
_df[["image_id", "file_path", "pred_label"]].to_csv(f"{NAME}_big_logit_test_is06789_11.csv", index=False)

#_df[["image_id", "file_path", "pred_label"]]<jupyter_output>(131, 3)
{'fraction_non_alphanumeric': 0.1905865573185951, 'fraction_numerical': 0.01479764663932965, 'mean_word_length': 6.581081081081081, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '44512', 'license': 'MIT', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2390, 'n_words': 339, 'path': 'notebook/first/make_pseudo_label/ex11_ex16_big_logit_test-checkpoint.ipynb', 'repo_name': 'riron1206/probspace_religious_art_1st_solution', 'size': '5609'}