Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<train_on_grid>
from sklearn.metrics import roc_auc_score
from fastai.tabular.all import *
import gc
import fastai
from tqdm.notebook import tqdm as tqdm
tune_hyperparameters(KNN, param_grid=knn_params)
 knn_best_params, knn_best_score = best_params,best_scores
print('KNN Best Score:', knn_best_score)
 print('And Best Parameters:', knn_best_params )<train_on_grid>
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 seed_everything(42 )
tune_hyperparameters(DT, param_grid=dt_params)
 dt_best_params, dt_best_score = best_params,best_scores
print('DT Best Score:', dt_best_score)
 print('And Best Parameters:', dt_best_params )<train_on_grid>
%%time
train_df = pd.read_feather('/kaggle/input/riid-with-attempts-feather/train_with_attempts')
 train_df.drop('timestamp', axis=1, inplace=True)
 train_df.info()
tune_hyperparameters(RF, param_grid=rf_params)
 rf_best_params, rf_best_score = best_params,best_scores
print('RF Best Score:', rf_best_score)
 print('And Best Parameters:', rf_best_params )<train_on_grid>
target = 'answered_correctly'
tune_hyperparameters(XGB, param_grid=xgb_params)
 xgb_best_params, xgb_best_score = best_params,best_scores
print('SVC Best Score:', xgb_best_score)
 print('And Best Parameters:', xgb_best_params )<train_on_grid>
content_agg = pd.read_feather('/kaggle/input/riid-with-attempts-feather/content_agg_feather')
 content_agg.info()
tune_hyperparameters(SVM, param_grid=svc_params)
 svc_best_params, svc_best_score = best_params,best_scores
print('SVC Best Score:', svc_best_score)
 print('And Best Parameters:', svc_best_params )<choose_model_class>
%%time
questions_df = pd.read_csv(
 '.. /input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 1, 3, 4],
dtype={'question_id': 'int16', 'bundle_id': 'int16', 'part': 'int8', 'tags': 'object'} 
)
 tags = questions_df['tags'].str.split(' ', n=10, expand=True)
 tags.columns = ['tags1', 'tags2', 'tags3', 'tags4', 'tags5', 'tags6']
questions_df.drop(['tags'], axis=1, inplace=True)
 tags.drop(['tags1', 'tags4', 'tags5', 'tags6'], axis=1, inplace=True)
 questions_df = pd.concat([questions_df, tags], axis=1)
 questions_df['tags2'] = pd.to_numeric(questions_df['tags2'], errors='coerce',downcast='integer' ).fillna(-1)
 questions_df['tags3'] = pd.to_numeric(questions_df['tags3'], errors='coerce',downcast='integer' ).fillna(-1)
 questions_df['tags2'] = questions_df['tags2'].astype('uint16')
 questions_df['tags3'] = questions_df['tags3'].astype('uint16')
 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)
 del tags
gc.collect()
train_df.info()
lr = LogisticRegression(**lr_best_params)
 knn = KNeighborsClassifier(**knn_best_params)
 dt = DecisionTreeClassifier(**dt_best_params)
 rf = RandomForestClassifier(**rf_best_params)
 svc = SVC(**svc_best_params)
 xgb = XGBClassifier(**xgb_best_params )<find_best_model_class>
cat = ['part', 'tags2', 'tags3', 'bundle_id']
cont = ['user_count', 'user_correctness', 'content_count', 'content_correctness', 'prior_question_elapsed_time',
'attempts', 'hmean_user_content']
features = cat + cont
models = {'LR':lr,'KNN':knn,'DT':dt,'RF':rf,'SVC':svc, 'XGB':xgb}
score = []
for x,(keys, items)in enumerate(models.items()):
items.fit(X_train,y_train)
 scores = cross_val_score(items, X_train, y_train, cv = 10, scoring='accuracy')*100
score.append(scores.mean())
 print('Mean Accuracy: %0.4f(+/- %0.4f)[%s]' %(scores.mean() , scores.std() , keys)) 
<predict_on_test>
def replace_na(x):
x = x.replace([np.inf, -np.inf], np.nan)
 x['tags2'].fillna(255, inplace=True)
 x['tags3'].fillna(255, inplace=True)
 return x.fillna(0 )
model_prediction = pd.DataFrame({'LR':lr.predict(X_test), 'KNN':knn.predict(X_test), 'DT':dt.predict(X_test),'RF':rf.predict(X_test), 'SVC':svc.predict(X_test), 'XGB':xgb.predict(X_test)})
 model_prediction.head()<save_to_csv>
thresh = int(0.75 * len(train_df))
train_df = train_df.iloc[thresh:].reset_index(drop=True)
 train_range = list(range(len(train_df)- int(len(train_df)/ 10)))
 valid_range = list(range(len(train_df)- int(len(train_df)/ 10), len(train_df)))
 splits =(train_range, valid_range)
 gc.collect()
train_df.info()
submisson = pd.DataFrame({"PassengerID":test["PassengerId"],"Survived":rf.predict(X_test)})
 submisson.to_csv('submisson_rf.csv',index=False )<save_to_csv>
%%time
EPOCHS=5
BATCH_SIZE=2048
pd.options.mode.chained_assignment=None
to = TabularPandas(train_df, [Categorify, Normalize, FillMissing], cat, cont, target, splits=splits, inplace=True)
 dls = to.dataloaders(BATCH_SIZE )
submission = pd.DataFrame({'PassengerId':test['PassengerId'],'Survived':svc.predict(X_test)})
 submission.to_csv("submission_svc.csv", index = False )<save_to_csv>
def my_auc(inp, targ):
"Simple wrapper around scikit's roc_auc_score function for regression problems"
inp,targ = flatten_check(inp,targ)
 return roc_auc_score(targ.cpu().numpy() , inp.cpu().numpy())
 learn = tabular_learner(dls, layers=[200,200], metrics=my_auc)
 lr_find_res = learn.lr_find()
learn.fit_one_cycle(EPOCHS, lr_find_res.lr_min / 10 )
submission = pd.DataFrame({'PassengerId':test['PassengerId'],'Survived':xgb.predict(X_test)})
 submission.to_csv("submission_xgb.csv", index = False )<set_options>
def predict_batch(self, df):
dl = self.dls.test_dl(df)
 dl.dataset.conts = dl.dataset.conts.astype(np.float32)
 inp,preds,_,dec_preds = self.get_preds(dl=dl, with_input=True, with_decoded=True)
 return preds.numpy()
setattr(learn, 'predict_batch', predict_batch )
plt.style.use('ggplot')
 warnings.simplefilter(action='ignore', category=FutureWarning )<load_from_csv>
del dls
del to
del train_df
gc.collect()
dataset = pd.read_csv('.. /input/titanic/train.csv')
 dataset.head()<count_missing_values>
%%time
train_df = pd.read_feather('/kaggle/input/riid-with-attempts-feather/train_with_attempts')
 train_df = train_df.loc[train_df['answered_correctly'] != -1]
train_df.sort_values('timestamp', inplace=True)
 train_df.drop(['prior_question_had_explanation', 'prior_question_elapsed_time', 'timestamp', 'attempts',
'user_correctness', 'user_count', 'content_correctness', 'content_count', 'hmean_user_content'], axis=1, inplace=True)
 train_df.reset_index(drop=True, inplace=True)
 gc.collect()
train_df.info()
dataset.isnull().sum(axis=0 )<create_dataframe>
%%time
def user_states(df):
mean_user_accuracy = train_df.groupby('user_id')[target].mean().values
answered_correctly_user = train_df.groupby('user_id')[target].sum().values
answered_user = train_df.groupby('user_id')[target].count().values
state = dict()
for user_id in train_df['user_id'].sort_values().unique() :
state[user_id] = {}
total = len(state.keys())
 user_content = train_df.groupby('user_id')['content_id'].apply(np.array ).apply(np.sort ).apply(np.unique)
 user_attempts = train_df.groupby(['user_id', 'content_id'])[target].count().astype(np.uint8 ).groupby('user_id' ).apply(np.array ).values
for user_id, content, attempt in tqdm(zip(state.keys() , user_content, user_attempts),total=total):
state[user_id]['user_content_attempts'] = dict(zip(content, attempt))
del user_content, user_attempts
gc.collect()
for idx, user_id in enumerate(state.keys()):
state[user_id]['user_correctness'] = mean_user_accuracy[idx]
state[user_id]['user_sum'] = answered_correctly_user[idx]
state[user_id]['user_count'] = answered_user[idx]
return state
user_states = user_states(train_df)
 del train_df
gc.collect()
dataset = dataset.fillna({"Embarked": "S"} )<categorify>
env = riiideducation.make_env()
iter_test = env.iter_test()
prior_test_df = None
dataset = pd.get_dummies(dataset, columns=['Sex'])
 dataset.head()<categorify>
content_sum_dict = content_agg['sum'].astype('int32' ).to_dict(defaultdict(int))
content_count_dict = content_agg['count'].astype('int32' ).to_dict(defaultdict(int))
<define_variables><EOS> 
for(test_df, sample_prediction_df)in iter_test:
y_preds = []
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):
user_states[user_id]['user_sum'] += answered_correctly
user_states[user_id]['user_count'] += 1
user_states[user_id]['user_content_attempts'][content_id] += 1
content_sum_dict[content_id] += answered_correctly
content_count_dict[content_id] += 1
prior_test_df = test_df.copy()
test_df = test_df[test_df['content_type_id'] == 0].reset_index(drop=True)
 test_df = pd.merge(test_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 user_sum = np.zeros(len(test_df), dtype=np.int16)
 user_count = np.zeros(len(test_df), dtype=np.int16)
 content_sum = np.zeros(len(test_df), dtype=np.int32)
 content_count = np.zeros(len(test_df), dtype=np.int32)
 user_content_attempts = np.zeros(len(test_df), dtype=np.int8)
 for i,(user_id, content_id)in enumerate(zip(test_df['user_id'].values, test_df['content_id'].values)) :
if user_id not in user_states.keys() :
user_states[user_id] = {'user_content_attempts':{user_id:0},
'user_correctness':0,
'user_count':0,
'user_sum':0}
if content_id not in user_states[user_id]['user_content_attempts'].keys() :
user_states[user_id]['user_content_attempts'][content_id] = 0
user_sum[i] = user_states[user_id]['user_sum']
user_count[i] = user_states[user_id]['user_count']
user_content_attempts[i] = user_states[user_id]['user_content_attempts'][content_id]
content_sum[i] = content_sum_dict[content_id]
content_count[i] = content_count_dict[content_id]
test_df['user_correctness'] = user_sum / user_count
test_df['user_count'] = user_count
test_df['content_count'] = content_count
test_df['content_correctness'] = content_sum / content_count
test_df['attempts'] = user_content_attempts
test_df['hmean_user_content'] = 2 *(
(test_df['user_correctness'] * test_df['content_correctness'])/ 
(test_df['user_correctness'] + test_df['content_correctness'])
 ).astype('float32')
 test_df = replace_na(test_df)
 test_df[target] = learn.predict_batch(learn, test_df[features])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )