Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<train_model>
import optuna
import lightgbm as lgb
import riiideducation
import dask.dataframe as dd
import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score
clf = SGDClassifier(max_iter=5, tol=None)
 clf.fit(X_train, y_train)
 y_pred_sgd = clf.predict(X_test)
 acc_sgd = round(clf.score(X_train, y_train)* 100, 2)
 print("Train Accuracy: " + str(acc_sgd)+ '%' )<compute_train_metric>
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']
clf = RandomForestClassifier(n_estimators=100)
 clf.fit(X_train, y_train)
 y_pred_random_forest_training_set = clf.predict(X_train)
 acc_random_forest = round(clf.score(X_train, y_train)* 100, 2)
 print("Accuracy: %i %%
"%acc_random_forest)
 class_names = ['Survived', 'Not Survived']
cnf_matrix = confusion_matrix(y_train, y_pred_random_forest_training_set)
 np.set_printoptions(precision=2)
 print('Confusion Matrix in Numbers')
 print(cnf_matrix)
 print('')
 cnf_matrix_percent = cnf_matrix.astype('float')/ cnf_matrix.sum(axis=1)[:, np.newaxis]
print('Confusion Matrix in Percentage')
 print(cnf_matrix_percent)
 print('')
 true_class_names = ['True Survived', 'True Not Survived']
predicted_class_names = ['Predicted Survived', 'Predicted Not Survived']
df_cnf_matrix = pd.DataFrame(cnf_matrix,
index = true_class_names,
columns = predicted_class_names)
 df_cnf_matrix_percent = pd.DataFrame(cnf_matrix_percent,
index = true_class_names,
columns = predicted_class_names)
 plt.figure(figsize =(15,5))
plt.subplot(121)
 sns.heatmap(df_cnf_matrix, annot=True, fmt='d', cmap = "Blues")
 plt.subplot(122)
 sns.heatmap(df_cnf_matrix_percent, annot=True, cmap = "Blues" )<create_dataframe>
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' )
models = pd.DataFrame({
'Model': ['LR', 'SVM', 'L-SVC',
'KNN', 'DTree', 'RF', 'NB',
'Perceptron', 'SGD'],
'Score': [acc_log_reg, acc_svc, acc_linear_svc,
acc_knn, acc_decision_tree, acc_random_forest, acc_gnb,
acc_perceptron, acc_sgd]
})
 models = models.sort_values(by='Score', ascending=False)
 models<save_to_csv>
X = train.iloc[80000000:,:]
X['prior_question_had_explanation'].fillna(False, inplace=True)
 X = pd.merge(X, results_u, on=['user_id'], how="left")
 X = pd.merge(X, results_c, on=['content_id'], how="left")
 X = pd.merge(X, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left')
 X=X[X.answered_correctly!= -1 ]
X=X.sort_values(['user_id'])
 Y = X[["answered_correctly"]]
X = X.drop(["answered_correctly"], axis=1 )
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": y_pred_random_forest
})
 submission.to_csv('gender_submission.csv', index=False )<load_from_csv>
print('Training data shrinked from {:,} records({:,} users)to {:,} records({:,} users ).'.format(train.shape[0], train['user_id'].nunique() , X.shape[0], X['user_id'].nunique()))
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 test_id = test['Id']<data_type_conversions>
lb_make = LabelEncoder()
X["prior_question_had_explanation_enc"] = lb_make.fit_transform(X["prior_question_had_explanation"])
 X.head()
X = X[['answered_correctly_user', 'answered_correctly_content', 'sum','bundle_id','part','prior_question_elapsed_time','prior_question_had_explanation_enc','tags1','tags2','tags3']]
X.fillna(0.5, inplace=True)
 Xt, Xv, Yt, Yv = train_test_split(X, Y, test_size = 0.2, shuffle=False )
missing_values = train.isnull().sum()
display(missing_values[missing_values>0].sort_values(ascending=False ).to_frame(name='Missing Values'))<count_values>
lgb_train = lgb.Dataset(Xt, Yt)
 lgb_eval = lgb.Dataset(Xv, Yv)
 def objective(trial):
params = {
'num_leaves': trial.suggest_int('num_leaves', 32, 512),
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'auc',
'max_depth': trial.suggest_int('max_depth', 4, 16),
'min_child_weight': trial.suggest_int('min_child_weight', 1, 16),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.4, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.4, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 1, 8),
'min_child_samples': trial.suggest_int('min_child_samples', 4, 80),
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-8, 1.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-8, 1.0),
'early_stopping_rounds': 10
}
model = lgb.train(params, lgb_train, valid_sets=[lgb_train, lgb_eval], verbose_eval=1000)
 val_pred = model.predict(Xv)
 score = roc_auc_score(Yv, val_pred)
 print(f"AUC = {score}")
 return score
train['OverallQual'].value_counts()<prepare_output>
study = optuna.create_study(direction='maximize')
 study.optimize(objective, n_trials=20 )
imp_ftr = corrmat['SalePrice'].sort_values(ascending=False ).head(11 ).to_frame()
imp_ftr<drop_column>
print('Number of finished trials: {}'.format(len(study.trials)))
 print('Best trial:')
 trial = study.best_trial
print(' Value: {}'.format(trial.value))
print(' Params: ')
 for key, value in trial.params.items() :
print(' {}: {}'.format(key, value))
train.drop(columns=['GarageArea','TotRmsAbvGrd','GarageYrBlt','1stFlrSF'],axis=1,inplace=True)
 test.drop(columns=['GarageArea','TotRmsAbvGrd','GarageYrBlt','1stFlrSF'],axis=1,inplace=True )<data_type_conversions>
model = lgb.train(trial.params, lgb_train, valid_sets=[lgb_train, lgb_eval], verbose_eval=1000 )
train['MSSubClass'] = train['MSSubClass'].apply(str)
 test['MSSubClass'] = test['MSSubClass'].apply(str)
 train['OverallCond'] = train['OverallCond'].astype(str)
 test['OverallCond'] = test['OverallCond'].astype(str)
 train['YrSold'] = train['YrSold'].astype(str)
 test['YrSold'] = test['YrSold'].astype(str)
 train['MoSold'] = train['MoSold'].astype(str)
 test['MoSold'] = test['MoSold'].astype(str )<sort_values>
y_pred = model.predict(Xv)
 y_true = np.array(Yv)
 print('CV score(AUC)= {}'.format(roc_auc_score(y_true, y_pred)) )
total_missing = train.isnull().sum().sort_values(ascending=False)
 total_missing = total_missing[total_missing>0]
percent =(( train.isnull().sum() /train.isnull().count())* 100 ).sort_values(ascending=False)
 percent = percent[percent>0]
data_missing = pd.concat([total_missing, percent], axis=1, keys=['Total', 'Percent'])
 data_missing<define_variables>
env = riiideducation.make_env()
<count_unique_values><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.fit_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']] )