Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<compute_test_metric>
env = riiideducation.make_env()
print(classification_report(y_pred_train, y_train))<compute_test_metric>
train = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/train.csv',
usecols=[1, 2, 3, 4, 5, 7, 8, 9],
dtype={'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id': 'int8',
'task_container_id': 'int16',
'answered_correctly':'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'boolean'}
)
print(classification_report(y_pred_test, y_test))<train_model>
questions_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 3],
dtype={'question_id': 'int16',
'part': 'int8'}
)
perm = PermutationImportance(log_reg ).fit(X_train, y_train)
 show_weights(perm, feature_names = list(X_train.columns))<prepare_output>
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv' )
weights = pd.DataFrame(data=log_reg.coef_[0], index=scaled_df.columns, columns=['Weights'])
 weights.loc['Intercept'] = log_reg.intercept_
weights.sort_values('Weights', ascending=False, inplace=True)
 weights.head(15 )<create_dataframe>
lectures_df['type_of'] = lectures_df['type_of'].replace('solving question', 'solving_question')
 lectures_df = pd.get_dummies(lectures_df, columns=['part', 'type_of'])
 part_lectures_columns = [column for column in lectures_df.columns if column.startswith('part')]
types_of_lectures_columns = [column for column in lectures_df.columns if column.startswith('type_of_')]
output = pd.DataFrame({'PassengerId': test_df['PassengerId'], 'Survived': y_pred_test.astype('int')} )<save_to_csv>
train_lectures = train[train.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left' )
output.to_csv('submission.csv', index=False )<import_modules>
user_lecture_stats_part = train_lectures.groupby('user_id')[part_lectures_columns + types_of_lectures_columns].sum()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import kurtosis
from scipy.stats import skew<set_options>
for column in user_lecture_stats_part.columns:
bool_column = column + '_boolean'
user_lecture_stats_part[bool_column] =(user_lecture_stats_part[column] > 0 ).astype(int )
plt.rcParams['figure.figsize'] =(8,5)
 plt.style.use("ggplot" )<load_from_csv>
del(train_lectures )
train=pd.read_csv(".. /input/titanic/train.csv")
 test=pd.read_csv(".. /input/titanic/test.csv" )<count_missing_values>
train = train[train.content_type_id == False].sort_values('timestamp' ).reset_index(drop = True )
train.isna().sum()<count_missing_values>
train[(train.content_type_id == False)].task_container_id.nunique()
test.isna().sum()<drop_column>
group1 = train.loc[(train.content_type_id == False), ['task_container_id', 'user_id']].groupby(['task_container_id'] ).agg(['count'])
 group1.columns = ['avg_questions']
group2 = train.loc[(train.content_type_id == False), ['task_container_id', 'user_id']].groupby(['task_container_id'] ).agg(['nunique'])
 group2.columns = ['avg_questions']
group3 = group1 / group2
train.drop("Cabin",axis=1,inplace=True)
 test.drop("Cabin",axis=1,inplace=True )<create_dataframe>
group3['avg_questions_seen'] = group3.avg_questions.cumsum()
df=pd.DataFrame(train.groupby(["Pclass","Sex"] ).sum().Survived)
 df.reset_index(inplace=True)
 df<categorify>
group3.iloc[0].avg_questions_seen
train["age_category"]=""
for i in range(891):
if train.loc[i,"Age"]<1:
train.loc[i,"age_category"]="Infants"
elif 1<=train.loc[i,"Age"]<18:
train.loc[i,"age_category"]="Children"
elif 18<=train.loc[i,"Age"]<65:
train.loc[i,"age_category"]="Adults"
elif train.loc[i,"Age"]>=65:
train.loc[i,"age_category"]="Elders"
else:
train.loc[i,"age_category"]="Unknown"
train.head()<count_values>
results_u_final = train.loc[train.content_type_id == False, ['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_final.columns = ['answered_correctly_user']
results_u2_final = train.loc[train.content_type_id == False, ['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_final.columns = ['explanation_mean_user']
df_2=pd.DataFrame(train.groupby(["age_category",] ).sum().Survived)
 df_2["Total"]=train["age_category"].value_counts()
df_2.reset_index(inplace=True)
 df_2<groupby>
train = pd.merge(train, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left' )
df_3=train.groupby("SibSp" ).sum().Survived
df_3<groupby>
results_q_final = train.loc[train.content_type_id == False, ['question_id','answered_correctly']].groupby(['question_id'] ).agg(['mean'])
 results_q_final.columns = ['quest_pct']
df_4=train.groupby("Parch" ).sum().Survived
df_4<create_dataframe>
results_q2_final = train.loc[train.content_type_id == False, ['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['count']
data={"Class":["Class1","Class2","Class3"],
"Passengers":[train[train.Pclass==1].shape[0],train[train.Pclass==2].shape[0],train[train.Pclass==3].shape[0]]}<create_dataframe>
question2 = pd.merge(questions_df, results_q_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
data={"Class":["Class1","Class2","Class3"],
"Passengers":[train[train.Pclass==1].shape[0],train[train.Pclass==2].shape[0],train[train.Pclass==3].shape[0]]}
Class=pd.DataFrame(data,columns=["Class","Passengers"])
 Class<string_transform>
question2 = pd.merge(question2, results_q2_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
train["status"]=""
for i in range(891):
if "Mr." in(train.Name[i].split()):
train.loc[i,"status"]="Mister"
elif "Miss." in(train.Name[i].split()):
train.loc[i,"status"]="Miss"
elif "Mrs." in(train.Name[i].split()):
train.loc[i,"status"]="Mistress"
elif "Rev." in(train.Name[i].split()):
train.loc[i,"status"]="Reverend"
elif "Master." in(train.Name[i].split()):
train.loc[i,"status"]="Master"
elif "Mlle." in(train.Name[i].split()):
train.loc[i,"status"]="Mademoiselle"
elif "Dr." in(train.Name[i].split()):
train.loc[i,"status"]="Doctor"
else:
train.loc[i,"status"]="Other"<count_values>
question2.quest_pct = round(question2.quest_pct,5 )
train.status.value_counts()<filter>
train.drop(['timestamp', 'content_type_id', 'question_id', 'part'], axis=1, inplace=True )
train[train.status=='Other']<count_values>
validation = train.groupby('user_id' ).tail(5)
 train = train[~train.index.isin(validation.index)]
len(train)+ len(validation )
print(train.Sex.value_counts())
 print("{:.2f} % of people were males".format(( train.Sex.value_counts() [0]/891)*100))
print("{:.2f} % of people were females".format(( train.Sex.value_counts() [1]/891)*100))<groupby>
results_u_val = train[['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_val.columns = ['answered_correctly_user']
results_u2_val = train[['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_val.columns = ['explanation_mean_user']
print(train.groupby("Sex" ).sum().Survived)
 print("{:.2f} % of females survived".format(( train.groupby("Sex" ).sum().Survived[0]/314)*100))
print("{:.2f} % of males survived".format(( train.groupby("Sex" ).sum().Survived[1]/577)*100)) 
<compute_test_metric>
X = train.groupby('user_id' ).tail(18)
 train = train[~train.index.isin(X.index)]
len(X)+ len(train)+ len(validation )
print("Skewness of Age distribution is {:.2f}".format(skew(train.dropna().Age)))
 print("Kurtosis of Age distribution is {:.2f}".format(kurtosis(train.dropna().Age)) )<count_values>
results_u_X = train[['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_X.columns = ['answered_correctly_user']
results_u2_X = train[['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_X.columns = ['explanation_mean_user']
train.SibSp.value_counts()<count_values>
del(train )
train.Parch.value_counts()<drop_column>
X = pd.merge(X, group3, left_on=['task_container_id'], right_index= True, how="left")
 X = pd.merge(X, results_u_X, on=['user_id'], how="left")
 X = pd.merge(X, results_u2_X, on=['user_id'], how="left")
 X = pd.merge(X, user_lecture_stats_part, on=['user_id'], how="left" )
train.drop("Ticket",axis=1,inplace=True)
 train.head()<groupby>
validation = pd.merge(validation, group3, left_on=['task_container_id'], right_index= True, how="left")
 validation = pd.merge(validation, results_u_val, on=['user_id'], how="left")
 validation = pd.merge(validation, results_u2_val, on=['user_id'], how="left")
 validation = pd.merge(validation, user_lecture_stats_part, on=['user_id'], how="left" )
train.groupby("Pclass" ).mean().Fare<count_values>
lb_make = LabelEncoder()
X.prior_question_had_explanation.fillna(False, inplace = True)
 validation.prior_question_had_explanation.fillna(False, inplace = True)
 validation["prior_question_had_explanation_enc"] = lb_make.fit_transform(validation["prior_question_had_explanation"])
 X["prior_question_had_explanation_enc"] = lb_make.fit_transform(X["prior_question_had_explanation"] )
train.Embarked.value_counts()<create_dataframe>
embarked=pd.DataFrame(train[["Pclass","Embarked"]].value_counts(sort=False))
embarked.reset_index(inplace=True)
 embarked.columns=["Class","Embarked","No.of passengers"]
embarked<filter>
question2.quest_pct = question2.quest_pct.mask(( question2['count'] < 3),.65)
 question2.quest_pct = question2.quest_pct.mask(( question2.quest_pct <.2)&(question2['count'] < 21),.2)
 question2.quest_pct = question2.quest_pct.mask(( question2.quest_pct >.95)&(question2['count'] < 21),.95 )
train[(train.status=="Master")&(train.Age.isna())]<groupby>
X = pd.merge(X, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 validation = pd.merge(validation, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 X.part = X.part - 1
validation.part = validation.part - 1
mean_age_children=round(train[(train["age_category"]=="Children")|(train["age_category"]=="Infants")].Age.mean())
 mean_age_children 
<data_type_conversions>
y = X['answered_correctly']
X = X.drop(['answered_correctly'], axis=1)
 X.head()
y_val = validation['answered_correctly']
X_val = validation.drop(['answered_correctly'], axis=1 )
train[(train.status=="Master")&(train.Age.isna())]=train[(train.status=="Master")&(train.Age.isna())].fillna(mean_age_children)
 train[train.status=="Master"].Age.isna().sum()<filter>
X = X[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]
X_val = X_val[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]
train[(train.status=="Mistress")&(train.Age.isna())]<groupby>
X['answered_correctly_user'].fillna(0.65, inplace=True)
 X['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X['quest_pct'].fillna(content_mean, inplace=True)
 X['part'].fillna(4, inplace = True)
 X['avg_questions_seen'].fillna(1, inplace = True)
 X['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X['prior_question_had_explanation_enc'].fillna(0, inplace = True)
 X['part_1'].fillna(0, inplace = True)
 X['part_2'].fillna(0, inplace = True)
 X['part_3'].fillna(0, inplace = True)
 X['part_4'].fillna(0, inplace = True)
 X['part_5'].fillna(0, inplace = True)
 X['part_6'].fillna(0, inplace = True)
 X['part_7'].fillna(0, inplace = True)
 X['type_of_concept'].fillna(0, inplace = True)
 X['type_of_intention'].fillna(0, inplace = True)
 X['type_of_solving_question'].fillna(0, inplace = True)
 X['type_of_starter'].fillna(0, inplace = True)
 X['part_1_boolean'].fillna(0, inplace = True)
 X['part_2_boolean'].fillna(0, inplace = True)
 X['part_3_boolean'].fillna(0, inplace = True)
 X['part_4_boolean'].fillna(0, inplace = True)
 X['part_5_boolean'].fillna(0, inplace = True)
 X['part_6_boolean'].fillna(0, inplace = True)
 X['part_7_boolean'].fillna(0, inplace = True)
 X['type_of_concept_boolean'].fillna(0, inplace = True)
 X['type_of_intention_boolean'].fillna(0, inplace = True)
 X['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X['type_of_starter_boolean'].fillna(0, inplace = True )
mean_age_mistress=round(train[train.status=="Mistress"].Age.mean())
 mean_age_mistress<data_type_conversions>
X_val['answered_correctly_user'].fillna(0.65, inplace=True)
 X_val['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X_val['quest_pct'].fillna(content_mean, inplace=True)
 X_val['part'].fillna(4, inplace = True)
 X_val['avg_questions_seen'].fillna(1, inplace = True)
 X_val['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X_val['prior_question_had_explanation_enc'].fillna(0, inplace = True)
 X_val['part_1'].fillna(0, inplace = True)
 X_val['part_2'].fillna(0, inplace = True)
 X_val['part_3'].fillna(0, inplace = True)
 X_val['part_4'].fillna(0, inplace = True)
 X_val['part_5'].fillna(0, inplace = True)
 X_val['part_6'].fillna(0, inplace = True)
 X_val['part_7'].fillna(0, inplace = True)
 X_val['type_of_concept'].fillna(0, inplace = True)
 X_val['type_of_intention'].fillna(0, inplace = True)
 X_val['type_of_solving_question'].fillna(0, inplace = True)
 X_val['type_of_starter'].fillna(0, inplace = True)
 X_val['part_1_boolean'].fillna(0, inplace = True)
 X_val['part_2_boolean'].fillna(0, inplace = True)
 X_val['part_3_boolean'].fillna(0, inplace = True)
 X_val['part_4_boolean'].fillna(0, inplace = True)
 X_val['part_5_boolean'].fillna(0, inplace = True)
 X_val['part_6_boolean'].fillna(0, inplace = True)
 X_val['part_7_boolean'].fillna(0, inplace = True)
 X_val['type_of_concept_boolean'].fillna(0, inplace = True)
 X_val['type_of_intention_boolean'].fillna(0, inplace = True)
 X_val['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X_val['type_of_starter_boolean'].fillna(0, inplace = True )
train[(train.status=="Mistress")&(train.Age.isna())]=train[(train.status=="Mistress")&(train.Age.isna())].fillna(mean_age_mistress)
 train[train.status=="Mistress"].Age.isna().sum()<filter>
params = {
'objective': 'binary',
'max_bin': 800,
'learning_rate': 0.0374001767,
'boosting_type': 'gbdt',
'num_leaves': 199,
'max_depth': 112
}
print(params)
 lgb_train = lgb.Dataset(X, y, categorical_feature = ['part', 'prior_question_had_explanation_enc'])
 lgb_eval = lgb.Dataset(X_val, y_val, categorical_feature = ['part', 'prior_question_had_explanation_enc'], reference=lgb_train )
train[(train.status=="Mister")&(train.Age.isna())]<groupby>
model = lgb.train(
 params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=50,
num_boost_round=10000,
early_stopping_rounds=12
)
mean_age_mister=round(train[train.status=="Mister"].Age.mean())
 mean_age_mister<data_type_conversions>
y_pred = model.predict(X_val)
 y_true = np.array(y_val)
 roc_auc_score(y_true, y_pred )
train[(train.status=="Mister")&(train.Age.isna())]=train[(train.status=="Mister")&(train.Age.isna())].fillna(mean_age_mister)
 train[train.status=="Mister"].Age.isna().sum()<feature_engineering>
import matplotlib.pyplot as plt
import seaborn as sns
for i in range(888):
if train.loc[i,"Age"]<1:
train.loc[i,"age_category"]="Infants"
elif 1<=train.loc[i,"Age"]<18:
train.loc[i,"age_category"]="Children"
elif 18<=train.loc[i,"Age"]<65:
train.loc[i,"age_category"]="Adults"
elif train.loc[i,"Age"]>=65:
train.loc[i,"age_category"]="Elders"
else:
train.loc[i,"age_category"]="Unknown"<count_missing_values>
iter_test = env.iter_test()
print(train.Age.isna().sum())
 train[train.Age.isna() ]<groupby>
for(test_df, sample_prediction_df)in iter_test:
test_df['task_container_id'] = test_df.task_container_id.mask(test_df.task_container_id > 9999, 9999)
 test_df = pd.merge(test_df, group3, left_on=['task_container_id'], right_index= True, how="left")
 test_df = pd.merge(test_df, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 test_df = pd.merge(test_df, results_u_final, on=['user_id'], how="left")
 test_df = pd.merge(test_df, results_u2_final, on=['user_id'], how="left")
 test_df = pd.merge(test_df, user_lecture_stats_part, on=['user_id'], how="left")
 test_df['part_1'].fillna(0, inplace = True)
 test_df['part_2'].fillna(0, inplace = True)
 test_df['part_3'].fillna(0, inplace = True)
 test_df['part_4'].fillna(0, inplace = True)
 test_df['part_5'].fillna(0, inplace = True)
 test_df['part_6'].fillna(0, inplace = True)
 test_df['part_7'].fillna(0, inplace = True)
 test_df['type_of_concept'].fillna(0, inplace = True)
 test_df['type_of_intention'].fillna(0, inplace = True)
 test_df['type_of_solving_question'].fillna(0, inplace = True)
 test_df['type_of_starter'].fillna(0, inplace = True)
 test_df['part_1_boolean'].fillna(0, inplace = True)
 test_df['part_2_boolean'].fillna(0, inplace = True)
 test_df['part_3_boolean'].fillna(0, inplace = True)
 test_df['part_4_boolean'].fillna(0, inplace = True)
 test_df['part_5_boolean'].fillna(0, inplace = True)
 test_df['part_6_boolean'].fillna(0, inplace = True)
 test_df['part_7_boolean'].fillna(0, inplace = True)
 test_df['type_of_concept_boolean'].fillna(0, inplace = True)
 test_df['type_of_intention_boolean'].fillna(0, inplace = True)
 test_df['type_of_solving_question_boolean'].fillna(0, inplace = True)
 test_df['type_of_starter_boolean'].fillna(0, inplace = True)
 test_df['answered_correctly_user'].fillna(0.65, inplace=True)
 test_df['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 test_df['quest_pct'].fillna(content_mean, inplace=True)
 test_df['part'] = test_df.part - 1
test_df['part'].fillna(4, inplace = True)
 test_df['avg_questions_seen'].fillna(1, inplace = True)
 test_df['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 test_df['prior_question_had_explanation'].fillna(False, inplace=True)
 test_df["prior_question_had_explanation_enc"] = lb_make.fit_transform(test_df["prior_question_had_explanation"])
 test_df['answered_correctly'] = model.predict(test_df[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )