Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<feature_engineering>
import riiideducation
import numpy as np
import pandas as pd
from tqdm import tqdm
cmb_log_df = combined_train_test_df.select_dtypes(include=[np.number])
 cmb_log_df = cmb_log_df.drop(['Id', 'SalePrice'], axis=1)
 for col in cmb_log_df.columns:
cmb_log_df[col] = np.log1p(cmb_log_df[col] )<normalization>
def get_new_theta(is_good_answer, beta, left_asymptote, theta, nb_previous_answers):
return theta + learning_rate_theta(nb_previous_answers)*(
 is_good_answer - probability_of_good_answer(theta, beta, left_asymptote)
)
 def get_new_beta(is_good_answer, beta, left_asymptote, theta, nb_previous_answers):
return beta - learning_rate_beta(nb_previous_answers)*(
 is_good_answer - probability_of_good_answer(theta, beta, left_asymptote)
)
 def learning_rate_theta(nb_answers):
return max(0.3 /(1 + 0.01 * nb_answers), 0.04)
 def learning_rate_beta(nb_answers):
return 1 /(1 + 0.05 * nb_answers)
 def probability_of_good_answer(theta, beta, left_asymptote):
return left_asymptote +(1 - left_asymptote)* sigmoid(theta - beta)
 def sigmoid(x):
return 1 /(1 + np.exp(-x))
rob_sca = RobustScaler()
rob_sca.fit(cmb_log_df)
 cmb_sca_df = pd.DataFrame(rob_sca.transform(cmb_log_df), index=cmb_log_df.index, columns=cmb_log_df.columns )<categorify>
def estimate_parameters(answers_df, granularity_feature_name='content_id'):
item_parameters = {
granularity_feature_value: {"beta": 0, "nb_answers": 0}
for granularity_feature_value in np.unique(answers_df[granularity_feature_name])
 }
student_parameters = {
student_id: {"theta": 0, "nb_answers": 0}
for student_id in np.unique(answers_df.student_id)
 }
print("Parameter estimation is starting...")
 for student_id, item_id, left_asymptote, answered_correctly in tqdm(
 zip(answers_df.student_id.values, answers_df[granularity_feature_name].values, answers_df.left_asymptote.values, answers_df.answered_correctly.values)
):
theta = student_parameters[student_id]["theta"]
beta = item_parameters[item_id]["beta"]
item_parameters[item_id]["beta"] = get_new_beta(
 answered_correctly, beta, left_asymptote, theta, item_parameters[item_id]["nb_answers"], 
)
 student_parameters[student_id]["theta"] = get_new_theta(
 answered_correctly, beta, left_asymptote, theta, student_parameters[student_id]["nb_answers"], 
)
 item_parameters[item_id]["nb_answers"] += 1
student_parameters[student_id]["nb_answers"] += 1
print(f"Theta & beta estimations on {granularity_feature_name} are completed.")
 return student_parameters, item_parameters
category_cols_dummies_df = pd.get_dummies(combined_train_test_df[category_cols])
 cmb_sca_df = pd.concat([cmb_sca_df, category_cols_dummies_df], axis = 1 )<split>
def update_parameters(answers_df, student_parameters, item_parameters, granularity_feature_name='content_id'):
for student_id, item_id, left_asymptote, answered_correctly in tqdm(zip(
 answers_df.student_id.values,
answers_df[granularity_feature_name].values,
answers_df.left_asymptote.values,
answers_df.answered_correctly.values)
):
if student_id not in student_parameters:
student_parameters[student_id] = {'theta': 0, 'nb_answers': 0}
if item_id not in item_parameters:
item_parameters[item_id] = {'beta': 0, 'nb_answers': 0}
theta = student_parameters[student_id]['theta']
beta = item_parameters[item_id]['beta']
student_parameters[student_id]['theta'] = get_new_theta(
 answered_correctly, beta, left_asymptote, theta, student_parameters[student_id]['nb_answers'] 
)
 item_parameters[item_id]['beta'] = get_new_beta(
 answered_correctly, beta, left_asymptote, theta, item_parameters[item_id]['nb_answers'] 
)
 student_parameters[student_id]['nb_answers'] += 1
item_parameters[item_id]['nb_answers'] += 1
return student_parameters, item_parameters
train_data = cmb_sca_df[:1460]
test_data = cmb_sca_df[1460:]<feature_engineering>
def estimate_probas(test_df, student_parameters, item_parameters, granularity_feature_name='content_id'):
probability_of_success_list = []
for student_id, item_id, left_asymptote in tqdm(
 zip(test_df.student_id.values, test_df[granularity_feature_name].values, test_df.left_asymptote.values)
):
theta = student_parameters[student_id]['theta'] if student_id in student_parameters else 0
beta = item_parameters[item_id]['beta'] if item_id in item_parameters else 0
probability_of_success_list.append(probability_of_good_answer(theta, beta, left_asymptote))
return probability_of_success_list
train_data['SalePrice'] = np.log1p(train_df['SalePrice'] )<prepare_x_and_y>
compute_estimations = False
nb_rows_training = None
train_data_X = train_data.drop(['SalePrice'], axis = 1)
 train_data_y = train_data['SalePrice']
test_data_X = test_data<compute_train_metric>
if compute_estimations:
training = pd.read_csv(
 filepath_or_buffer="/kaggle/input/riiid-test-answer-prediction/train.csv",
usecols=["content_id", "user_id", "answered_correctly"],
dtype={'answered_correctly': "int8"},
nrows=nb_rows_training 
)
 training.rename(columns={'user_id': 'student_id'}, inplace=True)
 training = training[training.answered_correctly != -1]
training['left_asymptote'] = 1/4
print(f"Dataset of shape {training.shape}")
 print(f"Columns are {list(training.columns)}")
 student_parameters, item_parameters = estimate_parameters(training)
 else:
student_data = pd.read_csv('.. /input/thetas-20201217/thetas_20201217.csv', index_col='student_id')
 student_parameters = student_data.to_dict('index')
 print(f"Successfully read student parameter file and converted to dict.")
 content_data = pd.read_csv('.. /input/betas-content-id-20201217/betas_content_id_20201217.csv', index_col='content_id')
 item_parameters = content_data.to_dict('index')
 print(f"Successfully read item parameter file and converted to dict." )
def RMSE(estimator,X_train,Y_train,cv=5,n_jobs=-1):
cv_results = cross_val_score(estimator,X_train,Y_train,cv=cv,scoring="neg_mean_squared_error",n_jobs=n_jobs)
 return(np.sqrt(-cv_results)).mean()<train_on_grid>
def format_test_df(test_df):
test_copy = test_df.copy()
test_copy = test_copy[test_copy['content_type_id'] == 0]
test_copy['left_asymptote'] = 1/4
test_copy = test_copy.rename(columns={'user_id': 'student_id'})
 return test_copy
<save_to_csv><EOS> 
env = riiideducation.make_env()
iter_test = env.iter_test()
previous_test_df = None
for(test_df, sample_prediction_df)in iter_test:
if previous_test_df is not None:
previous_test_df['answered_correctly'] = eval(test_df["prior_group_answers_correct"].iloc[0])
 previous_test_df = format_test_df(previous_test_df)
 student_parameters, item_parameters = update_parameters(previous_test_df, student_parameters, item_parameters)
 previous_test_df = test_df.copy()
test_df = format_test_df(test_df)
 test_df['answered_correctly'] = estimate_probas(test_df, student_parameters, item_parameters)
 env.predict(test_df[['row_id', 'answered_correctly']] )