Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<normalization>
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl
scaler=StandardScaler()
df_test_2=scaler.fit_transform(df_test )<save_to_csv>
import pandas as pd
import datatable as dt
import lightgbm as lgb
from matplotlib import pyplot as plt
result_2=model2.predict(df_test_2)
 Result_logistic_Regression_2=pd.DataFrame({'PassengerId':g1,'Survived':result_2})
 Result_logistic_Regression_2.to_csv("Result_logistic_Regression_2.csv",index=False,header=True )<normalization>
features = ['user_id', 'content_id', 'answered_correctly', 'prior_question_elapsed_time', 'prior_question_had_explanation']
train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv' ).to_pandas()
train_df = train_df[features]
scaler=MinMaxScaler()
df_test_3=scaler.fit_transform(df_test )<save_to_csv>
train_df = train_df[train_df['answered_correctly'] != -1].reset_index(drop=True)
 train_df.fillna(False, inplace=True)
 train_df['user_id'] = train_df['user_id'].astype('int32')
 train_df['content_id'] = train_df['content_id'].astype('int16')
 train_df['answered_correctly'] = train_df['answered_correctly'].astype('int8')
 train_df['prior_question_elapsed_time'] = train_df['prior_question_elapsed_time'].astype('float32')
 train_df['prior_question_had_explanation'] = train_df['prior_question_had_explanation'].astype('bool' )
result_3=model3.predict(df_test_3)
 Result_logistic_Regression_3=pd.DataFrame({'PassengerId':g1,'Survived':result_3})
 Result_logistic_Regression_3.to_csv("Result_logistic_Regression_3.csv",index=False,header=True )<save_to_csv>
train_df['user'] = train_df.groupby('user_id')['answered_correctly'].shift()
cumulated = train_df.groupby('user_id')['user'].agg(['cumsum', 'cumcount'])
 train_df['user_correctness'] = cumulated['cumsum'] / cumulated['cumcount']
train_df.drop(columns=['user'], inplace=True )
result_4=model4.predict(df_test)
 Result_logistic_Regression_4=pd.DataFrame({'PassengerId':g1,'Survived':result_4})
 Result_logistic_Regression_4.to_csv("Result_SVM_1.csv",index=False,header=True )<save_to_csv>
user_agg = train_df.groupby('user_id')['answered_correctly'].agg(['sum', 'count'])
 content_agg = train_df.groupby('content_id')['answered_correctly'].agg(['sum', 'count'] )
result_5=model5.predict(df_test_2)
 Result_logistic_Regression_5=pd.DataFrame({'PassengerId':g1,'Survived':result_5})
 Result_logistic_Regression_5.to_csv("Result_SVM_2.csv",index=False,header=True )<save_to_csv>
questions_df = pd.read_csv('.. /input/riiid-test-answer-prediction/questions.csv')[['question_id', 'part']]
questions_df['question_id'] = questions_df['question_id'].astype('int16')
 questions_df['part'] = questions_df['part'].astype('int8')
 train_df = pd.merge(train_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 train_df.drop(columns=['question_id'], inplace=True )
result_6=model6.predict(df_test_3)
 Result_logistic_Regression_6=pd.DataFrame({'PassengerId':g1,'Survived':result_6})
 Result_logistic_Regression_6.to_csv("Result_SVM_3.csv",index=False,header=True )<save_to_csv>
train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_id'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] )
result_7=model7.predict(df_test)
 Result_logistic_Regression_7=pd.DataFrame({'PassengerId':g1,'Survived':result_7})
 Result_logistic_Regression_7.to_csv("Result_Decision_tree_1.csv",index=False,header=True )<save_to_csv>
valid_df = train_df.groupby('user_id' ).tail(15)
 train_df.drop(valid_df.index, inplace=True )
result_8=model8.predict(df_test_2)
 Result_logistic_Regression_8=pd.DataFrame({'PassengerId':g1,'Survived':result_8})
 Result_logistic_Regression_8.to_csv("Result_Decision_tree_2.csv",index=False,header=True )<save_to_csv>
features = [
'content_id',
'prior_question_elapsed_time',
'prior_question_had_explanation',
'user_correctness',
'part',
'content_count'
]
target = 'answered_correctly'
result_9=model9.predict(df_test_3)
 Result_logistic_Regression_9=pd.DataFrame({'PassengerId':g1,'Survived':result_9})
 Result_logistic_Regression_9.to_csv("Result_Decision_tree_3.csv",index=False,header=True )<save_to_csv>
params = {
'objective': 'binary',
'seed': 42,
'metric': 'auc',
'learning_rate': 0.05,
'max_bin': 800,
'num_leaves': 100
}
result_10=model10.predict(df_test)
 Result_logistic_Regression_10=pd.DataFrame({'PassengerId':g1,'Survived':result_10})
 Result_logistic_Regression_10.to_csv("Result_Random_Forest_1.csv",index=False,header=True )<save_to_csv>
tr_data = lgb.Dataset(train_df[features], label=train_df[target])
 va_data = lgb.Dataset(valid_df[features], label=valid_df[target])
 model = lgb.train(
 params,
tr_data,
num_boost_round=10000,
valid_sets=[tr_data, va_data],
early_stopping_rounds=50,
verbose_eval=50 
)
result_11=model11.predict(df_test_2)
 Result_logistic_Regression_11=pd.DataFrame({'PassengerId':g1,'Survived':result_11})
 Result_logistic_Regression_11.to_csv("Result_Random_Forest_2.csv",index=False,header=True )<save_to_csv>
import riiideducation
result_12=model12.predict(df_test_3)
 Result_logistic_Regression_12=pd.DataFrame({'PassengerId':g1,'Survived':result_12})
 Result_logistic_Regression_12.to_csv("Result_Random_Forest_3.csv",index=False,header=True )<save_to_csv>
env = riiideducation.make_env()
iter_test = env.iter_test()
prior_test_df = None
<save_to_csv><EOS> 
for(test_df, sample_prediction_df)in iter_test:
if prior_test_df is not None:
prior_test_df[target] = eval(test_df['prior_group_answers_correct'].iloc[0])
 prior_test_df = prior_test_df[prior_test_df[target] != -1].reset_index(drop=True)
 user_ids = prior_test_df['user_id'].values
content_ids = prior_test_df['content_id'].values
targets = prior_test_df[target].values
for user_id, content_id, answered_correctly in zip(user_ids, content_ids, targets):
if user_id in user_agg.index:
user_agg.loc[user_id, 'sum'] += answered_correctly
user_agg.loc[user_id, 'count'] += 1
else:
user_agg.loc[user_id] = [answered_correctly, 1]
if content_id in content_agg.index:
content_agg.loc[content_id, 'sum'] += answered_correctly
content_agg.loc[content_id, 'count'] += 1
else:
content_agg.loc[content_id] = [answered_correctly, 1]
prior_test_df = test_df.copy()
test_df = pd.merge(test_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 test_df['prior_question_had_explanation'] = test_df['prior_question_had_explanation'].fillna(False ).astype('bool')
 test_df['user_correctness'] = test_df['user_id'].map(user_agg['sum'] / user_agg['count'])
 test_df['content_count'] = test_df['content_id'].map(content_agg['count'] ).fillna(1)
 test_df['content_id'] = test_df['content_id'].map(content_agg['sum'] / content_agg['count'] ).fillna(0.7)
 test_df['answered_correctly'] = model.predict(test_df[features])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )