text_html_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import f1_score

from sklearn.model_selection import GroupKFold

import gc

import joblib

import numpy as np # Import library NumPy (Kalkulasi dsb)

import pandas as pd # Import library Pandas (Baca data)

tmp = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv', usecols=[0], low_memory=True)
tmp = tmp.groupby('session_id').session_id.agg('count')

labels = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')
labels['session'] = labels['session_id'].str.split('_', expand=True)[0].astype(np.uint64)
labels['q'] = labels['session_id'].str.split('_q', expand=True)[1].astype(int)
labels

labels.dtypes

import gc
gc.enable()
gc.collect()

ITER = 10
PIECES = int(np.ceil(len(tmp) / ITER))
reads = []
skips = [0]
for k in range(ITER):
    a = k * PIECES
    b = (k + 1) * PIECES
    if b > len(tmp):
        b = len(tmp)
    r = tmp.iloc[a:b].sum()
    reads.append(r)
    skips.append(skips[-1] + r)

dtyping = {'session_id': np.uint64, 'index': np.uint8, 'elapsed_time': np.uint8, 'event_name': 'category', 'name': 'category', 'level': np.uint8, 'page': np.float32, 'room_coor_x': np.float32, 'room_coor_y': np.float32, 'screen_coor_x': np.float32, 'screen_coor_y': np.float32, 'hover_duration': np.float32, 'text': 'category', 'fqid': 'category', 'room_fqid': 'category', 'text_fqid': 'category', 'fullscreen': np.bool8, 'hq': np.bool8, 'music': np.bool8, 'level_group': 'category'}

CATS = ['event_name', 'fqid', 'room_fqid', 'text']
NUMS = ['elapsed_time', 'level', 'page', 'room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration']
EVENTS = ['navigate_click', 'person_click', 'cutscene_click', 'object_click', 'map_hover', 'notification_click', 'map_click', 'observation_click', 'checkpoint']

def process_level(train):
    dfs = []
    for c in CATS:
        tmp = train.groupby(['session_id', 'level_group'])[c].agg('nunique')
        tmp.name = tmp.name + '_nunique'
        dfs.append(tmp)
    for c in NUMS:
        tmp = train.groupby(['session_id', 'level_group'])[c].agg('mean')
        tmp.name = tmp.name + '_mean'
        dfs.append(tmp)
    for c in NUMS:
        tmp = train.groupby(['session_id', 'level_group'])[c].agg('std')
        tmp.name = tmp.name + '_std'
        dfs.append(tmp)
    for c in EVENTS:
        train[c] = (train.event_name == c).astype('int8')
    for c in EVENTS + ['elapsed_time']:
        tmp = train.groupby(['session_id', 'level_group'])[c].agg('sum')
        tmp.name = tmp.name + '_sum'
        dfs.append(tmp)
    train = train.drop(EVENTS, axis=1)
    df = pd.concat(dfs, axis=1)
    df = df.fillna(-1)
    df = df.reset_index()
    df = df.set_index('session_id')
    return df

FEATURES = [c for c in train_df.columns if not c in ['level_group']]
ALL_USERS = train_df.index.unique()

train_level_df = train_df.pivot_table(columns='level_group', values=[x for x in train_df.columns if not x == 'level_group'], index='session_id', aggfunc='sum', fill_value=0)

train_level_df.isna().sum()

from sklearn.model_selection import GroupKFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
gkf = GroupKFold(n_splits=5)
oof = pd.DataFrame(data=np.zeros((len(ALL_USERS), 18)), index=ALL_USERS)
models = {}
for i, (t, v) in enumerate(gkf.split(X=train_level_df, groups=train_level_df.index)):
    print(f'FOLD {i}')
    print('')
    for l in range(1, 19):
        xtrain = train_level_df.iloc[t]
        train_users = xtrain.index.values
        ytrain = labels.loc[labels.q == l].set_index('session').loc[train_users]
        xval = train_level_df.iloc[v]
        val_users = xval.index.values
        yval = labels.loc[labels.q == l].set_index('session').loc[val_users]
        model = LogisticRegression(random_state=0, solver='liblinear', max_iter=1500, n_jobs=-1)
        model.fit(xtrain[FEATURES].astype(np.float32), ytrain['correct'])
        yhat = model.predict_proba(xval)[:, 1]
        score = f1_score(yval['correct'], np.round(yhat).astype(int))
        print(f'FOLD {i} LEVEL {l} : {score}')
        models.update({f'{i}': model})
        joblib.dump(model, f'model-fold{i}-level{l}.pkl')
        oof.loc[val_users, l - 1] = yhat
        del xtrain, train_users, ytrain, xval, val_users, yval, model, yhat, score
        gc.collect()
    print()