Kaggle competition: Riiid Answer Correctness Prediction
mean_age_maleAdult=round(train[(train["age_category"]=="Adults")&(train.Sex=="male")].Age.mean())
 mean_age_maleAdult<groupby>
warnings.filterwarnings("ignore")
 _ = np.seterr(divide='ignore', invalid='ignore' )
mean_age_femaleAdult=round(train[(train["age_category"]=="Adults")&(train.Sex=="female")].Age.mean())
 print(mean_age_femaleAdult)
 mean_age_femaleChildren=round(train[(train["age_category"]=="Children")&(train.Sex=="female")].Age.mean())
 print(mean_age_femaleChildren )<filter>
data_path = ".. /input/riiid-test-answer-prediction/train.csv"
questions_path = ".. /input/riiid-test-answer-prediction/questions.csv"
train[(train.Age.isna())&(train.Parch==0)]=train[(train.Age.isna())&(train.Parch==0)].fillna(mean_age_femaleAdult)
 train[train.Age.isna() ]<filter>
%%time
dtype = {
"row_id": "int64",
"timestamp": "int64",
"user_id": "int32",
"content_id": "int16",
"content_type_id": "boolean",
"task_container_id": "int16",
"user_answer": "int8",
"answered_correctly": "int8",
"prior_question_elapsed_time": "float32",
"prior_question_had_explanation": 'boolean'
}
train = pd.read_parquet(".. /input/riiid-parquet-files/train.parquet")
 train = train[dtype.keys() ]
train = train.astype(dtype)
 train = train[train['answered_correctly']!=-1]
train['prior_question_had_explanation'].fillna(False, inplace=True)
 train['prior_question_had_explanation'] = train['prior_question_had_explanation'].astype(bool)
 train = train[['user_id','content_id','answered_correctly',
'prior_question_elapsed_time', 'prior_question_had_explanation']]
train.info()
train[train.Age.isna() ]=train[train.Age.isna() ].fillna(mean_age_femaleChildren )<categorify>
TARGET = "answered_correctly"
TIME_MEAN = 21000.0
TIME_MIN = 0.0
TIME_MAX = 300000.0
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"<count_missing_values>
dtype={'question_id':'int16','part':'int8','bundle_id':'int8', 'tags':'str'}
questions = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=dtype.keys() ,
dtype=dtype)
 questions['tags'].fillna('None', inplace=True)
 questions['num_tags'] = questions['tags'].apply(lambda x:len(x.split())if pd.notna(x)else 0)
 questions['tags'].fillna('None', inplace=True)
 le = LabelEncoder()
questions['tags_label'] = le.fit_transform(questions['tags'].values)
 questions = questions[['question_id','part','tags_label']]
questions.isnull().sum()
train.isna().sum()<filter>
def preprocess(df):
df = df[df[TARGET] != -1].reset_index(drop=True)
 df = df.sort_values(['timestamp'], ascending=True ).reset_index(drop = True)
 df = df.merge(questions, left_on='content_id', right_on='question_id', how='left')
 df.drop(columns=['question_id'], inplace=True)
 df["prior_question_had_explanation"].fillna(False, inplace=True)
 df["prior_question_elapsed_time"] = df["prior_question_elapsed_time"].fillna(TIME_MEAN)
 df['lag'] = df.groupby('user_id')[TARGET].shift()
cum = df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 df['user_correctness'] = cum['cumsum'] / cum['cumcount']
df.drop(columns=['lag'], inplace=True)
 user_agg = df.groupby('user_id')[TARGET].agg(['sum', 'count'])
 content_agg = df.groupby('content_id')[TARGET].agg(['sum', 'count'])
 df['content_count'] = df['content_id'].map(content_agg['count'] ).astype('int32')
 df['content_mean'] = df['content_id'].map(content_agg['sum'] / content_agg['count'])
 return df
train[train.Embarked.isna() ]<count_missing_values>
%%time
train['lag'] = train.groupby('user_id')[TARGET].shift()
cum = train.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 train['user_mean'] = cum['cumsum'] / cum['cumcount']
train.drop(columns=['lag'], inplace=True)
 user_agg = train.groupby('user_id')[TARGET].agg(['sum', 'count'])
 content_agg = train.groupby('content_id')[TARGET].agg(['sum', 'count'])
 train_df = train.groupby('user_id' ).tail(24 ).reset_index(drop=True)
 train_df = pd.merge(train_df, questions, left_on='content_id', right_on='question_id', how='left')
 train_df.drop(columns=['question_id'], inplace=True)
 train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_mean'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'])
 valid_df = train_df.groupby('user_id' ).tail(6)
 train_df.drop(valid_df.index, inplace=True)
 del train
train.fillna("S",inplace=True)
 train.isna().sum()<feature_engineering>
FE = ['content_mean','content_count','user_mean',
'prior_question_elapsed_time','prior_question_had_explanation',
'part','tags_label']
CF = ['prior_question_had_explanation','part']
test.drop("Ticket",axis=1,inplace=True)
 test["age_category"]=""
for i in range(418):
if test.loc[i,"Age"]<1:
test.loc[i,"age_category"]="Infants"
elif 1<=test.loc[i,"Age"]<18:
test.loc[i,"age_category"]="Children"
elif 18<=train.loc[i,"Age"]<65:
test.loc[i,"age_category"]="Adults"
elif test.loc[i,"Age"]>=65:
test.loc[i,"age_category"]="Elders"
else:
test.loc[i,"age_category"]="Unknown"
test["status"]=""
for i in range(418):
if "Mr." in(test.Name[i].split()):
test.loc[i,"status"]="Mister"
elif "Miss." in(test.Name[i].split()):
test.loc[i,"status"]="Miss"
elif "Mrs." in(test.Name[i].split()):
test.loc[i,"status"]="Mistress"
elif "Rev." in(test.Name[i].split()):
test.loc[i,"status"]="Reverend"
elif "Master." in(test.Name[i].split()):
test.loc[i,"status"]="Master"
elif "Dr." in(test.Name[i].split()):
test.loc[i,"status"]="Doctor"
else:
test.loc[i,"status"]="Other"
mean_age_children=round(test[(test["age_category"]=="Children")|(test["age_category"]=="Infants")].Age.mean())
 test[(test.status=="Master")&(test.Age.isna())]=test[(test.status=="Master")&(test.Age.isna())].fillna(mean_age_children)
 mean_age_mistress=round(test[test.status=="Mistress"].Age.mean())
 test[(test.status=="Mistress")&(test.Age.isna())]=test[(test.status=="Mistress")&(test.Age.isna())].fillna(mean_age_mistress)
 mean_age_mister=round(test[test.status=="Mister"].Age.mean())
 test[(test.status=="Mister")&(test.Age.isna())]=test[(test.status=="Mister")&(test.Age.isna())].fillna(mean_age_mister)
 for i in range(418):
if test.loc[i,"Age"]<1:
test.loc[i,"age_category"]="Infants"
elif 1<=test.loc[i,"Age"]<18:
test.loc[i,"age_category"]="Children"
elif 18<=train.loc[i,"Age"]<65:
test.loc[i,"age_category"]="Adults"
elif test.loc[i,"Age"]>=65:
test.loc[i,"age_category"]="Elders"
else:
test.loc[i,"age_category"]="Unknown"
mean_age_femaleAdult=round(test[(test["age_category"]=="Adults")&(test.Sex=="female")].Age.mean())
 mean_age_femaleChildren=round(test[(test["age_category"]=="Children")&(test.Sex=="female")].Age.mean())
 test[(test.Age.isna())&(test.Parch==0)]=test[(test.Age.isna())&(test.Parch==0)].fillna(mean_age_femaleAdult)
 test[test.Age.isna() ]=test[test.Age.isna() ].fillna(mean_age_femaleChildren)
 for i in range(418):
if test.loc[i,"Age"]<1:
test.loc[i,"age_category"]="Infants"
elif 1<=test.loc[i,"Age"]<18:
test.loc[i,"age_category"]="Children"
elif 18<=test.loc[i,"Age"]<65:
test.loc[i,"age_category"]="Adults"
elif test.loc[i,"Age"]>=65:
test.loc[i,"age_category"]="Elders"
else:
test.loc[i,"age_category"]="Unknown"
fare_mean=test[test.Pclass==3].Fare.mean()
test.fillna(fare_mean,inplace=True)
 print(test.isna().sum())
<categorify>
X_train, y_train = train_df[FE], train_df[TARGET]
X_test, y_test = valid_df[FE], valid_df[TARGET]
label_encoder = LabelEncoder()
col=["Sex","Embarked","age_category"]
for i in col:
train[i]=label_encoder.fit_transform(train[i])
 test[i]=label_encoder.fit_transform(test[i])
<import_modules>
best_params = {
'iterations': 20000,
}
catb = CatBoostClassifier(**best_params,
loss_function='CrossEntropy',
eval_metric = 'AUC',
thread_count=2,
use_best_model=True,
task_type = "GPU",
verbose = False)
 roc_auc = list()
average_precision = list()
best_iteration = list()
X_train, y_train = train_df[FE], train_df[TARGET]
X_test, y_test = valid_df[FE], valid_df[TARGET]
train = Pool(data=X_train,
label=y_train,
feature_names=FE,
cat_features=[])
 test = Pool(data=X_test,
label=y_test,
feature_names=FE,
cat_features=[])
 catb.fit(train,
verbose_eval=50,
early_stopping_rounds=100,
eval_set=test,
use_best_model=True,
plot=False)
 best_iteration.append(catb.best_iteration_)
 preds = catb.predict_proba(X_test)
 roc_auc.append(roc_auc_score(y_true=y_test, y_score=preds[:,1]))
average_precision.append(average_precision_score(y_true=y_test, y_score=preds[:,1]))
print("Average cv roc auc score %0.3f ± %0.3f" %(np.mean(roc_auc), np.std(roc_auc)))
 print("Average cv roc average precision %0.3f ± %0.3f" %(np.mean(average_precision), np.std(average_precision)))
 catb.save_model('catb_model.cbm' )
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score 
<find_best_model_class>
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))
features1=["Pclass","Sex","Fare","age_category"]
X1=train[features1]
y1=train.Survived
X_train1, X_test1, y_train1,y_test1=train_test_split(X1,y1,test_size=0.22,random_state=0)
 DT1=DecisionTreeClassifier(random_state=1)
 DT1.fit(X_train1,y_train1)
 y_pred1=DT1.predict(X_test1)
 score=(round(accuracy_score(y_pred1,y_test1)*100,2))
score 
<find_best_model_class>
env = riiideducation.make_env()
iter_test = env.iter_test()
prior_test_df = None
<predict_on_test><EOS> 
%%time
for test_df, sample_prediction_df in iter_test:
if prior_test_df is not None:
prior_test_df['answered_correctly'] = 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, target in zip(user_ids, content_ids, targets):
user_sum_dict[user_id] += target
user_count_dict[user_id] += 1
content_sum_dict[content_id] += target
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 = test_df.merge(questions, 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)
 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)
 for i,(user_id, content_id)in enumerate(zip(test_df['user_id'].values, \
test_df['content_id'].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]
test_df['user_mean'] = user_sum / user_count
test_df['content_count'] = content_count
test_df['content_mean'] = content_sum / content_count
Xtest = test_df[FE].values
Xtest = Pool(data=Xtest,
feature_names=FE,
cat_features=[])
 test_df['answered_correctly'] = catb.predict_proba(Xtest)[:,1]
env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']])