Kaggle competition: Riiid Answer Correctness Prediction
train.drop('Utilities', axis=1, inplace=True)
 test.drop('Utilities', axis=1, inplace=True )<sort_values>
env = riiideducation.make_env()
print("Train dataset:
",train.isnull().sum().sort_values(ascending=False ).head(5))
print("
Test dataset:
",test.isnull().sum().sort_values(ascending=False ).head(5))<feature_engineering>
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'
}
)
train['renovated']= 1
train.loc[(train['YearBuilt']!=train['YearRemodAdd']),'renovated' ]=0
test['renovated']= 1
test.loc[(test['YearBuilt']!=test['YearRemodAdd']),'renovated' ]=0
train['Total_porch']=(train['OpenPorchSF'])+train['ScreenPorch']+train['3SsnPorch']
train['BsmtFinSF_total']=(train['BsmtFinSF1']**2)+train['BsmtFinSF2']
train['Bath']=(train['FullBath']**2)+ train['HalfBath']
train['BsmtBath']=(train['BsmtFullBath'])+train['BsmtHalfBath']
train['TotalSF'] = train['TotalBsmtSF'] + train['1stFlrSF'] + train['2ndFlrSF']
train['Total_garage']=(train['GarageCars']**5)+ train['GarageArea']
test['Total_porch']=(test['OpenPorchSF'])+test['ScreenPorch']+test['3SsnPorch']
test['BsmtFinSF_total']=(test['BsmtFinSF1']**2)+test['BsmtFinSF2']
test['Bath']=(test['FullBath']**2)+ test['HalfBath']
test['BsmtBath']=(test['BsmtFullBath'])+test['BsmtHalfBath']
test['TotalSF'] = test['TotalBsmtSF'] + test['1stFlrSF'] + test['2ndFlrSF']
test['Total_garage']=(test['GarageCars']**5)+ test['GarageArea']<prepare_x_and_y>
questions_df = pd.read_csv(
 '/kaggle/input/riiid-test-answer-prediction/questions.csv',
usecols=[0,3],
dtype = {
"question_id":"int64",
"part":"int8"
}
)
y=np.asarray(train['SalePrice'])
 y=np.log(y+1)
 X_train = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[: train.shape[0]]
X_test = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[train.shape[0]:]
X_train.shape, X_test.shape<compute_train_metric>
lectures_df = pd.read_csv(
 '/kaggle/input/riiid-test-answer-prediction/lectures.csv',
)
def return_rmse(model):
return np.sqrt(-cross_val_score(model, X_train, y, cv=5, scoring="neg_mean_squared_error")).mean()<choose_model_class>
lectures_df["type_of"] = lectures_df["type_of"].replace("solving question", "solving_question")
 lectures_df = pd.get_dummies(lectures_df, columns=["part", "type_of"] )
RR= Ridge(alpha=15)
 return_rmse(RR )<compute_test_metric>
part_lectures_columns = [column for column in lectures_df.columns if column.startswith("part")]
type_of_lectures_columns = [column for column in lectures_df.columns if column.startswith("type_of_")]
LSR = Lasso(alpha=0.0005)
 return_rmse(LSR )<choose_model_class>
train_lectures = train[train.content_type_id==True].merge(lectures_df,
left_on="content_id",
right_on="lecture_id",
how="left" )
EN = ElasticNet(alpha=0.01,l1_ratio=0.1)
 return_rmse(EN )<train_model>
user_lecture_stats_part = train_lectures.groupby("user_id")[part_lectures_columns + type_of_lectures_columns].sum()
RR.fit(X_train, y)
 LSR.fit(X_train, y)
 EN.fit(X_train, y)
 y_pred = RR.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers1=np.where(abs(z)> abs(z ).std() * 3)[0]
outliers1<predict_on_test>
for column in user_lecture_stats_part.columns:
bool_column = column + "_boolean"
user_lecture_stats_part[bool_column] =(user_lecture_stats_part[column] > 0 ).astype(int )
y_pred = LSR.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers2=np.where(abs(z)> abs(z ).std() * 3)[0]
outliers2<predict_on_test>
del train_lectures
gc.collect()
y_pred = EN.predict(X_train)
 residual = y - y_pred
z = np.abs(stats.zscore(residual))
outliers3=list(np.where(abs(z)> abs(z ).std() * 3)) [0]
outliers3<drop_column>
train = train[train.content_type_id == False].sort_values("timestamp" ).reset_index(drop=True )
outliers = []
for i in outliers1:
if(i in outliers2)&(i in outliers3):
outliers.append(i)
 train = train.drop(outliers )<prepare_x_and_y>
group1 = train[["task_container_id", "user_id"]].groupby("task_container_id" ).agg(["count"])
 group1.columns = ["avg_questions"]
group2 = train[["task_container_id", "user_id"]].groupby("task_container_id" ).agg(["nunique"])
 group2.columns = ["avg_questions"]
group3 = group1 / group2
y = train["SalePrice"]
y = np.log(y+1)
 X_train = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[: train.shape[0]]
X_test = pd.get_dummies(pd.concat(( train.drop(["SalePrice", "Id"], axis=1),
test.drop(["Id"], axis=1)) , axis=0)).iloc[train.shape[0]:]<choose_model_class>
group3["avg_question_seen"] = group3.avg_questions.cumsum()
RR= Ridge(alpha=15)
 return_rmse(RR )<compute_test_metric>
results_u_final = train[["user_id", "answered_correctly"]].groupby("user_id" ).agg(["mean"])
 results_u_final.columns = ["answered_correctly_user"]
LSR = Lasso(alpha=0.0004)
 return_rmse(LSR )<choose_model_class>
results_u2_final = train[["user_id", "prior_question_had_explanation"]].groupby("user_id" ).agg(["mean"])
 results_u2_final.columns = ["explanation_mean_user"]
EN = ElasticNet(alpha=0.001,l1_ratio=0.5)
 return_rmse(EN )<save_to_csv>
train = pd.merge(train, questions_df, left_on="content_id", right_on="question_id", how="left" )
RR.fit(X_train, y)
 LSR.fit(X_train, y)
 EN.fit(X_train, y)
 predict = 0.4 * RR.predict(X_test)+ 0.3 * EN.predict(X_test)+ 0.3 * LSR.predict(X_test)
 predict= np.exp(predict)-1
sample_submission= pd.DataFrame({'Id':np.asarray(test.Id), 'SalePrice':predict})
 sample_submission.to_csv("submit.csv", index=False )<import_modules>
results_q_final = train[['question_id','answered_correctly']].groupby(['question_id'] ).agg(['mean'])
 results_q_final.columns = ['quest_pct']
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.linear_model import ElasticNet
from sklearn.kernel_ridge import KernelRidge
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
import xgboost as xgb
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error<load_from_csv>
results_q2_final = train[['question_id','part']].groupby(['question_id'] ).agg(['count'])
 results_q2_final.columns = ['quest_count']
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<concatenate>
question2 = pd.merge(questions_df, results_q2_final, on="question_id", how="left" )
df_train['DataType'], df_test['DataType'] = 'training', 'testing'
df_test.insert(df_test.shape[1] - 1, 'SalePrice', np.nan)
 df_data = pd.concat([df_train, df_test], ignore_index=True )<data_type_conversions>
question2 = pd.merge(question2, results_q_final, on="question_id", how="left" )
col_convert = ['MSSubClass']
df_data[col_convert] = df_data[col_convert].astype('object' )<feature_engineering>
question2.quest_pct = round(question2.quest_pct, 5 )
df_data['SalePrice'] = np.log1p(df_data['SalePrice'] )<feature_engineering>
train.drop(["timestamp", "content_type_id", "question_id", "part"], axis=1, inplace=True )
df_data['MiscVal'] = np.log1p(df_data['MiscVal'] )<feature_engineering>
train = train[~train.index.isin(validation.index)]
df_data['LotFrontage'] = df_data['LotFrontage'].fillna(df_data.groupby(['Neighborhood'])['LotFrontage'].transform('mean'))
df_data.loc[(df_data['LotFrontage'] > 200)&(df_data['DataType'] == 'trainnig'), 'DataType'] = 'excluded'<feature_engineering>
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"]
df_data.loc[(df_data['LotArea'] > 100000)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<feature_engineering>
X = train.groupby("user_id" ).tail(18)
 train = train[~train.index.isin(X.index)]
df_data.loc[(df_data['BsmtFinSF1'] > 4000)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<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"]
df_data.loc[(df_data['TotalBsmtSF'] > 5000)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<feature_engineering>
del(train)
 gc.collect()
df_data.loc[(df_data['1stFlrSF'] > 4000)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<feature_engineering>
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")
 X = pd.merge(X, user_lecture_stats_part, on="user_id", how="left" )
df_data.loc[(df_data['GrLivArea'] > 4500)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<feature_engineering>
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")
 validation = pd.merge(validation, user_lecture_stats_part, on="user_id", how="left" )
df_data.loc[(df_data['OpenPorchSF'] > 500)&(df_data['SalePrice'] < 11)&(df_data['DataType'] == 'training'), 'DataType'] = 'excluded'<feature_engineering>
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"] )
col_convert = ['LotArea', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF',
'1stFlrSF', '2ndFlrSF', 'LowQualFinSF', 'GrLivArea', 'GarageArea',
'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea']
df_data[col_convert] = df_data[col_convert].fillna(0)
 df_data['TotalSF'] = df_data['TotalBsmtSF'] + df_data['GrLivArea']
df_data['TotalPorch'] = df_data['OpenPorchSF'] + df_data['EnclosedPorch'] + df_data['3SsnPorch'] + df_data['ScreenPorch']
df_data['TotalArea'] = df_data['TotalSF'] + df_data['TotalPorch'] + df_data['GarageArea'] + df_data['WoodDeckSF']<feature_engineering>
question2.quest_pct = question2.quest_pct.mask(( question2["quest_count"] < 3),.65)
 question2.quest_pct = question2.quest_pct.mask(( question2["quest_pct"] <.2)&(question2["quest_count"] < 21),.2)
 question2.quest_pct = question2.quest_pct.mask(( question2["quest_pct"] >.95)&(question2["quest_count"] < 21),.95 )
col_convert = ['BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath',
'BedroomAbvGr', 'KitchenAbvGr', 'TotRmsAbvGrd']
df_data[col_convert] = df_data[col_convert].fillna(0)
 df_data['TotalBathBsmt'] = df_data['BsmtFullBath'] + 0.5 * df_data['BsmtHalfBath']
df_data['TotalBathAbvGrd'] = df_data['FullBath'] + 0.5 * df_data['HalfBath']
df_data['TotalRmsAbvGrdIncBath'] = df_data['TotRmsAbvGrd'] + df_data['TotalBathAbvGrd']
df_data['TotalRms'] = df_data['TotalRmsAbvGrdIncBath'] + df_data['TotalBathBsmt']<feature_engineering>
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
df_data['AreaPerRmsBsmt'] = df_data['TotalBsmtSF'] /(df_data['TotalBathBsmt'] + 1)
 df_data['AreaPerRmsGrLivAbvGrd'] = df_data['GrLivArea'] /(df_data['TotalRmsAbvGrdIncBath'] + 1)
 df_data['AreaPerRmsTotal'] = df_data['TotalSF'] /(df_data['TotalRms'] + 1 )<data_type_conversions>
y = X["answered_correctly"]
X = X.drop(["answered_correctly"], axis=1)
 y_val = validation["answered_correctly"]
X_val = validation.drop(["answered_correctly"], axis=1 )
col_convert = ['ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'HeatingQC',
'KitchenQual', 'FireplaceQu', 'GarageQual', 'GarageCond', 'PoolQC']
df_data[col_convert] = df_data[col_convert].replace('Ex', 5 ).replace('Gd', 4 ).replace('TA', 3 ).replace('Fa', 2 ).replace('Po', 1 ).replace('NA', 0)
 df_data[col_convert] = df_data[col_convert].fillna(0 ).astype(int)
 df_data['ExterQualCond'] = df_data['ExterQual'] * df_data['ExterCond']
df_data['BsmtQualCond'] = df_data['BsmtQual'] * df_data['BsmtCond']
df_data['GarageQualCond'] = df_data['GarageQual'] * df_data['GarageCond']
df_data['OverallQualCond'] = df_data['OverallQual'] * df_data['OverallCond']<data_type_conversions>
X = X[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]
X_val = X_val[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']]
col_convert = ['BsmtExposure']
df_data[col_convert] = df_data[col_convert].replace('Gd', 4 ).replace('Av', 3 ).replace('Mn', 2 ).replace('No', 1 ).replace('NA', 0)
 df_data[col_convert] = df_data[col_convert].fillna(0 ).astype(int )<data_type_conversions>
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_question_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)
 X['part_1'].fillna(0, inplace = True)
 X['part_2'].fillna(0, inplace = True)
 X['part_3'].fillna(0, inplace = True)
 X['part_4'].fillna(0, inplace = True)
 X['part_5'].fillna(0, inplace = True)
 X['part_6'].fillna(0, inplace = True)
 X['part_7'].fillna(0, inplace = True)
 X['type_of_concept'].fillna(0, inplace = True)
 X['type_of_intention'].fillna(0, inplace = True)
 X['type_of_solving_question'].fillna(0, inplace = True)
 X['type_of_starter'].fillna(0, inplace = True)
 X['part_1_boolean'].fillna(0, inplace = True)
 X['part_2_boolean'].fillna(0, inplace = True)
 X['part_3_boolean'].fillna(0, inplace = True)
 X['part_4_boolean'].fillna(0, inplace = True)
 X['part_5_boolean'].fillna(0, inplace = True)
 X['part_6_boolean'].fillna(0, inplace = True)
 X['part_7_boolean'].fillna(0, inplace = True)
 X['type_of_concept_boolean'].fillna(0, inplace = True)
 X['type_of_intention_boolean'].fillna(0, inplace = True)
 X['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X['type_of_starter_boolean'].fillna(0, inplace = True )
col_convert = ['BsmtFinType1', 'BsmtFinType2']
df_data[col_convert] = df_data[col_convert].replace('GLQ', 6 ).replace('ALQ', 5 ).replace('BLQ', 4 ).replace('Rec', 3 ).replace('LwQ', 2 ).replace('Unf', 1 ).replace('NA', 0)
 df_data[col_convert] = df_data[col_convert].fillna(0 ).astype(int )<data_type_conversions>
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_val['avg_question_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)
 X_val['part_1'].fillna(0, inplace = True)
 X_val['part_2'].fillna(0, inplace = True)
 X_val['part_3'].fillna(0, inplace = True)
 X_val['part_4'].fillna(0, inplace = True)
 X_val['part_5'].fillna(0, inplace = True)
 X_val['part_6'].fillna(0, inplace = True)
 X_val['part_7'].fillna(0, inplace = True)
 X_val['type_of_concept'].fillna(0, inplace = True)
 X_val['type_of_intention'].fillna(0, inplace = True)
 X_val['type_of_solving_question'].fillna(0, inplace = True)
 X_val['type_of_starter'].fillna(0, inplace = True)
 X_val['part_1_boolean'].fillna(0, inplace = True)
 X_val['part_2_boolean'].fillna(0, inplace = True)
 X_val['part_3_boolean'].fillna(0, inplace = True)
 X_val['part_4_boolean'].fillna(0, inplace = True)
 X_val['part_5_boolean'].fillna(0, inplace = True)
 X_val['part_6_boolean'].fillna(0, inplace = True)
 X_val['part_7_boolean'].fillna(0, inplace = True)
 X_val['type_of_concept_boolean'].fillna(0, inplace = True)
 X_val['type_of_intention_boolean'].fillna(0, inplace = True)
 X_val['type_of_solving_question_boolean'].fillna(0, inplace = True)
 X_val['type_of_starter_boolean'].fillna(0, inplace = True )
col_convert = ['GarageFinish']
df_data[col_convert] = df_data[col_convert].replace('Fin', 3 ).replace('RFn', 2 ).replace('Unf', 1 ).replace('NA', 0)
 df_data[col_convert] = df_data[col_convert].fillna(0 ).astype(int )<data_type_conversions>
params = {
'num_leaves': 31,
'n_estimators': 200,
'max_depth': 8,
'min_child_samples': 356,
'learning_rate': 0.2982483634778906,
'min_data_in_leaf': 82,
'bagging_fraction': 0.6545628633239445,
'feature_fraction': 0.9164482379289846,
'random_state': 666
}
full_model = LGBMClassifier(**params)
 full_model.fit(X, y)
 preds = full_model.predict_proba(X_val)[:,1]
print("LGB roc auc", roc_auc_score(y_val, preds))
full_xgb = XGBClassifier(random_state=666)
 full_xgb.fit(X, y)
 preds = full_xgb.predict_proba(X_val)[:,1]
print("XGB roc auc", roc_auc_score(y_val, preds))
full_lr = LogisticRegression(random_state=666)
 full_lr.fit(X, y)
 preds = full_lr.predict_proba(X_val)[:,1]
print("LR roc auc", roc_auc_score(y_val, preds))
col_convert = ['Fence']
df_data[col_convert] = df_data[col_convert].replace('GdPrv', 4 ).replace('MnPrv', 3 ).replace('GdWo', 2 ).replace('MnWw', 1 ).replace('NA', 0)
 df_data[col_convert] = df_data[col_convert].fillna(0 ).astype(int )<feature_engineering>
import optuna
from optuna.samplers import TPESampler
df_data['GarageYrBlt'] = df_data['GarageYrBlt'].fillna(df_data['YearBuilt'])
 df_data['YearBuiltRemod'] = df_data['YearRemodAdd'] - df_data['YearBuilt']
df_data['YearBuiltSold'] = df_data['YrSold'] - df_data['YearBuilt']
df_data['YearRemodSold'] = df_data['YrSold'] - df_data['YearRemodAdd']
df_data['YearGarageSold'] = df_data['YrSold'] - df_data['GarageYrBlt']<feature_engineering>
rfe = RFE(estimator=DecisionTreeClassifier(random_state=666), n_features_to_select=14)
 rfe.fit(X, y)
 X = rfe.transform(X)
 X_val = rfe.transform(X_val )
df_data['Utilities'] = df_data['Utilities'].fillna('ELO' )<data_type_conversions>
params = {'num_leaves': 29,
'n_estimators': 300,
'max_depth': 9,
'min_child_samples': 1089,
'learning_rate': 0.306030368670154,
'min_data_in_leaf': 65,
'bagging_fraction': 0.49498535405259425,
'feature_fraction': 0.9235503880887722,
'random_state': 666}
df_data['SaleType'] = df_data['SaleType'].fillna('Oth' )<count_values>
model = LGBMClassifier(**params)
 model.fit(X, y)
 preds = model.predict_proba(X_val)[:,1]
roc_auc_score(y_val, preds )
col_fillnas = ['MSZoning', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Electrical', 'Functional']
for col_fillna in col_fillnas: df_data[col_fillna] = df_data[col_fillna].fillna(df_data[col_fillna].value_counts().idxmax() )<data_type_conversions>
X = pd.DataFrame(X)
 X_val = pd.DataFrame(X_val)
 y = pd.DataFrame(y)
 y_val = pd.DataFrame(y_val )
col_fillnas = ['Alley', 'GarageType']
for col_fillna in col_fillnas: df_data[col_fillna] = df_data[col_fillna].fillna('NA' )<data_type_conversions>
models = []
preds = []
for n,(tr, te)in enumerate(KFold(n_splits=5, random_state=666, shuffle=True ).split(y)) :
print(f'Fold {n}')
 model = LGBMClassifier(**params)
 model.fit(X.values[tr], y.values[tr])
 pred = model.predict_proba(X_val)[:, 1]
preds.append(pred)
 print('Fold roc auc:', roc_auc_score(y.values[te], model.predict_proba(X.values[te])[:, 1]))
models.append(model )
col_fillnas = ['GarageCars', 'MiscFeature']
df_data[col_fillnas] = df_data[col_fillnas].fillna(0 )<feature_engineering>
predictions = preds[0]
for i in range(1,5):
predictions += preds[i]
predictions /= 5
print("ROC AUC", roc_auc_score(y_val, predictions))
col_number = df_data.select_dtypes(include=['number'] ).columns.drop(['MiscVal', 'SalePrice', 'YearBuiltRemod', 'YearBuiltSold', 'YearRemodSold', 'YearGarageSold'] ).tolist()
for col_transform in col_number:
skewness = scipy.stats.skew(df_data[col_transform].dropna())
 if skewness > 0.75: df_data[col_transform] = np.log1p(df_data[col_transform] )<feature_engineering>
iter_test = env.iter_test()
<categorify><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 = pd.merge(test_df, user_lecture_stats_part, on=['user_id'], how="left")
 test_df['part_1'].fillna(0, inplace = True)
 test_df['part_2'].fillna(0, inplace = True)
 test_df['part_3'].fillna(0, inplace = True)
 test_df['part_4'].fillna(0, inplace = True)
 test_df['part_5'].fillna(0, inplace = True)
 test_df['part_6'].fillna(0, inplace = True)
 test_df['part_7'].fillna(0, inplace = True)
 test_df['type_of_concept'].fillna(0, inplace = True)
 test_df['type_of_intention'].fillna(0, inplace = True)
 test_df['type_of_solving_question'].fillna(0, inplace = True)
 test_df['type_of_starter'].fillna(0, inplace = True)
 test_df['part_1_boolean'].fillna(0, inplace = True)
 test_df['part_2_boolean'].fillna(0, inplace = True)
 test_df['part_3_boolean'].fillna(0, inplace = True)
 test_df['part_4_boolean'].fillna(0, inplace = True)
 test_df['part_5_boolean'].fillna(0, inplace = True)
 test_df['part_6_boolean'].fillna(0, inplace = True)
 test_df['part_7_boolean'].fillna(0, inplace = True)
 test_df['type_of_concept_boolean'].fillna(0, inplace = True)
 test_df['type_of_intention_boolean'].fillna(0, inplace = True)
 test_df['type_of_solving_question_boolean'].fillna(0, inplace = True)
 test_df['type_of_starter_boolean'].fillna(0, inplace = True)
 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_question_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"])
 full_preds = full_model.predict_proba(test_df[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']])[:, 1]
full_preds_xgb = full_xgb.predict_proba(test_df[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']])[:, 1]
full_preds_lr = full_lr.predict_proba(test_df[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']])[:, 1]
X_test = rfe.transform(test_df[['answered_correctly_user', 'explanation_mean_user', 'quest_pct', 'avg_question_seen',
'prior_question_elapsed_time','prior_question_had_explanation_enc', 'part',
'part_1', 'part_2', 'part_3', 'part_4', 'part_5', 'part_6', 'part_7',
'type_of_concept', 'type_of_intention', 'type_of_solving_question', 'type_of_starter',
'part_1_boolean', 'part_2_boolean', 'part_3_boolean', 'part_4_boolean', 'part_5_boolean', 'part_6_boolean', 'part_7_boolean',
'type_of_concept_boolean', 'type_of_intention_boolean', 'type_of_solving_question_boolean', 'type_of_starter_boolean']])
 preds = [model.predict_proba(X_test)[:,1] for model in models]
predictions = preds[0]
for i in range(1, 5):
predictions += preds[i]
predictions /= 5
test_df['answered_correctly'] = predictions * 0.75 + full_preds * 0.125 + full_preds_xgb * 0.75 + full_preds_lr * 0.05
env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )