Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<choose_model_class>
import optuna
import lightgbm as lgb
import pickle
import riiideducation
import dask.dataframe as dd
import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score
model = Sequential()
model.add(Conv2D(150 ,(3,3), strides = 1 , padding = 'same' , activation = 'relu' , input_shape =(28,28,1)))
 model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(100 ,(3,3), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.1))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(75 ,(5,5), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.2))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(50 ,(4,4), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(Dropout(0.15))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Conv2D(25 ,(4,4), strides = 1 , padding = 'same' , activation = 'relu'))
model.add(MaxPool2D(( 2,2), strides = 2 , padding = 'same'))
model.add(Flatten())
 model.add(Dense(units = 512 , activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(units = 10 , activation = 'softmax'))
model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = ['accuracy'])
 model.summary()<train_model>
train = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/train.csv',
usecols=[1, 2, 3,4,7,8,9], dtype={'timestamp': 'int64', 'user_id': 'int32' ,'content_id': 'int16','content_type_id': 'int8','answered_correctly':'int8','prior_question_elapsed_time': 'float32','prior_question_had_explanation': 'boolean'} 
)
 train = train[train.content_type_id == False]
train = train.sort_values(['timestamp'], ascending=True)
 train.drop(['timestamp','content_type_id'], axis=1, inplace=True)
 results_c = train[['content_id','answered_correctly']].groupby(['content_id'] ).agg(['mean'])
 results_c.columns = ["answered_correctly_content"]
results_u = train[['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean', 'sum'])
 results_u.columns = ["answered_correctly_user", 'sum']
history = model.fit(datagen.flow(x_train,y_train, batch_size = 256),epochs = 30 , validation_data =(x_val, y_val))<save_to_csv>
questions_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0,1, 3,4],
dtype={'question_id': 'int16',
'part': 'int8','bundle_id': 'int8','tags': 'str'} 
)
 tag = questions_df["tags"].str.split(" ", n = 10, expand = True)
 tag.columns = ['tags1','tags2','tags3','tags4','tags5','tags6']
questions_df = pd.concat([questions_df,tag],axis=1)
 questions_df['tags1'] = pd.to_numeric(questions_df['tags1'], errors='coerce')
 questions_df['tags2'] = pd.to_numeric(questions_df['tags2'], errors='coerce')
 questions_df['tags3'] = pd.to_numeric(questions_df['tags3'], errors='coerce')
 questions_df['tags4'] = pd.to_numeric(questions_df['tags4'], errors='coerce')
 questions_df['tags5'] = pd.to_numeric(questions_df['tags5'], errors='coerce')
 questions_df['tags6'] = pd.to_numeric(questions_df['tags6'], errors='coerce' )
y_pred=model.predict_classes(x_test)
 submission.head()
submission=pd.DataFrame({'ImageId': submission.ImageId,'Label':y_pred})
 submission.to_csv('/kaggle/working/submission.csv',index=False)
 check=pd.read_csv('/kaggle/working/submission.csv')
 check<set_options>
lb_make = LabelEncoder()
train['prior_question_had_explanation'].fillna(False, inplace=True)
 train["prior_question_had_explanation_enc"] = lb_make.fit_transform(train["prior_question_had_explanation"] )
np.random.seed(2)
 %matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
file = '.. /input/riiid-lgb-training-and-save-model/trained_model.pkl'
model = pickle.load(open(file, 'rb'))
print('Trained LGB model was loaded!' )
train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test=pd.read_csv('.. /input/digit-recognizer/test.csv')
 print(train.shape)
 print(test.shape)
<train_model>
env = riiideducation.make_env()
<categorify><EOS> 
iter_test = env.iter_test()
for(test_df, sample_prediction_df)in iter_test:
test_df = test_df.sort_values(['user_id','timestamp'], ascending=False)
 test_df['answer_time'] = test_df.groupby(['user_id'])['prior_question_elapsed_time'].shift(1)
 test_df = pd.merge(test_df, results_u, on=['user_id'], how="left")
 test_df = pd.merge(test_df, results_c, on=['content_id'], how="left")
 test_df = pd.merge(test_df, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left')
 test_df['answered_correctly_user'].fillna(0.5, inplace=True)
 test_df['answered_correctly_content'].fillna(0.5, inplace=True)
 test_df['sum'].fillna(0, inplace=True)
 test_df['prior_question_had_explanation'].fillna(False, inplace=True)
 test_df["prior_question_had_explanation_enc"] = lb_make.transform(test_df["prior_question_had_explanation"])
 test_df['answered_correctly'] = model.predict(test_df[['answered_correctly_user', 'answered_correctly_content', 'sum','bundle_id','part','prior_question_elapsed_time','prior_question_had_explanation_enc',
'tags1','tags2','tags3']])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )