<jupyter_start><jupyter_text>LGBM Inference<jupyter_code>import pandas as pd
import os
import random
import math
from tqdm import tqdm
import numpy as np
import time

import lightgbm as lgb
from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score
import torch<jupyter_output><empty_output><jupyter_text>데이터 로드<jupyter_code># 성능 제일 좋았음 07 서브미션

# train = pd.read_csv("/opt/ml/input/data/train_dataset/cv_train_data_FE.csv")
# valid = pd.read_csv("/opt/ml/input/data/train_dataset/cv_valid_data_FE.csv")
# test = pd.read_csv('/opt/ml/input/data/train_dataset/test_data_FE.csv')
# 성능 좀 떨어졌음 새로 만든 것 - 델타 새로하고, 테스트셋 조인 새로함

# train = pd.read_csv('/opt/ml/input/data/train_dataset/cv_new_train.csv')
# valid = pd.read_csv("/opt/ml/input/data/train_dataset/cv_new_valid.csv")
# test = pd.read_csv('/opt/ml/input/data/train_dataset/new_test.csv')
# 성능 제일 좋았던 것에서 델타 새로하고 상대누적값 추가 - 5천 돌렸을때 가장 높은 성능

train = pd.read_csv('/opt/ml/input/data/train_dataset/cv_train_data_FE_modify_FE_relative_log.csv')
valid = pd.read_csv("/opt/ml/input/data/train_dataset/cv_valid_data_FE_modify_FE_relative_log.csv")
test = pd.read_csv('/opt/ml/input/data/train_dataset/test_data_FE_modify_FE_relative_log.csv')
train
valid
test
# 테스트셋 사용하기
test_for_train = test.loc[test.answerCode != -1]
# 컬럼 떨구고 합치기
train = train.drop(['cum_correct'], axis = 1)
valid = valid.drop(['cum_correct'], axis = 1)

train = pd.concat([train, test_for_train]).sort_values(by=['userID', 'Timestamp']).reset_index()
train = train.drop(['index'], axis = 1)
train
# LEAVE LAST INTERACTION ONLY
test = test[test['userID'] != test['userID'].shift(-1)]
# DROP ANSWERCODE
test = test.drop(['answerCode'], axis=1)
train.columns
test<jupyter_output><empty_output><jupyter_text>카테고리 피쳐 라벨링<jupyter_code>from sklearn.preprocessing import OrdinalEncoder, LabelEncoder

ordinal_feats = ['grade']
label_feats = ['problem_number','hour','dow','solved_disorder','KnowledgeTag','testId','retest']
        
# 'problem_number','grade', 'testId','KnowledgeTag','problem_count','type_count','solved_disorder'

for c in ordinal_feats :
    X = train[c].values.reshape(-1,1)
    enc = OrdinalEncoder()
    enc.fit(X)
    X = enc.transform(X)
    train[c] = X

    X = valid[c].values.reshape(-1,1)
    X = enc.transform(X)
    valid[c] = X
    
    X = test[c].values.reshape(-1,1)
    X = enc.transform(X)
    test[c] = X
    
for c in label_feats :
    X = train[c].values.reshape(-1,1)
    enc = LabelEncoder()
    enc.fit(X)
    X = enc.transform(X)
    train[c] = X

    X = valid[c].values.reshape(-1,1)
    X = enc.transform(X)
    valid[c] = X
    
    X = test[c].values.reshape(-1,1)
    X = enc.transform(X)
    test[c] = X
# 사용할 Feature 설정

FEATS = [
    'last_problem', 'elapsed', 'grade', 'prior_KnowledgeTag_frequency','KnowledgeTag','testId','retest','hour','dow',"hour_mean", 'hour_sum', "dow_mean", 'dow_sum',
       'problem_number', 'test_mean', 'test_sum', 'tag_mean', 'tag_sum', 'elo_prob',
    
        'tag_delta', 'answer_delta', 'test_delta', 'assess_delta',
    
        'prior_relative_assess_ac_sum', 'prior_relative_answer_ac_sum',
       'prior_relative_tag_ac_sum', 'prior_relative_test_ac_sum',
    
       'ass_mean', 'ass_sum', 'prb_mean', 'prb_sum', 'tag_elp', 'tag_elp_o',
       'tag_elp_x', 'ass_elp', 'ass_elp_o', 'ass_elp_x', 'prb_elp',
       'prb_elp_o', 'prb_elp_x', 'user_correct_answer', 'user_total_answer',
       'user_acc', 'Grade_o', 'GradeCount', 'GradeAcc', 'GradeElp',
       'GradeMElp', 'problem_count', 'tag_count', 'RepeatedTime',
       'problem_position', 'solve_order', 'solved_disorder'
]

# X, y 값 분리
y_train = train['answerCode']
train = train.drop(['answerCode'], axis=1)

y_valid = valid['answerCode']
valid = valid.drop(['answerCode'], axis=1)
lgb_train = lgb.Dataset(train[FEATS], y_train)
lgb_valid = lgb.Dataset(valid[FEATS], y_valid)<jupyter_output><empty_output><jupyter_text>4. 훈련 및 검증<jupyter_code>'''
    original
'''
model = lgb.train(
                    {'objective': 'binary','metric':'auc', 'boosting' : 'dart', 'learning_rate':0.05},
#                     {'objective': 'binary','metric':'auc','boosting':'dart',
#                      'learning_rate':0.05,'max_depth':15,'feature_fraction':0.8},
                    lgb_train,
                    valid_sets=[lgb_train, lgb_valid],
                    verbose_eval=100,
                    num_boost_round=8000,
                    early_stopping_rounds=200
                )

model.save_model('/opt/ml/output/lgbm_train_with_test_8000.txt')

preds = model.predict(valid[FEATS])
acc = accuracy_score(y_valid, np.where(preds >= 0.5, 1, 0))
auc = roc_auc_score(y_valid, preds)

print(f'VALID AUC : {auc} ACC : {acc}\n')
# 추가 학습
lgb_new_train = lgb.Dataset(train[FEATS], y_train, free_raw_data = False)
lgb_new_valid = lgb.Dataset(valid[FEATS], y_valid, free_raw_data = False)

train_again = lgb.train(
                    {'objective': 'binary','metric':'auc', 'boosting' : 'dart', 'learning_rate':0.05},
                    lgb_new_train,
                    valid_sets=[lgb_new_train, lgb_new_valid],
                    verbose_eval=100,
                    num_boost_round=2000,
                    early_stopping_rounds=200,
                    keep_training_booster = True,
                    init_model=model)

train_again.save_model('/opt/ml/output/lgbm_train_with_test_10000.txt')
preds = train_again.predict(valid[FEATS])
acc = accuracy_score(y_valid, np.where(preds >= 0.5, 1, 0))
auc = roc_auc_score(y_valid, preds)

print(f'VALID AUC : {auc} ACC : {acc}\n')
## 불러올때 이렇게 불러와야된다 ~!!!
#model = lgb.Booster(model_file='/opt/ml/output/lgbm_train_with_test.txt')
# 이어서한 모델 피쳐임포턴스
ax = lgb.plot_importance(train_again)
fig = ax.figure
fig.set_size_inches(30, 40)
# MAKE PREDICTION
total_preds = train_again.predict(test[FEATS])

output_dir = '/opt/ml/output/'
write_path = os.path.join(output_dir, "LGBM_all_modified_features_with_testset_10000 (dart).csv")
if not os.path.exists(output_dir):
    os.makedirs(output_dir)    
with open(write_path, 'w', encoding='utf8') as w:
    print("writing prediction : {}".format(write_path))
    w.write("id,prediction\n")
    for id, p in enumerate(total_preds):
        w.write('{},{}\n'.format(id,p))
total_preds
# Feature Importance 파악
import matplotlib.pyplot as plt
ax = lgb.plot_importance(model)
fig = ax.figure
fig.set_size_inches(30, 40)<jupyter_output><empty_output><jupyter_text>Inference<jupyter_code># MAKE PREDICTION
total_preds = model.predict(test[FEATS])
# SAVE OUTPUT
output_dir = '/opt/ml/output/'
write_path = os.path.join(output_dir, "LGBM_all_modified_features_with_testset_8000 (dart).csv")
if not os.path.exists(output_dir):
    os.makedirs(output_dir)    
with open(write_path, 'w', encoding='utf8') as w:
    print("writing prediction : {}".format(write_path))
    w.write("id,prediction\n")
    for id, p in enumerate(total_preds):
        w.write('{},{}\n'.format(id,p))
total_preds<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15323967889908258, 'fraction_numerical': 0.013331422018348624, 'mean_word_length': 4.118855465884079, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '279132', 'license': 'Unlicense', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2859, 'n_words': 432, 'path': 'dongjae/lgbm_inference_new_plus.ipynb', 'repo_name': 'DongjaeJang/Deep-Knowledge-Tracing', 'size': '6976'}