Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<import_modules>
env = riiideducation.make_env()
<train_model>
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'}
)
<save_to_csv>
questions_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 3],
dtype={'question_id': 'int16',
'part': 'int8'}
)
predicted_test = []
for x in exTreeClf.predict(testing_set_dummy):
predicted_test.append(x)
 predicted_test_value = pd.DataFrame({ 'PassengerId': pID,
'Survived': predicted_test })
 predicted_test_value.to_csv("PredictedTestScore.csv", index=False )<create_dataframe>
train = train[train.content_type_id == False].sort_values('timestamp' ).reset_index(drop = True )
<set_options>
train[(train.content_type_id == False)].task_container_id.nunique()
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None)
 pd.set_option('display.width', 1000)
<load_from_csv>
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 = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 dataset = [train, test]
print('Entries in training set: ', len(train), '
Entries in testing set: ',len(test))
for df in dataset:
print(df.isna().sum())
 train_test_comb = pd.concat([train, test], axis=0 )<feature_engineering>
group3['avg_questions_seen'] = group3.avg_questions.cumsum()
for df in dataset:
df['Familysize'] = df['SibSp']+df['Parch']
df['Title'] = df['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
title_names =(df['Title'].value_counts() > 10)
 df['Title'] = df['Title'].apply(lambda x: x if title_names.loc[x] == True else 'Misc')
 train_test_comb = pd.concat([train, test], axis=0)
 print(train_test_comb['Title'].value_counts() )<feature_engineering>
group3.iloc[0].avg_questions_seen
for df in dataset:
df['Fare_cat'] = pd.qcut(df['Fare'], q=4, labels=(1,2,3,4))
df['Age_cat'] = pd.qcut(df['Age'], q=4, labels=(1,2,3,4))
df['Familysize'] = df['Familysize'].apply(lambda x: 'Alone' if x==0 else('Small' if x>0 and x<5 else('Medium' if x>=5 and x<7 else 'Large')) )<categorify>
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']
for df in dataset:
df['Age_cat'] = df['Age_cat'].astype(np.int32)
 df['Fare_cat'] = df['Fare_cat'].astype(np.int32)
 df.Title.replace({'Mr':1, 'Mrs':2, 'Miss':3, 'Master':4, 'Misc':5}, inplace=True)
 df.Sex.replace({'female':0, 'male': 1}, inplace=True)
 df.Embarked.replace({'S':1, 'C':2, 'Q':3}, inplace=True )<categorify>
train = pd.merge(train, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left' )
features = ['Age_cat', 'Fare_cat', 'Pclass', 'Sex', 'Embarked', 'Title', 'Familysize']
encoded_fearures = []
for df in dataset:
for feature in features:
encoded = OneHotEncoder().fit_transform(df[feature].values.reshape(-1, 1)).toarray()
n = df[feature].nunique()
cols = [f'{feature}_{n}' for n in range(1, n + 1)]
encoded_df = pd.DataFrame(encoded, columns=cols)
 encoded_df.index = df.index
encoded_fearures.append(encoded_df)
 train_one = pd.concat([train, *encoded_fearures[:7]], axis=1)
 test_one = pd.concat([test, *encoded_fearures[7:]], axis=1)
 dataset = [train_one, test_one]<drop_column>
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']
for df in dataset:
df.drop(['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch',
'Ticket', 'Fare', 'Embarked', 'Familysize', 'Title', 'Fare_cat', 'Age_cat' ], axis=1, inplace=True )<split>
results_q2_final = train.loc[train.content_type_id == False, ['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['count']
features = [x for x in train_one.columns if x!='Survived']
x = train_one[features].to_numpy()
y = train_one['Survived'].to_numpy()
x_train, x_val, y_train, y_val = train_test_split(x, y, train_size = int(0.95*len(train_one)) , shuffle=False ,random_state=1400)
 print(x_train.shape, y_train.shape, x_val.shape, y_val.shape )<choose_model_class>
question2 = pd.merge(questions_df, results_q_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
clf = RandomForestClassifier(criterion='gini',
n_estimators=300,
max_depth=4,
min_samples_split=4,
min_samples_leaf=7,
max_features='auto',
oob_score=True,
random_state=1400,
n_jobs=-1)
 clf.fit(x_train, y_train)
 y_pred = clf.predict(x_val)
 cm = confusion_matrix(y_val, y_pred)
 print(cm)
 print(classification_report(y_val, y_pred))<save_to_csv>
question2 = pd.merge(question2, results_q2_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
test_data = test_one[features].to_numpy()
prediction_clf = clf.predict(test_data)
 print(len(prediction_clf))
output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': prediction_clf})
 output.to_csv('/kaggle/working/my_submission.csv', index=False )<choose_model_class>
question2.quest_pct = round(question2.quest_pct,5 )
seed = 1400
tf.random.set_seed(seed)
 my_init = keras.initializers.glorot_uniform(seed=seed)
 model = keras.models.Sequential()
model.add(keras.layers.Input(shape=(x_train.shape[1],)))
 model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dropout(0.2))
model.add(keras.layers.Dense(360, activation='selu', kernel_initializer=my_init))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam', loss = keras.losses.BinaryCrossentropy() , metrics=['accuracy'])
 early_stopping = keras.callbacks.EarlyStopping(monitor='accuracy', patience=3, mode='max', restore_best_weights=True)
 reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='accuracy', factor=0.1, patience=3, mode='max', min_lr=0)
 model.fit(x_train, y_train, epochs = 50, batch_size = 2, callbacks=[reduce_lr, early_stopping], verbose = 1)
 val_loss, val_acc = model.evaluate(x_val, y_val, verbose=1)
 print('
Validation accuracy:', val_acc )<save_to_csv>
train.drop(['timestamp', 'content_type_id', 'question_id', 'part'], axis=1, inplace=True )
target_col =[]
test_data = test_one[features].to_numpy()
prediction_nn = model.predict(test_data)
 for i in prediction_nn:
target_col.append(int(round(i[0])))
 output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': target_col})
 output.to_csv('my_submission.csv', index=False )<set_options>
validation = train.groupby('user_id' ).tail(5)
 train = train[~train.index.isin(validation.index)]
len(train)+ len(validation )
%matplotlib inline
plt.style.use("ggplot")
 warnings.simplefilter("ignore" )<load_from_csv>
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']
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_train.head()<load_from_csv>
X = train.groupby('user_id' ).tail(18)
 train = train[~train.index.isin(X.index)]
len(X)+ len(train)+ len(validation )
df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df_test.head()<count_missing_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']
df_train['Age'].isna().sum()<feature_engineering>
del(train )
df_train['Age'] = df_train['Age'].fillna(0)
 df_train['Age'].isna().sum()<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" )
df_train.drop(columns = ['Name','Ticket' ,'Fare' , 'Cabin'] , inplace= True )<categorify>
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" )
cat_vars=['Sex' , 'Embarked']
for var in cat_vars:
cat_list='var'+'_'+var
cat_list = pd.get_dummies(df_train[var], prefix=var)
 df_train1=df_train.join(cat_list)
 df_train=df_train1<drop_column>
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"] )
df_train.drop(columns = ['Sex','Embarked'] , inplace= True)
 df_train.head(5 )<define_variables>
s=0
d=0
for i in df_train['Survived']:
if i==0:
d+=1
else:
s+=1
print('Dead - ',d,' Survived -',s )<prepare_x_and_y>
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 )
X = df_train.loc[:, df_train.columns != 'Survived']
Y = df_train.Survived<feature_engineering>
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
df_test['Age'] = df_test['Age'].fillna(0)
 df_test.info()<categorify>
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 )
cat_vars=['Sex' , 'Embarked']
for var in cat_vars:
cat_list='var'+'_'+var
cat_list = pd.get_dummies(df_test[var], prefix=var)
 df_test1=df_test.join(cat_list)
 df_test=df_test1<import_modules>
X = X[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part']]
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']]
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import export_graphviz
from IPython.display import SVG
from graphviz import Source
from IPython.display import display
from ipywidgets import interactive, IntSlider, FloatSlider, interact
import ipywidgets
from IPython.display import Image
from subprocess import call
import matplotlib.image as mpimg<save_to_csv>
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)
estimator = plot_tree_rf(crit='gini', bootstrap='False' , depth =16 , forests=100 , min_split=3 , min_leaf= 3)
 y_pred_rf = estimator.predict(df_test)
 print('len',len(y_pred_rf))
sub = pd.DataFrame(columns=['PassengerId' , 'Survived'])
 sub['PassengerId'] = df_test['PassengerId'].astype(int)
 sub['Survived'] = y_pred_rf.astype(int)
 sub.to_csv('sub_rf.csv', index=False )<set_options>
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['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 )
%matplotlib inline
sns.set()
warnings.filterwarnings("ignore" )<load_from_csv>
y_true = np.array(y_val)
 lgb_train = lgb.Dataset(X, y, categorical_feature = ['part', 'prior_question_had_explanation_enc'],free_raw_data=False)
 lgb_eval = lgb.Dataset(X_val, y_val, categorical_feature = ['part', 'prior_question_had_explanation_enc'], reference=lgb_train, free_raw_data=False )
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv' )<count_missing_values>
def objective(trial):
params = {
'num_leaves': trial.suggest_int('num_leaves', 32, 512),
'boosting_type': 'gbdt',
'max_bin': trial.suggest_int('max_bin', 700, 900),
'objective': 'binary',
'metric': 'auc',
'max_depth': trial.suggest_int('max_depth', 4, 16),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 16),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.4, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.4, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 1, 8),
'min_child_samples': trial.suggest_int('min_child_samples', 4, 80),
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-8, 1.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-8, 1.0),
'early_stopping_rounds': 10
}
model = lgb.train(params, lgb_train, valid_sets=[lgb_train, lgb_eval], verbose_eval=1000, num_boost_round=1300)
 val_pred = model.predict(X_val)
 score = roc_auc_score(y_true, val_pred)
 print(f"AUC = {score}")
 return score
train.isnull().sum()<count_missing_values>
import optuna
test.isnull().sum()<count_missing_values>
study = optuna.create_study(direction='maximize')
 study.optimize(objective, n_trials=5 )
test.isnull().sum()<filter>
print('Number of finished trials: {}'.format(len(study.trials)))
 print('Best trial:')
 trial = study.best_trial
print(' Value: {}'.format(trial.value))
print(' Params: ')
 for key, value in trial.params.items() :
print(' {}: {}'.format(key, value))
survived = train[train['Survived'] == 1]
not_survived = train[train['Survived'] == 0]
print("Survived: %i(%.1f%%)"%(len(survived), float(len(survived)) /len(train)*100.0))
print("Not Survived: %i(%.1f%%)"%(len(not_survived), float(len(not_survived)) /len(train)*100.0))
print("Total: %i"%len(train))<count_values>
model = lgb.train(trial.params, lgb_train, valid_sets=[lgb_train, lgb_eval], verbose_eval=1000,num_boost_round=1300 )
train.Pclass.value_counts()<groupby>
y_pred = model.predict(X_val)
 roc_auc_score(y_true, y_pred )
pclass_survived = train.groupby('Pclass' ).Survived.value_counts()
pclass_survived<groupby>
import matplotlib.pyplot as plt
import seaborn as sns
pclass_survived_average = train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()
pclass_survived_average<count_values>
iter_test = env.iter_test()
train.Sex.value_counts()<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['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']])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )