Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<feature_engineering>
env = riiideducation.make_env()
df_merged['LotFrontage'] = df_merged.groupby(['BldgType'])['LotFrontage'].transform(lambda x: x.fillna(x.median()))<create_dataframe>
train = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/train.csv',
usecols=[1, 2, 3, 4, 5, 7, 8, 9],
dtype={'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id': 'int8',
'task_container_id': 'int16',
'answered_correctly':'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'boolean'}
)
df_merged_num_scaled = pd.DataFrame(data = df_merged_num_scaled, columns = df_merged_num.columns, index = df_merged_num.index)
<categorify>
questions_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 3],
dtype={'question_id': 'int16',
'part': 'int8'}
)
df_merged_cat = df_merged.select_dtypes(include = ['object'] ).astype('category')

df_merged_cat.LotShape.replace(to_replace = ['IR3', 'IR2', 'IR1', 'Reg'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.LandContour.replace(to_replace = ['Low', 'Bnk', 'HLS', 'Lvl'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.Utilities.replace(to_replace = ['NoSeWa', 'AllPub'], value = [0, 1], inplace = True)
 df_merged_cat.LandSlope.replace(to_replace = ['Sev', 'Mod', 'Gtl'], value = [0, 1, 2], inplace = True)
 df_merged_cat.ExterQual.replace(to_replace = ['Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.ExterCond.replace(to_replace = ['Po', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.BsmtQual.replace(to_replace = ['None', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.BsmtCond.replace(to_replace = ['None', 'Po', 'Fa', 'TA', 'Gd'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.BsmtExposure.replace(to_replace = ['None', 'No', 'Mn', 'Av', 'Gd'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.BsmtFinType1.replace(to_replace = ['None', 'Unf', 'LwQ', 'Rec', 'BLQ', 'ALQ', 'GLQ'], value = [0, 1, 2, 3, 4, 5, 6], inplace = True)
 df_merged_cat.BsmtFinType2.replace(to_replace = ['None', 'Unf', 'LwQ', 'Rec', 'BLQ', 'ALQ', 'GLQ'], value = [0, 1, 2, 3, 4, 5, 6], inplace = True)
 df_merged_cat.HeatingQC.replace(to_replace = ['Po', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.Electrical.replace(to_replace = ['Mix', 'FuseP', 'FuseF', 'FuseA', 'SBrkr'], value = [0, 1, 2, 3, 4], inplace = True)
 df_merged_cat.KitchenQual.replace(to_replace = ['Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.Functional.replace(to_replace = ['Sev', 'Maj2', 'Maj1', 'Mod', 'Min2', 'Min1', 'Typ'], value = [0, 1, 2, 3, 4, 5, 6], inplace = True)
 df_merged_cat.FireplaceQu.replace(to_replace = ['None', 'Po', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4, 5], inplace = True)
 df_merged_cat.GarageFinish.replace(to_replace = ['None', 'Unf', 'RFn', 'Fin'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.GarageQual.replace(to_replace = ['None', 'Po', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4, 5], inplace = True)
 df_merged_cat.GarageCond.replace(to_replace = ['None', 'Po', 'Fa', 'TA', 'Gd', 'Ex'], value = [0, 1, 2, 3, 4, 5], inplace = True)
 df_merged_cat.PavedDrive.replace(to_replace = ['N', 'P', 'Y'], value = [0, 1, 2], inplace = True)
 df_merged_cat.PoolQC.replace(to_replace = ['None', 'Fa', 'Gd', 'Ex'], value = [0, 1, 2, 3], inplace = True)
 df_merged_cat.Fence.replace(to_replace = ['None', 'MnWw', 'GdWo', 'MnPrv', 'GdPrv'], value = [0, 1, 2, 3, 4], inplace = True )<data_type_conversions>
train = train[train.content_type_id == False].sort_values('timestamp' ).reset_index(drop = True )
df_merged_cat.loc[:, ['OverallQual', 'OverallCond']] = df_merged_cat.loc[:, ['OverallQual', 'OverallCond']].astype('int64')

df_merged_label_encoded = df_merged_cat.select_dtypes(include = ['int64'] )<categorify>
train[(train.content_type_id == False)].task_container_id.nunique()
df_merged_one_hot = df_merged_cat.select_dtypes(include=['category'])

df_merged_one_hot = pd.get_dummies(df_merged_one_hot, drop_first=True )<choose_model_class>
group1 = train.loc[(train.content_type_id == False), ['task_container_id', 'user_id']].groupby(['task_container_id'] ).agg(['count'])
 group1.columns = ['avg_questions']
group2 = train.loc[(train.content_type_id == False), ['task_container_id', 'user_id']].groupby(['task_container_id'] ).agg(['nunique'])
 group2.columns = ['avg_questions']
group3 = group1 / group2
seed = 44


lr = LinearRegression(n_jobs = -1)
 lasso = Lasso(random_state = seed)
 ridge = Ridge(random_state = seed)
 elnt = ElasticNet(random_state = seed)
 kr = KernelRidge()
dt = DecisionTreeRegressor(random_state = seed)
 svr = SVR()
knn = KNeighborsRegressor(n_jobs= -1)
 pls = PLSRegression()
rf = RandomForestRegressor(n_jobs = -1, random_state = seed)
 et = ExtraTreesRegressor(n_jobs = -1, random_state = seed)
 ab = AdaBoostRegressor(random_state = seed)
 gb = GradientBoostingRegressor(random_state = seed)
 xgb = XGBRegressor(n_jobs = -1, random_state = seed)
 lgb = LGBMRegressor(n_jobs = -1, random_state = seed )<train_on_grid>
group3['avg_questions_seen'] = group3.avg_questions.cumsum()
def tune_hyperparameters(model, param_grid):
global best_params, best_score

 grid = GridSearchCV(model, param_grid, cv = 10, verbose = 1, scoring = 'neg_mean_squared_error', n_jobs = -1)
 grid.fit(df_train_final, y_train)
 best_params = grid.best_params_
best_score = np.round(np.sqrt(-1 * grid.best_score_), 5)
 return best_params, best_score<find_best_params>
group3.iloc[0].avg_questions_seen
alpha = [0.0001, 0.0002, 0.00025, 0.0003, 0.00031, 0.00032, 0.00033, 0.00034, 0.00035, 0.00036, 0.00037, 0.00038,
0.0004, 0.00045, 0.0005, 0.00055, 0.0006, 0.0008, 0.001, 0.002, 0.005, 0.007, 0.008, 0.01]
lasso_params = {'alpha': alpha,
'random_state':[seed]}
tune_hyperparameters(lasso, lasso_params)
 lasso_best_params, lasso_best_score = best_params, best_score
print('Lasso best params:{} & best_score:{:0.5f}'.format(lasso_best_params, lasso_best_score))<find_best_params>
results_u_final = train.loc[train.content_type_id == False, ['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_final.columns = ['answered_correctly_user']
results_u2_final = train.loc[train.content_type_id == False, ['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_final.columns = ['explanation_mean_user']
KernelRidge_param_grid = {'alpha':[0.1, 0.15, 0.23, 0.25, 0.3,1],
'kernel': ['linear', 'polynomial'],
'degree': [2,3],
'coef0': [1.5,2,3]}
tune_hyperparameters(kr, KernelRidge_param_grid)
 kr_best_params, kr_best_score = best_params, best_score
print('Kernel Ridge best params:{} & best_score: {:0.5f}'.format(kr_best_params, kr_best_score))<find_best_params>
train = pd.merge(train, questions_df, left_on = 'content_id', right_on = 'question_id', how = 'left' )
elastic_params_grid = {'alpha': [0.0001,0.0002, 0.0003, 0.01,0.1,2],
'l1_ratio': [0.2, 0.85, 0.95,0.98,10],
'random_state':[seed]}
tune_hyperparameters(elnt, elastic_params_grid)
 elastic_best_params, elastic_best_score = best_params, best_score
print('Elastic Net best params:{} & best_score:{:0.5f}'.format(elastic_best_params, elastic_best_score))<find_best_params>
results_q_final = train.loc[train.content_type_id == False, ['question_id','answered_correctly']].groupby(['question_id'] ).agg(['mean'])
 results_q_final.columns = ['quest_pct']
svr_params_grid = {'kernel':['linear', 'poly', 'rbf'],
'C':[2,4,5],
'gamma':[0.01,0.001,0.0001]}
tune_hyperparameters(svr, svr_params_grid)
 svr_best_params, svr_best_score = best_params, best_score
print('SVR best params:{} & best_score:{:0.5f}'.format(svr_best_params, svr_best_score))<find_best_params>
results_q2_final = train.loc[train.content_type_id == False, ['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['count']
rf_params_grid = {'n_estimators':[1,5,50,100],
'max_depth':[1,2],
'min_samples_split':[3,4],
'min_samples_leaf':[2,4],
'random_state':[seed]}
tune_hyperparameters(rf, rf_params_grid)
 rf_best_params, rf_best_score = best_params, best_score
print('RF best params:{} & best_score:{:0.5f}'.format(rf_best_params, rf_best_score))<define_search_space>
question2 = pd.merge(questions_df, results_q_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
xgb_params_grid = {'min_child_weight': [5, 10],
'gamma': [0.04, 0.1, 1.5],
'subsample': [0.6, 0.8, 1.0],
'colsample_bytree': [0.46, 1.0],
'max_depth': [3, 4]}
xgb_opt = XGBRegressor(learning_rate = 0.03, reg_alpha = 0.4640, reg_lambda = 0.8571, n_estimators = 1000,
silent = 1, nthread = -1, random_state = 101)
 tune_hyperparameters(xgb_opt, xgb_params_grid)
 xgb_best_params, xgb_best_score = best_params, best_score
print('XGB best params:{} & best_score:{:0.5f}'.format(xgb_best_params, xgb_best_score))<choose_model_class>
question2 = pd.merge(question2, results_q2_final, left_on = 'question_id', right_on = 'question_id', how = 'left' )
gb_opt = GradientBoostingRegressor(n_estimators = 3000, learning_rate = 0.05,
max_depth = 4, max_features = 'sqrt',
min_samples_leaf = 15, min_samples_split = 10,
loss = 'huber', random_state = seed)

lgb_opt = LGBMRegressor(objective = 'regression', num_leaves = 5,
learning_rate=0.05, n_estimators = 660,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed = 9, bagging_seed = 9,
min_data_in_leaf = 6, min_sum_hessian_in_leaf = 11)

gb_best_score = cross_validation(gb_opt)
 lgb_best_score = cross_validation(lgb_opt )<choose_model_class>
question2.quest_pct = round(question2.quest_pct,5 )
lasso_opt = Lasso(**lasso_best_params)
 ridge_opt = Ridge(**ridge_best_params)
 kernel_ridge_opt = KernelRidge(**kr_best_params)
 elastic_net_opt = ElasticNet(**elastic_best_params)
 rf_opt = RandomForestRegressor(**rf_best_params)
 svm_opt = SVR(**svr_best_params)
 xgb_opt = XGBRegressor(**xgb_best_params)
 gb_opt = gb_opt
lgb_opt = lgb_opt<save_to_csv>
train.drop(['timestamp', 'content_type_id', 'question_id', 'part'], axis=1, inplace=True )
def predict_with_optimized_models(model):
model.fit(df_train_final, y_train)
 y_pred = np.expm1(model.predict(df_test_final))
submission = pd.DataFrame()
submission['Id']= test.Id
submission['SalePrice'] = y_pred
return submission

 predict_with_optimized_models(lasso_opt ).to_csv('lasso_optimized.csv', index = False)
 predict_with_optimized_models(ridge_opt ).to_csv('ridge_optimized.csv', index = False)
 predict_with_optimized_models(kernel_ridge_opt ).to_csv('kernel_ridge_optimized.csv', index = False)
 predict_with_optimized_models(elastic_net_opt ).to_csv('elastic_net_optimized.csv', index = False)
 predict_with_optimized_models(rf_opt ).to_csv('rf_opt_optimized.csv', index = False)
 predict_with_optimized_models(svm_opt ).to_csv('svm_opt_optimized.csv', index = False)
 predict_with_optimized_models(xgb_opt ).to_csv('xgb_optimized.csv', index = False)
 predict_with_optimized_models(gb_opt ).to_csv('gb_optimized.csv', index = False)
 predict_with_optimized_models(lgb_opt ).to_csv('lgb_optimized.csv', index = False )<load_from_csv>
validation = train.groupby('user_id' ).tail(5)
 train = train[~train.index.isin(validation.index)]
len(train)+ len(validation )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv" )<create_dataframe>
results_u_val = train[['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_val.columns = ['answered_correctly_user']
results_u2_val = train[['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_val.columns = ['explanation_mean_user']
train_df_na =(train_df.isnull().sum() / len(train_df)) * 100
train_df_na = train_df_na.drop(train_df_na[train_df_na == 0].index ).sort_values(ascending=False)[:20]
missing_data = pd.DataFrame({'Missing Ratio' :train_df_na})
 missing_data.head(10 )<drop_column>
X = train.groupby('user_id' ).tail(18)
 train = train[~train.index.isin(X.index)]
len(X)+ len(train)+ len(validation )
train_df = train_df.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence'], axis=1)
 test_df = test_df.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence'], axis=1 )<feature_engineering>
results_u_X = train[['user_id','answered_correctly']].groupby(['user_id'] ).agg(['mean'])
 results_u_X.columns = ['answered_correctly_user']
results_u2_X = train[['user_id','prior_question_had_explanation']].groupby(['user_id'] ).agg(['mean'])
 results_u2_X.columns = ['explanation_mean_user']
for dataset in [train_df, test_df]:
dataset['SF'] = dataset['TotalBsmtSF'] + dataset['1stFlrSF'] + dataset['2ndFlrSF']
dataset['TotalBath'] = dataset['BsmtFullBath'] +(1/2)* dataset['BsmtHalfBath'] + dataset['FullBath'] +(1/2)* dataset['HalfBath']
dataset['HasPool'] = dataset['PoolArea'].apply(lambda x : 1 if x>0 else 0)
 dataset['Has2ndFloor'] = dataset['2ndFlrSF'].apply(lambda x : 1 if x>0 else 0)
 dataset['HasGarage'] = dataset['GarageArea'].apply(lambda x : 1 if x>0 else 0 )<drop_column>
del(train )
train_df = train_df.drop(['Id'], axis=1 )<define_variables>
X = pd.merge(X, group3, left_on=['task_container_id'], right_index= True, how="left")
 X = pd.merge(X, results_u_X, on=['user_id'], how="left")
 X = pd.merge(X, results_u2_X, on=['user_id'], how="left" )
LotShape_mapping = {"Reg": 1, "IR1": 2, "IR2": 3, "IR3": 4}
LandSlope_mapping = {"Gtl": 1, "Mod": 2, "Sev": 3}
ExterQual_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
ExterCond_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
BsmtQual_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5, "NA": 6}
BsmtCond_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5, "NA": 6}
BsmtExposure_mapping = {"Gd": 1, "Av": 2, "Mn": 3, "No": 4, "NA": 5}
BsmtFinType1_mapping = {"GLQ": 1, "ALQ": 2, "BLQ": 3, "Rec": 4, "LwQ": 5, "Unf": 6, "NA": 7}
BsmtFinType2_mapping = {"GLQ": 1, "ALQ": 2, "BLQ": 3, "Rec": 4, "LwQ": 5, "Unf": 6, "NA": 7}
HeatingQC_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
CentralAir_mapping = {"N": 0, "Y": 1}
KitchenQual_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
FireplaceQu_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
GarageQual_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
GarageCond_mapping = {"Ex": 1, "Gd": 2, "TA": 3, "Fa": 4, "Po": 5}
PavedDrive_mapping = {"Y": 1, "P": 2, "N": 3}
for dataset in [train_df, test_df]:
dataset['LotShape'] = dataset['LotShape'].map(LotShape_mapping)
 dataset['LandSlope'] = dataset['LandSlope'].map(LandSlope_mapping)
 dataset['ExterQual'] = dataset['ExterQual'].map(ExterQual_mapping)
 dataset['ExterCond'] = dataset['ExterCond'].map(ExterCond_mapping)
 dataset['BsmtQual'] = dataset['BsmtQual'].map(BsmtQual_mapping)
 dataset['BsmtCond'] = dataset['BsmtCond'].map(BsmtCond_mapping)
 dataset['BsmtExposure'] = dataset['BsmtExposure'].map(BsmtExposure_mapping)
 dataset['BsmtFinType1'] = dataset['BsmtFinType1'].map(BsmtFinType1_mapping)
 dataset['BsmtFinType2'] = dataset['BsmtFinType2'].map(BsmtFinType2_mapping)
 dataset['HeatingQC'] = dataset['HeatingQC'].map(HeatingQC_mapping)
 dataset['CentralAir'] = dataset['CentralAir'].map(CentralAir_mapping)
 dataset['KitchenQual'] = dataset['KitchenQual'].map(KitchenQual_mapping)
 dataset['GarageQual'] = dataset['GarageQual'].map(GarageQual_mapping)
 dataset['GarageCond'] = dataset['GarageCond'].map(GarageCond_mapping)
 dataset['PavedDrive'] = dataset['PavedDrive'].map(PavedDrive_mapping )<sort_values>
validation = pd.merge(validation, group3, left_on=['task_container_id'], right_index= True, how="left")
 validation = pd.merge(validation, results_u_val, on=['user_id'], how="left")
 validation = pd.merge(validation, results_u2_val, on=['user_id'], how="left" )
for attr in train_df.select_dtypes(include='object'):
print(train_df[[attr, "SalePrice"]].groupby([attr], as_index=False ).mean().sort_values(by='SalePrice', ascending=False))
print("
________________________
" )<define_variables>
lb_make = LabelEncoder()
X.prior_question_had_explanation.fillna(False, inplace = True)
 validation.prior_question_had_explanation.fillna(False, inplace = True)
 validation["prior_question_had_explanation_enc"] = lb_make.fit_transform(validation["prior_question_had_explanation"])
 X["prior_question_had_explanation_enc"] = lb_make.fit_transform(X["prior_question_had_explanation"] )
cat_attrs =["MSZoning","LandContour", "LotConfig", "Neighborhood", "Condition1", "Condition2", "HouseStyle", "RoofStyle",
"RoofMatl", "Exterior1st", "Exterior2nd", "MasVnrType", "Foundation", "Heating", "Electrical", "Functional",
"GarageType", "GarageFinish", "SaleType", "SaleCondition"]<sort_values>
drop_indexes = train_df.sort_values(by='SF', ascending=False)[:2].index
train_df = train_df.drop(drop_indexes )<train_model>
question2.quest_pct = question2.quest_pct.mask(( question2['count'] < 3),.65)
 question2.quest_pct = question2.quest_pct.mask(( question2.quest_pct <.2)&(question2['count'] < 21),.2)
 question2.quest_pct = question2.quest_pct.mask(( question2.quest_pct >.95)&(question2['count'] < 21),.95 )
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attr_names):
self.attr_names = attr_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attr_names].values<train_model>
X = pd.merge(X, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 validation = pd.merge(validation, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 X.part = X.part - 1
validation.part = validation.part - 1
class LabelBinarizerPipelineFriendly(MultiLabelBinarizer):
def fit(self, X, y=None):
super(LabelBinarizerPipelineFriendly,self ).fit(X)
 def transform(self, X, y=None):
return super(LabelBinarizerPipelineFriendly, self ).transform(X)
 def fit_transform(self, X, y=None):
return super(LabelBinarizerPipelineFriendly, self ).fit(X ).transform(X )<create_dataframe>
y = X['answered_correctly']
X = X.drop(['answered_correctly'], axis=1)
 X.head()
y_val = validation['answered_correctly']
X_val = validation.drop(['answered_correctly'], axis=1 )
num_data_attrs = [x for x in num_attrs if not x=='SalePrice']
num_pipeline = Pipeline([ 
('selector', DataFrameSelector(num_data_attrs)) , 
('imputer', SimpleImputer(strategy="median")) , 
('std_scaler', StandardScaler()),
] )<create_dataframe>
X = X[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part']]
X_val = X_val[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part']]
cat_pipeline = Pipeline([ 
('selector', DataFrameSelector(cat_attrs)) , 
('imputer', SimpleImputer(strategy="most_frequent")) , 
('binarizer', LabelBinarizerPipelineFriendly()),
] )<import_modules>
X['answered_correctly_user'].fillna(0.65, inplace=True)
 X['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X['quest_pct'].fillna(content_mean, inplace=True)
 X['part'].fillna(4, inplace = True)
 X['avg_questions_seen'].fillna(1, inplace = True)
 X['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X['prior_question_had_explanation_enc'].fillna(0, inplace = True)
full_pipeline = FeatureUnion(transformer_list=[ 
('num_pipeline', num_pipeline), 
('cat_pipeline', cat_pipeline),
] )<predict_on_test>
X_val['answered_correctly_user'].fillna(0.65, inplace=True)
 X_val['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 X_val['quest_pct'].fillna(content_mean, inplace=True)
 X_val['part'].fillna(4, inplace = True)
 X['avg_questions_seen'].fillna(1, inplace = True)
 X_val['prior_question_elapsed_time'].fillna(elapsed_mean, inplace = True)
 X_val['prior_question_had_explanation_enc'].fillna(0, inplace = True )
def find_outliers(model, X, y, sigma=3):
y_pred = pd.Series(model.predict(X), index=y.index)
 resid = y - y_pred
mean_resid = resid.mean()
std_resid = resid.std()
z =(resid - mean_resid)/std_resid
outliers = z[abs(z)>sigma].index
return outliers<train_model>
params = {
'objective': 'binary',
'boosting' : 'gbdt',
'max_bin': 800,
'learning_rate': 0.0175,
'num_leaves': 80
}
lgb_train = lgb.Dataset(X, y, categorical_feature = ['part', 'prior_question_had_explanation_enc'])
 lgb_eval = lgb.Dataset(X_val, y_val, categorical_feature = ['part', 'prior_question_had_explanation_enc'], reference=lgb_train )
train_data = train_df.drop(['SalePrice'], axis=1)
 train_labels = train_df['SalePrice']
model = Ridge()
train_prepared = full_pipeline.fit_transform(train_data)
 model.fit(train_prepared, train_labels)
 outliers = find_outliers(model, train_prepared, train_labels)
 train_df = train_df.drop(np.asarray(outliers))<drop_column>
model = lgb.train(
 params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=50,
num_boost_round=1300,
early_stopping_rounds=8
)
train_data = train_df.drop(['SalePrice'], axis=1)
 train_labels = train_df['SalePrice']<categorify>
y_pred = model.predict(X_val)
 y_true = np.array(y_val)
 roc_auc_score(y_true, y_pred )
train_prepared = full_pipeline.fit_transform(train_data )<compute_train_metric>
import matplotlib.pyplot as plt
import seaborn as sns
def rms_score_cv(model):
rmse= np.sqrt(-cross_val_score(model, train_prepared, train_labels, scoring="neg_mean_squared_error", cv = 5))
return(rmse.mean() )<choose_model_class>
iter_test = env.iter_test()
<choose_model_class><EOS> 
for(test_df, sample_prediction_df)in iter_test:
test_df['task_container_id'] = test_df.task_container_id.mask(test_df.task_container_id > 9999, 9999)
 test_df = pd.merge(test_df, group3, left_on=['task_container_id'], right_index= True, how="left")
 test_df = pd.merge(test_df, question2, left_on = 'content_id', right_on = 'question_id', how = 'left')
 test_df = pd.merge(test_df, results_u_final, on=['user_id'], how="left")
 test_df = pd.merge(test_df, results_u2_final, on=['user_id'], how="left")
 test_df['answered_correctly_user'].fillna(0.65, inplace=True)
 test_df['explanation_mean_user'].fillna(prior_mean_user, inplace=True)
 test_df['quest_pct'].fillna(content_mean, inplace=True)
 test_df['part'] = test_df.part - 1
test_df['part'].fillna(4, inplace = True)
 test_df['avg_questions_seen'].fillna(1, inplace = True)
 test_df['prior_question_elapsed_time'].fillna(elapsed_mean, 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', 'explanation_mean_user', 'quest_pct', 'avg_questions_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part']])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )