Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<train_model>
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1
ada_clf = AdaBoostClassifier()
ada_clf.fit(X_train, y_train)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=True)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=False )<save_to_csv>
np.seterr(divide = 'ignore', invalid = 'ignore' )
Y_pred = ada_clf.predict(test_df.drop('PassengerId',axis=1))
Y_pred
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submissions_ada.csv', index=False )<import_modules>
data_types_dict = {
'user_id': 'int32',
'timestamp': 'int64',
'content_id': 'int16',
'answered_correctly': 'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'bool'
}
target = 'answered_correctly'
from sklearn.ensemble import RandomForestClassifier<train_model>
train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv', columns = set(data_types_dict.keys())).to_pandas()
ada_clf = AdaBoostClassifier(RandomForestClassifier())
 ada_clf.fit(X_train, y_train)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=True)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=False)
 ada_clf = AdaBoostClassifier(base_estimator=RandomForestClassifier())
 ada_clf.fit(X_train, y_train)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=True)
 print_score(ada_clf, X_train, y_train, X_test, y_test, train=False )<save_to_csv>
print('Training dataset detailed information')
 print('*' * 50)
 print('Columns:', train_df.columns)
 print('*' * 50)
 print('Shape:', train_df.shape)
 print('*' * 50)
 print('NA values in each column:', sum(train_df.isna().sum()))
print('*' * 50 )
Y_pred = ada_clf.predict(test_df.drop('PassengerId',axis=1))
Y_pred
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submissions_ada_random.csv', index=False )<import_modules>
train_df = train_df[train_df[target] != -1].reset_index(drop = True, inplace = False)
 train_df['prior_question_had_explanation'].fillna(False, inplace = True)
 train_df = train_df.astype(data_types_dict)
from sklearn.ensemble import GradientBoostingClassifier<train_model>
prior_question_elapsed_time_mean = train_df.prior_question_elapsed_time.dropna().values.mean()
train_df['prior_question_elapsed_time_mean'] = train_df.prior_question_elapsed_time.fillna(prior_question_elapsed_time_mean )
gbc_clf = GradientBoostingClassifier()
gbc_clf.fit(X_train, y_train )<compute_test_metric>
train_df['lag'] = train_df.groupby('user_id')[target].shift()
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 train_df['user_correctness'] = cum['cumsum'] / cum['cumcount']
train_df.drop(columns = ['lag'], inplace = True)
print_score(gbc_clf, X_train, y_train, X_test, y_test, train=True )<compute_test_metric>
user_agg = train_df.groupby('user_id')[target].agg(['sum', 'count'])
 content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count'] )
print_score(gbc_clf, X_train, y_train, X_test, y_test, train=False )<save_to_csv>
train_df['Accuracy_sum'] = train_df['user_id'].map(user_agg['sum'])
 train_df['Questions_num'] = train_df['user_id'].map(user_agg['count'] )
Y_pred = gbc_clf.predict(test_df.drop('PassengerId',axis=1))
Y_pred
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submissions_gbc.csv', index=False )<import_modules>
train_df = train_df.groupby('user_id' ).tail(500 ).reset_index(drop = True )
import xgboost as xgb<choose_model_class>
questions_df = pd.read_csv(
 '.. /input/riiid-test-answer-prediction/questions.csv',
usecols = [0, 3],
dtype = {'question_id': 'int16', 'part': '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)
xgb_clf = xgb.XGBClassifier(max_depth=5, n_estimators=10000, learning_rate=0.3,
n_jobs=-1 )<train_model>
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'] )
xgb_clf.fit(X_train, y_train )<compute_test_metric>
train_df.prior_question_had_explanation=train_df.prior_question_had_explanation.astype('int8')
 train_df['lag'] = train_df.groupby('user_id')['prior_question_had_explanation'].shift()
print_score(xgb_clf, X_train, y_train, X_test, y_test, train=True )<compute_test_metric>
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 train_df['explanation_mean'] = cum['cumsum'] / cum['cumcount']
train_df['explanation_cumsum'] = cum['cumsum']
train_df.drop(columns=['lag'], inplace=True)
 train_df['explanation_mean'].fillna(0, inplace=True)
 train_df['explanation_cumsum'].fillna(0, inplace=True)
 train_df.explanation_mean=train_df.explanation_mean.astype('float16')
 train_df.explanation_cumsum=train_df.explanation_cumsum.astype('int16')
print_score(xgb_clf, X_train, y_train, X_test, y_test, train=False )<save_to_csv>
explanation_agg = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['sum', 'count'])
 explanation_agg = explanation_agg.astype('int16')
Y_pred = xgb_clf.predict(test_df.drop('PassengerId',axis=1))
Y_pred
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": Y_pred
})
 submission.to_csv('submissions_xgb.csv', index=False )<train_model>
max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']
train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
train_df['lagtime'].fillna(0, inplace=True)
 train_df.lagtime=train_df.lagtime.astype('int32')
 lagtime_agg = train_df.groupby('user_id')['lagtime'].agg(['mean'])
 train_df['lagtime_mean'] = train_df['user_id'].map(lagtime_agg['mean'])
 train_df.lagtime_mean=train_df.lagtime_mean.astype('int32')
 train_df['timestamp']=train_df['timestamp']/(1000*3600)
 train_df.timestamp=train_df.timestamp.astype('int16')
tree_clf = DecisionTreeClassifier()
tree_clf.fit(X_train, y_train)
 print_score(tree_clf, X_train, y_train, X_test, y_test, train=True)
 print_score(tree_clf, X_train, y_train, X_test, y_test, train=False )<train_model>
valid_df = train_df.groupby('user_id' ).tail(125)
 train_df.drop(valid_df.index, inplace = True )
rf_clf = RandomForestClassifier()
rf_clf.fit(X_train, y_train.ravel())
 print_score(rf_clf, X_train, y_train, X_test, y_test, train=True)
 print_score(rf_clf, X_train, y_train, X_test, y_test, train=False )<create_dataframe>
train_set = Pool(train_df[features], label = train_df[target])
 val_set = Pool(valid_df[features], label = valid_df[target] )
en_en = pd.DataFrame()<predict_on_test>
model = CatBoostClassifier(**params)
 model.fit(train_set, eval_set = val_set, use_best_model = True )
tree_clf.predict_proba(X_train )<prepare_output>
user_sum_dict = user_agg['sum'].astype('int16' ).to_dict(defaultdict(int))
user_count_dict = user_agg['count'].astype('int16' ).to_dict(defaultdict(int))
content_sum_dict = content_agg['sum'].astype('int32' ).to_dict(defaultdict(int))
content_count_dict = content_agg['count'].astype('int32' ).to_dict(defaultdict(int))
explanation_sum_dict = explanation_agg['sum'].astype('int16' ).to_dict(defaultdict(int))
explanation_count_dict = explanation_agg['count'].astype('int16' ).to_dict(defaultdict(int))
lagtime_mean_dict = lagtime_agg['mean'].astype('int32' ).to_dict(defaultdict(int))
max_timestamp_u_dict = max_timestamp_u.set_index('user_id' ).to_dict()
en_en['tree_clf'] = pd.DataFrame(tree_clf.predict_proba(X_train)) [1]
en_en['rf_clf'] = pd.DataFrame(rf_clf.predict_proba(X_train)) [1]
col_name = en_en.columns
en_en = pd.concat([en_en, pd.DataFrame(y_train ).reset_index(drop=True)], axis=1 )<rename_columns>
try:
env = riiideducation.make_env()
except:
pass
iter_test = env.iter_test()
prior_test_df = None
<train_model><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):
user_sum_dict[user_id] += answered_correctly
user_count_dict[user_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')
 test_df['prior_question_had_explanation'] = test_df['prior_question_had_explanation'].fillna(False ).astype('bool')
 test_df['prior_question_elapsed_time_mean'] = test_df.prior_question_elapsed_time.fillna(prior_question_elapsed_time_mean)
 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)
 explanation_sum = np.zeros(len(test_df), dtype=np.int32)
 explanation_count = np.zeros(len(test_df), dtype=np.int32)
 lagtime = np.zeros(len(test_df), dtype=np.int32)
 lagtime_mean = np.zeros(len(test_df), dtype=np.int32)
 for i,(user_id, content_id,timestamp)in enumerate(zip(test_df['user_id'].values, test_df['content_id'].values,test_df['timestamp'].values)) :
user_sum[i] = user_sum_dict[user_id]
user_count[i] = user_count_dict[user_id]
content_sum[i] = content_sum_dict[content_id]
content_count[i] = content_count_dict[content_id]
explanation_sum[i] = explanation_sum_dict[user_id]
explanation_count[i] = explanation_count_dict[user_id]
if user_id in max_timestamp_u_dict['max_time_stamp'].keys() :
lagtime[i] = timestamp-max_timestamp_u_dict['max_time_stamp'][user_id]
max_timestamp_u_dict['max_time_stamp'][user_id]=timestamp
lagtime_mean[i] =(lagtime_mean_dict[user_id]+lagtime[i])/2
else:
lagtime[i]=0
max_timestamp_u_dict['max_time_stamp'].update({user_id:timestamp})
 lagtime_mean_dict.update({user_id:timestamp})
 lagtime_mean[i]=(lagtime_mean_dict[user_id]+lagtime[i])/2
test_df['user_correctness'] = user_sum / user_count
test_df['Accuracy_sum'] = user_sum
test_df['Questions_num'] = user_count
test_df['content_count'] = content_count
test_df['content_id'] = content_sum / content_count
test_df['explanation_mean'] = explanation_sum / explanation_count
test_df['explanation_cumsum'] = explanation_sum
test_df["lagtime"] = lagtime
test_df["lagtime_mean"] = lagtime_mean
test_df['timestamp']=test_df['timestamp']/(1000*3600)
 test_df.timestamp=test_df.timestamp.astype('int16')
 test_df[target] = model.predict_proba(test_df[features])[:,1]
env.predict(test_df[['row_id', target]] )