Kaggle competition: Riiid Answer Correctness Prediction
<SOS> metric: AUC Kaggle data source: riiid-answer-correctness-prediction<find_best_model_class>
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1
pca = PCA(n_components=2,svd_solver='full')
 X_pca = pca.fit_transform(X)
 X_reduced, X_test_reduced, Y_reduced, Y_test_reduced = train_test_split(X_pca, Y, test_size = 0.30, random_state = 101)
 start = time.process_time()
trainedforest = RandomForestClassifier(n_estimators=700 ).fit(X_reduced,Y_reduced)
 print(time.process_time() - start)
 predictionforest = trainedforest.predict(X_test_reduced)
 print(confusion_matrix(Y_test_reduced,predictionforest))
print(classification_report(Y_test_reduced,predictionforest))<train_model>
_ = np.seterr(divide='ignore', invalid='ignore' )
model1 = svm.LinearSVC()
model2 = tree.DecisionTreeClassifier()
model3 = RandomForestClassifier()
model = VotingClassifier(estimators=[('svm', model1),('dt', model2),('rf', model3)], voting='hard')
 model.fit(X_Train,Y_Train)
 model.score(X_Test,Y_Test )<choose_model_class>
data_types_dict = {
'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': 'bool'
}
target = 'answered_correctly'
model1 = LogisticRegression()
model2 = tree.DecisionTreeClassifier()
model3 = RandomForestClassifier()
model1.fit(X_Train,Y_Train)
 model2.fit(X_Train,Y_Train)
 model3.fit(X_Train,Y_Train)
 pred1 = model1.predict_proba(X_Test)[:,1]
pred2 = model2.predict_proba(X_Test)[:,1]
pred3 = model3.predict_proba(X_Test)[:,1]
finalpred =(pred1+pred2+pred3)/3
preds = np.where(finalpred > 0.5, 1, 0)
 print(confusion_matrix(Y_Test, preds))
print(classification_report(Y_Test, preds))<choose_model_class>
train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv', columns=set(data_types_dict.keys())).to_pandas()
model1 = LogisticRegression()
model2 = tree.DecisionTreeClassifier()
model3 = RandomForestClassifier()
model1.fit(X_Train,Y_Train)
 model2.fit(X_Train,Y_Train)
 model3.fit(X_Train,Y_Train)
 pred1 = model1.predict_proba(X_Test)[:,1]
pred2 = model2.predict_proba(X_Test)[:,1]
pred3 = model3.predict_proba(X_Test)[:,1]
finalpred=(pred1*0.2+pred2*0.4+pred3*0.4)
 preds = np.where(finalpred > 0.5, 1, 0)
 print(confusion_matrix(Y_Test, preds))
print(classification_report(Y_Test, preds))<import_modules>
train_df = train_df.astype(data_types_dict )
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from xgboost import XGBClassifier
from vecstack import stacking
from sklearn.metrics import accuracy_score<choose_model_class>
content_agg = train_df.groupby('content_id')[target].agg(['sum', 'count','var'] )
random_search = {'criterion': ['entropy', 'gini'],
'max_depth': list(np.linspace(10, 1200, 10, dtype = int)) + [None],
'max_features': ['auto', 'sqrt','log2', None],
'min_samples_leaf': [4, 6, 8, 12],
'min_samples_split': [5, 7, 10, 14],
'n_estimators': list(np.linspace(151, 1200, 10, dtype = int)) }
clf = RandomForestClassifier()
model = RandomizedSearchCV(estimator = clf, param_distributions = random_search, n_iter = 80,
cv = 4, verbose= 5, random_state= 101, n_jobs = -1)
 model.fit(X,Y)
 model.best_params_<choose_model_class>
train_df['content_correctness'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] ).astype("float16" )
random_search = {'criterion': ['entropy', 'gini'],
'max_depth': list(np.linspace(10, 1200, 10, dtype = int)) ,
'max_features': ['auto', 'sqrt','log2', None],
'min_samples_leaf': [4, 6, 8, 12],
'min_samples_split': [5, 7, 10, 14],
'n_estimators': list(np.linspace(151, 1200, 10, dtype = int)) }
clf = XGBClassifier()
model = RandomizedSearchCV(estimator = clf, param_distributions = random_search, n_iter = 80,
cv = 4, verbose= 5, random_state= 101, n_jobs = -1)
 model.fit(X,Y)
 model.best_params_<train_on_grid>
train_df["lag"]=0
train_df.loc[train_df[target] ==0, 'lag'] = train_df['content_correctness']
train_df['lag'] = train_df.groupby(['user_id'])["lag"].shift()
cum = train_df.groupby(['user_id'])['lag'].agg(['cumsum','cumcount'])
 cum.fillna(0,inplace=True)
 train_df["count_user"]=cum["cumcount"].astype("int16")
 train_df['mean_fail_user'] =(cum['cumsum']/(train_df["count_user"])).astype("float16")
 del cum
train_df["lag_std"]=0
train_df['lag'] = train_df.groupby(['user_id'])["lag"].shift(-1)
 train_df.loc[train_df[target] ==0, 'lag_std'] =(( train_df["mean_fail_user"]-train_df["lag"])**2 ).astype("float16")
 train_df["lag_std"] = train_df.groupby(['user_id'])["lag_std"].shift()
cum_std = train_df.groupby(['user_id'])['lag_std'].agg(['cumsum'])
 train_df.drop(columns=['lag_std'], inplace=True)
 train_df.drop(columns=['lag'], inplace=True)
 train_df['deviation_fail_user'] =(cum_std['cumsum']/(train_df["count_user"])).astype("float16")
 del cum_std
random_search = {'dual': [False],
'solver' : ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
'max_iter': list(np.linspace(10, 50000, 100, dtype = int)) }
clf = LogisticRegression()
model = RandomizedSearchCV(estimator = clf, param_distributions = random_search, n_iter = 80,
cv = 4, verbose= 5, random_state= 101, n_jobs = -1)
 model.fit(X,Y)
 model.best_params_<choose_model_class>
train_df['dev_fail_not_divided']=0
train_df.loc[train_df[target] ==0, 'dev_fail_not_divided'] =(( train_df.loc[train_df[target] ==0, 'mean_fail_user']- train_df.loc[train_df[target] ==0, 'content_correctness'])**2 ).astype("float16")
 user_agg_std_fail = train_df.loc[train_df[target]==0].groupby(["user_id"])[ 'dev_fail_not_divided'].agg(['sum'])
 train_df.drop(columns=['dev_fail_not_divided'], inplace=True)
 user_agg_fail = train_df.loc[train_df[target]==0].groupby(["user_id"])['content_correctness'].agg(['sum'] )
models = [
LogisticRegression(solver= 'sag', max_iter = 15024, dual= False),
RandomForestClassifier(n_estimators = 1200, min_samples_split = 5, min_samples_leaf = 4, max_features = None, max_depth = 1200, criterion = 'entropy'),
XGBClassifier(n_estimators = 151, min_samples_split = 10, min_samples_leaf = 6,
max_features = 'auto', max_depth = 10, criterion = 'gini')
 ]<concatenate>
questions_df = pd.read_csv(
 '.. /input/riiid-test-answer-prediction/questions.csv',
usecols=[0, 1,3,4],
dtype={'question_id': 'int16','bundle_id': 'int16', 'part': 'int8','tags': 'str'} 
)
 questions_df['part_bundle_id']=questions_df['part']*100000+questions_df['bundle_id']
questions_df.part_bundle_id=questions_df.part_bundle_id.astype('int32')
 tag = questions_df["tags"].str.split(" ", n = 10, expand = True)
 tag.columns = ['tags1','tags2','tags3','tags4','tags5','tags6']
tag.fillna(0, inplace=True)
 tag = tag.astype('int16')
 questions_df = pd.concat([questions_df,tag],axis=1 ).drop(['tags'],axis=1)
 questions_df.rename(columns={'question_id':'content_id'}, inplace=True )
S_train, S_test = stacking(models,
X_Train, Y_Train, Test_Data,
regression=False,
mode='oof_pred_bag',
needs_proba=False,
save_dir=None,
metric=accuracy_score,
n_folds= 4,
stratified=True,
shuffle=True,
random_state=0,
verbose=2 )<train_model>
train_df=pd.merge(train_df,questions_df[["content_id","part"]],on="content_id",how="left" )
model = XGBClassifier(n_estimators = 151, min_samples_split = 10, min_samples_leaf = 6,
max_features = 'auto', max_depth = 10, criterion = 'gini')
 model = model.fit(S_train, Y_Train)
 Y_Pred = model.predict(S_test)
<set_options>
train_df.drop(columns='content_correctness',inplace=True )
warnings.simplefilter('ignore')
 clf1 = KNeighborsClassifier(n_neighbors=1)
 clf2 = RandomForestClassifier(random_state=1)
 clf3 = GaussianNB()
lr = LogisticRegression()
sclf = StackingClassifier(classifiers=[clf1, clf2, clf3],
use_probas=True,
average_probas=False,
meta_classifier=lr)
 print('3-fold cross validation:
')
 for clf, label in zip([clf1, clf2, clf3, sclf],
['KNN',
'Random Forest',
'Naive Bayes',
'StackingClassifier']):
scores = model_selection.cross_val_score(clf, X, Y,
cv=3, scoring='accuracy')
 print("Accuracy: %0.2f(+/- %0.2f)[%s]"
%(scores.mean() , scores.std() , label))<predict_on_test>
train_df['lag'] = train_df.groupby(['user_id','part'])[target].shift()
cum = train_df.groupby(['user_id','part'])['lag'].agg(['cumsum','cumcount'])
 cum.fillna(0,inplace=True)
 train_df.drop(columns="lag",inplace=True)
 train_df['mean_part_user'] =(cum['cumsum']/cum['cumcount'] ).astype("float16")
 del cum
sclf.fit(X_Train, Y_Train)
 Y_Pred = sclf.predict(X_Test)
 sclf.score(X_Test,Y_Test )<find_best_model_class>
user_agg_part = train_df.groupby(['user_id','part'])[target].agg(['sum','count'] )
X_Train2, X_Val, Y_Train2, Y_Val = train_test_split(X_Train, Y_Train, test_size = 0.30, random_state = 101)
 model1 = tree.DecisionTreeClassifier()
model1.fit(X_Train2,Y_Train2)
 val_pred1=model1.predict(X_Val)
 test_pred1=model1.predict(X_Test)
 val_pred1=pd.DataFrame(val_pred1)
 test_pred1=pd.DataFrame(test_pred1)
 model2 = LogisticRegression()
model2.fit(X_Train2,Y_Train2)
 val_pred2=model2.predict(X_Val)
 test_pred2=model2.predict(X_Test)
 val_pred2=pd.DataFrame(val_pred2)
 test_pred2=pd.DataFrame(test_pred2)
 df_val=pd.concat([pd.DataFrame(X_Val), val_pred1,val_pred2],axis=1)
 df_test=pd.concat([pd.DataFrame(X_Test), test_pred1,test_pred2],axis=1)
 model = LogisticRegression()
model.fit(df_val,Y_Val)
 model.score(df_test,Y_Test )<train_model>
train_df.drop(columns="part",inplace=True )
model = BaggingClassifier(tree.DecisionTreeClassifier(random_state=1))
model.fit(X_Train,Y_Train)
 model.score(X_Test,Y_Test )<train_model>
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv' )
model = AdaBoostClassifier()
model.fit(X_Train,Y_Train)
 model.score(X_Test,Y_Test )<train_model>
lectures_df['type_of'] = lectures_df['type_of'].replace('solving question', 'solving_question')
 lectures_df = pd.get_dummies(lectures_df, columns=['part', 'type_of'])
 part_lectures_columns = [column for column in lectures_df.columns if column.startswith('part')]
types_of_lectures_columns = [column for column in lectures_df.columns if column.startswith('type_of_')]
model= GradientBoostingClassifier(learning_rate=0.01,random_state=1)
 model.fit(X_Train,Y_Train)
 model.score(X_Test,Y_Test )<train_model>
train_lectures = train_df[train_df.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left' )
model=xgb.XGBClassifier(random_state=1,learning_rate=0.01)
 model.fit(X_Train,Y_Train)
 model.score(X_Test,Y_Test )<compute_train_metric>
user_lecture_stats_part = train_lectures.groupby('user_id',as_index = False)[part_lectures_columns + types_of_lectures_columns].sum()
train_data=lgb.Dataset(X_Train,Y_Train)
 params = {'learning_rate':0.2}
model= lgb.train(params, train_data, 100)
 y_pred=model.predict(X_Test)
 preds = np.where(y_pred > 0.5, 1, 0)
 print(confusion_matrix(Y_Test, preds))
print(classification_report(Y_Test, preds))<import_modules>
lecturedata_types_dict = {
'user_id': 'int32',
'part_1': 'int8',
'part_2': 'int8',
'part_3': 'int8',
'part_4': 'int8',
'part_5': 'int8',
'part_6': 'int8',
'part_7': 'int8',
'type_of_concept': 'int8',
'type_of_intention': 'int8',
'type_of_solving_question': 'int8',
'type_of_starter': 'int8'
}
user_lecture_stats_part = user_lecture_stats_part.astype(lecturedata_types_dict )
<train_model>
for column in user_lecture_stats_part.columns:
if(column !='user_id'):
user_lecture_stats_part[column] =(user_lecture_stats_part[column] > 0 ).astype('int8' )
def fit_model(trainX, trainy):
model = Sequential()
model.add(Dense(14, input_dim= 10, activation='relu'))
model.add(Dense(50))
model.add(Dense(10, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.fit(trainX, trainy, epochs=500, verbose=0)
 return model
def define_stacked_model(members):
for i in range(len(members)) :
model = members[i]
for layer in model.layers:
layer.trainable = False
layer.name = 'ensemble_' + str(i+1)+ '_' + layer.name
ensemble_visible = [model.input for model in members]
ensemble_outputs = [model.output for model in members]
merge = concatenate(ensemble_outputs)
 hidden = Dense(10, activation='relu' )(merge)
 output = Dense(2, activation='softmax' )(hidden)
 model = Model(inputs=ensemble_visible, outputs=output)
 plot_model(model, show_shapes=True, to_file='model_graph.png')
 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model
def fit_stacked_model(model, inputX, inputy):
X = [inputX for _ in range(len(model.input)) ]
model.fit(X, inputy, epochs=300, verbose=0)
 def predict_stacked_model(model, inputX):
X = [inputX for _ in range(len(model.input)) ]
return model.predict(X, verbose=0)
 BinY_Train = to_categorical(Y_Train)
 n_members = 5
members = []
for i in range(n_members):
members.append(fit_model(X_Train, BinY_Train))
print('Created %d models' % len(members))
stacked_model = define_stacked_model(members )<predict_on_test>
train_lectures[train_lectures.user_id==5382]
BinY_Test = to_categorical(Y_Test)
 fit_stacked_model(stacked_model, X_Test, BinY_Test)
 yhat = predict_stacked_model(stacked_model, X_Test)
 yhat = argmax(yhat, axis=1)
 acc = accuracy_score(Y_Test, yhat)
 print('Stacked Test Accuracy: %.3f' % acc )<predict_on_test>
user_lecture_stats_part[user_lecture_stats_part.user_id==5382]
n_members = 7
members = []
for i in range(n_members):
members.append(fit_model(X_Train, BinY_Train))
print('Created %d models' % len(members))
stacked_model = define_stacked_model(members)
 fit_stacked_model(stacked_model, X_Test, BinY_Test)
 yhat = predict_stacked_model(stacked_model, Test_Data)
 yhat = argmax(yhat, axis=1 )<save_to_csv>
del(train_lectures )
submission = pd.DataFrame({
"PassengerId": df2["PassengerId"],
"Survived": yhat
})
 submission.to_csv('titanic.csv', index=False)
 submission.head()<set_options>
print("1")
 cum = train_df.groupby('user_id')['content_type_id'].agg(['cumsum', 'cumcount'])
 train_df['user_lecture_cumsum'] = cum['cumsum'].astype('int8')
 train_df['user_lecture_lv'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 user_lecture_agg = train_df.groupby('user_id')['content_type_id'].agg(['sum', 'count'] )
%matplotlib inline<define_variables>
print("1")
 max_timestamp_u = train_df[['user_id','timestamp']].groupby(['user_id'] ).agg(['max'] ).reset_index()
max_timestamp_u.columns = ['user_id', 'max_time_stamp']
test_images = os.listdir(".. /input/iwildcam2020-256/256_images/test/images/")
 train_images = os.listdir(".. /input/iwildcam2020-256/256_images/train/images/" )<load_from_disk>
print("1")
 train_df['lagtime'] = train_df.groupby('user_id')['timestamp'].shift()
train_df['lagtime']=train_df['timestamp']-train_df['lagtime']
train_df['lagtime'].fillna(0, inplace=True)
 train_df.lagtime=train_df.lagtime.astype('int16')
with open(r'/kaggle/input/iwildcam-2020-fgvc7/iwildcam2020_train_annotations.json')as json_file:
train_data = json.load(json_file )<prepare_output>
print("1")
 lagtime_agg = train_df.groupby('user_id')['lagtime'].agg(['mean'])
 train_df['lagtime_mean'] = train_df['user_id'].map(lagtime_agg['mean'])
 train_df.lagtime_mean=train_df.lagtime_mean.astype('float16' )
df_train = pd.DataFrame({'id': [item['id'] for item in train_data['annotations']],
'category_id': [item['category_id'] for item in train_data['annotations']],
'image_id': [item['image_id'] for item in train_data['annotations']],
'location': [item['location'] for item in train_data['images']],
'file_name': [item['file_name'] for item in train_data['images']]})
 df_train.head()<filter>
print("1")
 user_prior_question_elapsed_time = train_df[['user_id','prior_question_elapsed_time']].groupby(['user_id'] ).tail(1)
 user_prior_question_elapsed_time.columns = ['user_id', 'prior_question_elapsed_time']
df_train = df_train[df_train['file_name'].isin(train_images)]<feature_engineering>
print("1")
 train_df['delta_prior_question_elapsed_time'] = train_df.groupby('user_id')['prior_question_elapsed_time'].shift()
train_df['delta_prior_question_elapsed_time'].fillna(0, inplace=True)
 train_df['delta_prior_question_elapsed_time']=(train_df['prior_question_elapsed_time']-train_df['delta_prior_question_elapsed_time'] ).astype('int16')
cat_images = dict()
cat_count = dict()
annotations = train_data['annotations']
_images = train_data['images']
for i, annotation in enumerate(annotations):
_img = annotation['image_id']
cat = annotation['category_id']
imgs = cat_images.get(cat, None)
 if imgs is None:
cat_images[cat] = [{'image_id': _img, 'category': cat}]
else:
cat_images[cat].append({'image_id': _img, 'category': cat})
 count = cat_count.get(cat, 0)
 if count == 0:
cat_count[cat] = 1
else:
cat_count[cat] += 1
n_train = dict()
n_val = dict()
for cat, count in cat_count.items() :
_train = math.floor(count * 0.70)
 if _train < 1:
_train = 1
_val = count - _train
n_train[cat] = _train
n_val[cat] = _val
train_images = []
val_images = []
for cat in cat_images.keys() :
random.shuffle(cat_images[cat])
 train_images += cat_images[cat][:n_train[cat]]
val_images += cat_images[cat][n_train[cat]:]
val_img_dt = pd.DataFrame(val_images)
<feature_engineering>
train_df['timestamp']=(train_df['timestamp']/(1000*3600)).astype('int16')
df_train['is_valid'] = np.where(df_train.image_id.isin(val_img_dt['image_id']), True, False )<groupby>
train_df['lag'] = train_df.groupby('user_id')[target].shift()
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 cum.fillna(0,inplace=True)
 train_df['user_correctness'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 train_df['user_correct_cumsum'] = cum['cumsum'].astype('int16')
 train_df['user_correct_cumcount'] = cum['cumcount'].astype('int16')
 train_df.drop(columns=['lag'], inplace=True)
 train_df['user_correct_cumsum'].fillna(0, inplace=True)
df_train.groupby('is_valid' ).size()<drop_column>
train_df.prior_question_had_explanation=train_df.prior_question_had_explanation.astype('int8')
 train_df['lag'] = train_df.groupby('user_id')['prior_question_had_explanation'].shift()
df_train.drop(df_train.loc[df_train['file_name']=='87022118-21bc-11ea-a13a-137349068a90.jpg'].index, inplace=True)
 df_train.drop(df_train.loc[df_train['file_name']=='8792549a-21bc-11ea-a13a-137349068a90.jpg'].index, inplace=True )<count_unique_values>
cum = train_df.groupby('user_id')['lag'].agg(['cumsum', 'cumcount'])
 cum.fillna(0,inplace=True)
 train_df['explanation_mean'] =(cum['cumsum'] / cum['cumcount'] ).astype('float16')
 train_df['explanation_cumsum'] = cum['cumsum'].astype('int16')
 train_df.drop(columns=['lag'], inplace=True)
 train_df['explanation_mean'].fillna(0, inplace=True)
 train_df['explanation_cumsum'].fillna(0, inplace=True)
df_train.category_id.unique().shape<load_from_disk>
del cum
gc.collect()
with open(r'/kaggle/input/iwildcam-2020-fgvc7/iwildcam2020_test_information.json')as f:
test_data = json.load(f )<prepare_output>
train_df["attempt_no"] = 1
train_df.attempt_no=train_df.attempt_no.astype('int8')
 train_df["attempt_no"] = train_df[["user_id","content_id",'attempt_no']].groupby(["user_id","content_id"])["attempt_no"].cumsum()
df_test = pd.DataFrame.from_records(test_data['images'])
 df_test.head()<categorify>
explanation_agg = train_df.groupby('user_id')['prior_question_had_explanation'].agg(['sum', 'count'])
 explanation_agg=explanation_agg.astype('int16')
train, test = [ImageList.from_df(df, path='.. /input/iwildcam2020-256/256_images/', cols='file_name', folder=folder, suffix='')
 for df, folder in zip([df_train, df_test], ['train/images', 'test/images'])]
trfm = get_transforms(max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4,
p_affine=1., p_lighting=1.)
src =(train.use_partial_data(1)
.split_from_df(col='is_valid')
.label_from_df(cols='category_id')
.add_test(test))
data =(src.transform(trfm, size = 128, padding_mode = 'reflection')
.databunch(path=Path('.'), bs = 256 ).normalize(imagenet_stats))<create_dataframe>
user_agg = train_df.groupby('user_id')[target].agg(['sum', 'count'])
 task_container_agg = train_df.groupby('task_container_id')[target].agg(['sum', 'count','var'])
org_classes = pd.DataFrame({"org_category": data.classes})
 org_classes['Category'] = org_classes.index<define_variables>
user_agg=user_agg.astype('int16')
 content_agg=content_agg.astype('float32')
 task_container_agg=task_container_agg.astype('float32' )
data.show_batch()<choose_model_class>
attempt_no_agg=train_df.groupby(["user_id","content_id"])["attempt_no"].agg(['sum'])
 attempt_no_agg=attempt_no_agg.astype('int8')
learn = cnn_learner(data, base_arch=models.resnet50, metrics=accuracy ).mixup()<train_model>
train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_sum'] = train_df['content_id'].map(content_agg['sum'] ).astype('int32')
 train_df['content_correctness'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] ).astype("float16")
 train_df.content_correctness=train_df.content_correctness.astype('float16')
 train_df['task_container_sum'] = train_df['task_container_id'].map(task_container_agg['sum'] ).astype('int32')
 train_df['task_container_std'] = train_df['task_container_id'].map(task_container_agg['var'] ).astype('float16')
 train_df['task_container_correctness'] = train_df['task_container_id'].map(task_container_agg['sum'] / task_container_agg['count'])
 train_df.task_container_correctness=train_df.task_container_correctness.astype('float16' )
learn.fit_one_cycle(10, slice(0.01))<train_model>
questions_df['content_correctness'] = questions_df['content_id'].map(content_agg['sum'] / content_agg['count'])
 questions_df.content_correctness=questions_df.content_correctness.astype('float16')
 questions_df['content_correctness_std'] = questions_df['content_id'].map(content_agg['var'])
 questions_df.content_correctness_std=questions_df.content_correctness_std.astype('float16' )
learn.fit_one_cycle(10, slice(1e-5, 1e-4))<feature_engineering>
part_agg = questions_df.groupby('part')['content_correctness'].agg(['mean', 'var'])
 questions_df['part_correctness_mean'] = questions_df['part'].map(part_agg['mean'])
 questions_df['part_correctness_std'] = questions_df['part'].map(part_agg['var'])
 questions_df.part_correctness_mean=questions_df.part_correctness_mean.astype('float16')
 questions_df.part_correctness_std=questions_df.part_correctness_std.astype('float16' )
preds,y = learn.TTA(ds_type=DatasetType.Test )<save_to_csv>
bundle_agg = questions_df.groupby('bundle_id')['content_correctness'].agg(['mean'])
 questions_df['bundle_correctness'] = questions_df['bundle_id'].map(bundle_agg['mean'])
 questions_df.bundle_correctness=questions_df.bundle_correctness.astype('float16' )
pred_csv = pd.DataFrame(preds.numpy())
 pred_csv['Id'] = learn.data.test_ds.items
pred_csv.to_csv("outout_preds.csv", index = False )<load_from_csv>
tags1_agg = questions_df.groupby('tags1')['content_correctness'].agg(['mean', 'var'])
 questions_df['tags1_correctness_mean'] = questions_df['tags1'].map(tags1_agg['mean'])
 questions_df['tags1_correctness_std'] = questions_df['tags1'].map(tags1_agg['var'])
 questions_df.tags1_correctness_mean=questions_df.tags1_correctness_mean.astype('float16')
 questions_df.tags1_correctness_std=questions_df.tags1_correctness_std.astype('float16' )
submission = pd.read_csv('.. /input/iwildcam-2020-fgvc7/sample_submission.csv')
 id_list = list(submission.Id)
 pred_list = list(np.argmax(preds.numpy() , axis=1))
pred_dict = dict(( key, value.item())for(key, value)in zip(learn.data.test_ds.items,pred_list))
pred_ordered = [pred_dict['.. /input/iwildcam2020-256/256_images/test/images/' + id + '.jpg'] for id in id_list]
submission_with_idx = pd.DataFrame({'Id':id_list,'Category':pred_ordered})
 submission_fixed_labels = pd.merge(submission_with_idx, org_classes, on = 'Category', how='left')
 submission_fixed_labels = submission_fixed_labels.drop(['Category'], axis = 1)
 submission_fixed_labels.rename(columns={'org_category': 'Category'}, inplace=True)
 submission_fixed_labels.to_csv("submission.csv".format(Category),index = False)
 print("Done" )<import_modules>
questions_df.drop(columns=['content_correctness'], inplace=True )
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Input, Conv2D, Dense, Activation, Flatten, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D, MaxPooling2D, AveragePooling2D, BatchNormalization, LeakyReLU, Concatenate
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint<load_from_csv>
del bundle_agg
del part_agg
del tags1_agg
gc.collect()
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<split>
train_df['user_correctness'].fillna(1, inplace=True)
 train_df['attempt_no'].fillna(1, inplace=True)
 train_df.fillna(0, inplace=True )
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size = 0.1, random_state=1337 )<choose_model_class>
MAX_SEQ = 160
skills = train_df["content_id"].unique()
n_skill = len(skills)
 print("number skills", len(skills))
group = train_df[['user_id', 'content_id', 'answered_correctly']].groupby('user_id' ).apply(lambda r:(
 r['content_id'].values,
r['answered_correctly'].values))
for user_id in group.index:
q, qa = group[user_id]
if len(q)>MAX_SEQ:
group[user_id] =(q[-MAX_SEQ:],qa[-MAX_SEQ:])
 pickle.dump(group, open("group.pkl", "wb"))
del group
gc.collect()
rlr = ReduceLROnPlateau(monitor='accuracy', mode = 'max', factor=0.5, min_lr=1e-7, verbose = 1, patience=5)
 es = EarlyStopping(monitor='accuracy', mode='max', verbose = 1, patience=50)
 mc = ModelCheckpoint('cnn_best_model.h5', monitor='accuracy', mode='max', verbose = 1, save_best_only=True )<choose_model_class>
features = [
'lagtime',
'lagtime_mean',
'user_lecture_cumsum',
'user_lecture_lv',
'prior_question_elapsed_time',
'delta_prior_question_elapsed_time',
'user_correctness',
'user_correct_cumcount',
'user_correct_cumsum',
'content_correctness',
'content_count',
'content_sum',
'task_container_correctness',
'bundle_correctness',
'attempt_no',
'part',
'part_correctness_mean',
'tags1',
'tags1_correctness_mean',
'bundle_id',
'explanation_mean',
'explanation_cumsum',
'prior_question_had_explanation',
'mean_fail_user',
'deviation_fail_user',
'count_user',
'mean_part_user',
]
categorical_columns= [
'part',
'tags1',
'bundle_id',
'prior_question_had_explanation',
]
def build_model(lr = 0, mt = 0, dr = 0):
model = Sequential(name = 'cnn_mnist')
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(dr))
model.add(Dense(10, activation = "softmax"))
opt = optimizers.SGD(lr = lr, momentum = mt)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 return model<choose_model_class>
flag_lgbm=True
clfs = list()
params = {
'num_leaves': 350,
'max_bin':700,
'min_child_weight': 0.03454472573214212,
'feature_fraction': 0.58,
'bagging_fraction': 0.58,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.05,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3899927210061127,
'reg_lambda': 0.6485237330340494,
'random_state': 47
}
trains=list()
valids=list()
num=1
for i in range(0,num):
train_df_clf=train_df.sample(n=15000*1000)
 print('sample end')
 del train_df
users=train_df_clf['user_id'].drop_duplicates()
users=users.sample(frac=0.025)
 users_df=pd.DataFrame()
users_df['user_id']=users.values
valid_df_newuser = pd.merge(train_df_clf, users_df, on=['user_id'], how='inner',right_index=True)
 del users_df
del users
gc.collect()
train_df_clf.drop(valid_df_newuser.index, inplace=True)
 train_df_clf = pd.merge(train_df_clf, questions_df, on='content_id', how='left',right_index=True)
 valid_df_newuser = pd.merge(valid_df_newuser, questions_df, on='content_id', how='left',right_index=True)
 valid_df=train_df_clf.sample(frac=0.09)
 train_df_clf.drop(valid_df.index, inplace=True)
 valid_df = valid_df.append(valid_df_newuser)
 del valid_df_newuser
gc.collect()
trains.append(train_df_clf)
 valids.append(valid_df)
 print('valid_df length：',len(valid_df))
model = build_model(lr = 0.01, mt = 0.9, dr = 0.5 )<choose_model_class>
del train_df_clf
del valid_df
gc.collect()
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
horizontal_flip=False,
vertical_flip=False 
)
 datagen.fit(X_train )<train_model>
for i in range(0,num):
tr_data = lgb.Dataset(trains[i][features], label=trains[i][target])
 va_data = lgb.Dataset(valids[i][features], label=valids[i][target])
 del trains
del valids
gc.collect()
model = lgb.train(
 params,
tr_data,
num_boost_round=5000,
valid_sets=[tr_data, va_data],
early_stopping_rounds=50,
feature_name=features,
categorical_feature=categorical_columns,
verbose_eval=50 
)
 clfs.append(model)
 lgb.plot_importance(model, importance_type='gain')
 plt.show()
del tr_data
del va_data
gc.collect()
model.fit_generator(datagen.flow(X_train, y_train, batch_size = 64),
validation_data =(X_valid, y_valid),
steps_per_epoch = X_train.shape[0] // 64,
epochs = 400, verbose = 2,
callbacks = [rlr, es, mc] )<save_to_csv>
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))
del user_agg
del content_agg
gc.collect()
task_container_sum_dict = task_container_agg['sum'].astype('int32' ).to_dict(defaultdict(int))
task_container_count_dict = task_container_agg['count'].astype('int32' ).to_dict(defaultdict(int))
task_container_std_dict = task_container_agg['var'].astype('float16' ).to_dict(defaultdict(int))
explanation_sum_dict = explanation_agg['sum'].astype('int16' ).to_dict(defaultdict(int))
explanation_count_dict = explanation_agg['count'].astype('int16' ).to_dict(defaultdict(int))
del task_container_agg
del explanation_agg
user_sum_part_dict =user_agg_part['sum'].astype('float32' ).to_dict(defaultdict(int))
user_count_part_dict = user_agg_part['count'].astype('int32' ).to_dict(defaultdict(int))
user_sum_fail_dict =user_agg_fail['sum'].astype('float32' ).to_dict(defaultdict(int))
user_sum_std_fail_dict = user_agg_std_fail['sum'].astype('float32' ).to_dict(defaultdict(int))
gc.collect()
saved_model = load_model('cnn_best_model.h5')
 y_pred = saved_model.predict_classes(X_test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1, len(y_pred)+1)) ,
"Label": y_pred})
 submissions.to_csv("submission.csv", index=False )<import_modules>
user_lecture_sum_dict = user_lecture_agg['sum'].astype('int16' ).to_dict(defaultdict(int))
user_lecture_count_dict = user_lecture_agg['count'].astype('int16' ).to_dict(defaultdict(int))
lagtime_mean_dict = lagtime_agg['mean'].astype('int32' ).to_dict(defaultdict(int))
del user_lecture_agg
del lagtime_agg
gc.collect()
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_addons as tfa
import PIL
from sklearn.model_selection import train_test_split<load_from_csv>
attempt_no_agg=attempt_no_agg[attempt_no_agg['sum'] >1]
attempt_no_sum_dict = attempt_no_agg['sum'].to_dict(defaultdict(int))
del attempt_no_agg
gc.collect()
ROOT_DIR = '/kaggle/input/digit-recognizer'
train_df = pd.read_csv(os.path.join(ROOT_DIR, 'train.csv'))
test_df = pd.read_csv(os.path.join(ROOT_DIR, 'test.csv'))<data_type_conversions>
max_timestamp_u_dict=max_timestamp_u.set_index('user_id' ).to_dict()
user_prior_question_elapsed_time_dict=user_prior_question_elapsed_time.set_index('user_id' ).to_dict()
del max_timestamp_u
del user_prior_question_elapsed_time
gc.collect()
feature_train = train_df.to_numpy() [:,1:]
label_train = train_df.to_numpy() [:,0]
feature_test = test_df.to_numpy()<split>
def get_max_attempt(user_id,content_id):
k =(user_id,content_id)
 if k in attempt_no_sum_dict.keys() :
attempt_no_sum_dict[k]+=1
return attempt_no_sum_dict[k]
attempt_no_sum_dict[k] = 1
return attempt_no_sum_dict[k]
feature_train, feature_val, label_train, label_val = train_test_split(feature_train, label_train, test_size=0.2, stratify=label_train )<train_model>
class FFN(nn.Module):
def __init__(self, state_size=200):
super(FFN, self ).__init__()
self.state_size = state_size
self.lr1 = nn.Linear(state_size, state_size)
 self.relu = nn.ReLU()
self.lr2 = nn.Linear(state_size, state_size)
 self.dropout = nn.Dropout(0.2)
 def forward(self, x):
x = self.lr1(x)
 x = self.relu(x)
 x = self.lr2(x)
 return self.dropout(x)
 def future_mask(seq_length):
future_mask = np.triu(np.ones(( seq_length, seq_length)) , k=1 ).astype('bool')
 return torch.from_numpy(future_mask)
 class SAKTModel(nn.Module):
def __init__(self, n_skill, max_seq=MAX_SEQ, embed_dim=128):
super(SAKTModel, self ).__init__()
self.n_skill = n_skill
self.embed_dim = embed_dim
self.embedding = nn.Embedding(2*n_skill+1, embed_dim)
 self.pos_embedding = nn.Embedding(max_seq-1, embed_dim)
 self.e_embedding = nn.Embedding(n_skill+1, embed_dim)
 self.multi_att = nn.MultiheadAttention(embed_dim=embed_dim, num_heads=8, dropout=0.2)
 self.dropout = nn.Dropout(0.2)
 self.layer_normal = nn.LayerNorm(embed_dim)
 self.ffn = FFN(embed_dim)
 self.pred = nn.Linear(embed_dim, 1)
 def forward(self, x, question_ids):
device = x.device
x = self.embedding(x)
 pos_id = torch.arange(x.size(1)).unsqueeze(0 ).to(device)
 pos_x = self.pos_embedding(pos_id)
 x = x + pos_x
e = self.e_embedding(question_ids)
 x = x.permute(1, 0, 2)
 e = e.permute(1, 0, 2)
 att_mask = future_mask(x.size(0)).to(device)
 att_output, att_weight = self.multi_att(e, x, x, attn_mask=att_mask)
 att_output = self.layer_normal(att_output + e)
 att_output = att_output.permute(1, 0, 2)
 x = self.ffn(att_output)
 x = self.layer_normal(x + att_output)
 x = self.pred(x)
 return x.squeeze(-1), att_weight
class TestDataset(Dataset):
def __init__(self, samples, test_df, skills, max_seq=MAX_SEQ):
super(TestDataset, self ).__init__()
self.samples = samples
self.user_ids = [x for x in test_df["user_id"].unique() ]
self.test_df = test_df
self.skills = skills
self.n_skill = len(skills)
 self.max_seq = max_seq
def __len__(self):
return self.test_df.shape[0]
def __getitem__(self, index):
test_info = self.test_df.iloc[index]
user_id = test_info["user_id"]
target_id = test_info["content_id"]
q = np.zeros(self.max_seq, dtype=int)
 qa = np.zeros(self.max_seq, dtype=int)
 if user_id in self.samples.index:
q_, qa_ = self.samples[user_id]
seq_len = len(q_)
 if seq_len >= self.max_seq:
q = q_[-self.max_seq:]
qa = qa_[-self.max_seq:]
else:
q[-seq_len:] = q_
qa[-seq_len:] = qa_
x = np.zeros(self.max_seq-1, dtype=int)
 x = q[1:].copy()
x +=(qa[1:] == 1)* self.n_skill
questions = np.append(q[2:], [target_id])
 return x, questions
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 SAKT_model = SAKTModel(n_skill, embed_dim=128)
 try:
SAKT_model.load_state_dict(torch.load(".. /input/sakt-with-randomization-state-updates/SAKT-HDKIM.pt"))
except:
SAKT_model.load_state_dict(torch.load(".. /input/sakt-with-randomization-state-updates/SAKT-HDKIM.pt", map_location='cpu'))
SAKT_model.to(device)
 SAKT_model.eval()
group = pickle.load(open("group.pkl", "rb"))
print(psutil.virtual_memory().percent)
print('Train size: {}, validation size: {}'.format(len(label_train), len(label_val)) )<load_pretrained>
iter_test = env.iter_test()
prior_test_df = None
<define_variables><EOS> 
%%time
for(test_df, sample_prediction_df)in iter_test:
test_df=test_df.merge(questions_df.loc[questions_df.index.isin(test_df['content_id'])],
how='left', on='content_id', right_index=True)
 if(prior_test_df is not None)&(psutil.virtual_memory().percent<90):
print(psutil.virtual_memory().percent)
 prior_test_df[target] = eval(test_df['prior_group_answers_correct'].iloc[0])
 prior_test_df = prior_test_df[prior_test_df[target] != -1].reset_index(drop=True)
 prior_test_df['prior_question_had_explanation'].fillna(False, inplace=True)
 prior_test_df.prior_question_had_explanation=prior_test_df.prior_question_had_explanation.astype('int8')
 prev_group = prior_test_df[['user_id', 'content_id', 'answered_correctly']].groupby('user_id' ).apply(lambda r:(
 r['content_id'].values,
r['answered_correctly'].values))
for prev_user_id in prev_group.index:
prev_group_content = prev_group[prev_user_id][0]
prev_group_ac = prev_group[prev_user_id][1]
if prev_user_id in group.index:
group[prev_user_id] =(np.append(group[prev_user_id][0],prev_group_content),
np.append(group[prev_user_id][1],prev_group_ac))
else:
group[prev_user_id] =(prev_group_content,prev_group_ac)
 if len(group[prev_user_id][0])>MAX_SEQ:
new_group_content = group[prev_user_id][0][-MAX_SEQ:]
new_group_ac = group[prev_user_id][1][-MAX_SEQ:]
group[prev_user_id] =(new_group_content,new_group_ac)
 parts = prior_test_df['part'].values
user_ids = prior_test_df['user_id'].values
content_ids = prior_test_df['content_id'].values
task_container_ids = prior_test_df['task_container_id'].values
prior_question_had_explanations = prior_test_df['prior_question_had_explanation'].values
targets = prior_test_df[target].values
for user_id, content_id,prior_question_had_explanation,task_container_id,answered_correctly, part in zip(user_ids, content_ids, prior_question_had_explanations,task_container_ids,targets, parts):
user_sum_part_dict[user_id,part] += answered_correctly
user_count_part_dict[user_id,part]+= 1
user_sum_dict[user_id] += answered_correctly
user_count_dict[user_id] += 1
explanation_sum_dict[user_id] += prior_question_had_explanation
explanation_count_dict[user_id] += 1
user_sum_fail_dict[user_id]+=(1-answered_correctly)*(content_sum_dict[content_id]/content_count_dict[content_id])
 user_sum_std_fail_dict[user_id]+=(1-answered_correctly)*(( user_sum_fail_dict[user_id]/(user_count_dict[user_id])) -(content_sum_dict[content_id]/content_count_dict[content_id])) **2
prior_test_df = test_df.copy()
lecture_test_df = test_df[test_df['content_type_id'] == 1].reset_index(drop=True)
 for i,(user_id,content_type_id, content_id)in enumerate(zip(lecture_test_df['user_id'].values,lecture_test_df['content_type_id'].values,lecture_test_df['content_id'].values)) :
user_lecture_sum_dict[user_id] += content_type_id
user_lecture_count_dict[user_id] += 1
if(len(user_lecture_stats_part[user_lecture_stats_part.user_id==user_id])==0):
user_lecture_stats_part = user_lecture_stats_part.append([{'user_id':user_id}], ignore_index=True)
 user_lecture_stats_part.fillna(0, inplace=True)
 user_lecture_stats_part.loc[user_lecture_stats_part.user_id==user_id,part_lectures_columns + types_of_lectures_columns]+=lectures_df[lectures_df.lecture_id==content_id][part_lectures_columns + types_of_lectures_columns].values
else:
user_lecture_stats_part.loc[user_lecture_stats_part.user_id==user_id,part_lectures_columns + types_of_lectures_columns]+=lectures_df[lectures_df.lecture_id==content_id][part_lectures_columns + types_of_lectures_columns].values
test_df = test_df[test_df['content_type_id'] == 0].reset_index(drop=True)
 test_dataset = TestDataset(group, test_df, skills)
 test_dataloader = DataLoader(test_dataset, batch_size=51200, shuffle=False)
 SAKT_outs = []
for item in test_dataloader:
x = item[0].to(device ).long()
target_id = item[1].to(device ).long()
with torch.no_grad() :
output, att_weight = SAKT_model(x, target_id)
 output = torch.sigmoid(output)
 output = output[:, -1]
SAKT_outs.extend(output.view(-1 ).data.cpu().numpy())
 test_df['prior_question_had_explanation'].fillna(False, inplace=True)
 test_df.prior_question_had_explanation=test_df.prior_question_had_explanation.astype('int8')
 test_df['prior_question_elapsed_time'].fillna(prior_question_elapsed_time_mean, inplace=True)
 user_sum_part = np.zeros(len(test_df), dtype=np.float32)
 user_count_part = np.zeros(len(test_df), dtype=np.int32)
 user_sum_fail=np.zeros(len(test_df), dtype=np.float32)
 user_sum_std_fail = np.zeros(len(test_df), dtype=np.float32)
 user_lecture_sum = np.zeros(len(test_df), dtype=np.int16)
 user_lecture_count = np.zeros(len(test_df), dtype=np.int16)
 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)
 task_container_sum = np.zeros(len(test_df), dtype=np.int32)
 task_container_count = np.zeros(len(test_df), dtype=np.int32)
 task_container_std = np.zeros(len(test_df), dtype=np.float16)
 content_task_mean = np.zeros(len(test_df), dtype=np.float16)
 explanation_sum = np.zeros(len(test_df), dtype=np.int32)
 explanation_count = np.zeros(len(test_df), dtype=np.int32)
 delta_prior_question_elapsed_time = np.zeros(len(test_df), dtype=np.int32)
 attempt_no_count = np.zeros(len(test_df), dtype=np.int16)
 lagtime = np.zeros(len(test_df), dtype=np.int32)
 lagtime_mean = np.zeros(len(test_df), dtype=np.int32)
 for i,(user_id,prior_question_had_explanation,content_type_id,prior_question_elapsed_time,timestamp, content_id,task_container_id,part)in enumerate(zip(test_df['user_id'].values,test_df['prior_question_had_explanation'].values,test_df['content_type_id'].values,test_df['prior_question_elapsed_time'].values,test_df['timestamp'].values, test_df['content_id'].values, test_df['task_container_id'].values,test_df['part'].values)) :
user_sum_part[i] = user_sum_part_dict[user_id,part]
user_count_part[i] = user_count_part_dict[user_id,part]
user_sum_std_fail[i] = user_sum_std_fail_dict[user_id]
user_sum_fail[i] = user_sum_fail_dict[user_id]
user_lecture_sum_dict[user_id] += content_type_id
user_lecture_count_dict[user_id] += 1
user_lecture_sum[i] = user_lecture_sum_dict[user_id]
user_lecture_count[i] = user_lecture_count_dict[user_id]
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]
task_container_sum[i] = task_container_sum_dict[task_container_id]
task_container_count[i] = task_container_count_dict[task_container_id]
task_container_std[i]=task_container_std_dict[task_container_id]
explanation_sum[i] = explanation_sum_dict[user_id]
explanation_count[i] = explanation_count_dict[user_id]
if user_id in max_timestamp_u_dict['max_time_stamp'].keys() :
lagtime[i]=timestamp-max_timestamp_u_dict['max_time_stamp'][user_id]
max_timestamp_u_dict['max_time_stamp'][user_id]=timestamp
lagtime_mean[i]=(lagtime_mean_dict[user_id]+lagtime[i])/2
else:
lagtime[i]=0
max_timestamp_u_dict['max_time_stamp'].update({user_id:timestamp})
 lagtime_mean_dict.update({user_id:timestamp})
 lagtime_mean[i]=(lagtime_mean_dict[user_id]+lagtime[i])/2
if user_id in user_prior_question_elapsed_time_dict['prior_question_elapsed_time'].keys() :
delta_prior_question_elapsed_time[i]=prior_question_elapsed_time-user_prior_question_elapsed_time_dict['prior_question_elapsed_time'][user_id]
user_prior_question_elapsed_time_dict['prior_question_elapsed_time'][user_id]=prior_question_elapsed_time
else:
delta_prior_question_elapsed_time[i]=0
user_prior_question_elapsed_time_dict['prior_question_elapsed_time'].update({user_id:prior_question_elapsed_time})
 test_df['mean_part_user'] = user_sum_part/(user_count_part)
 test_df['mean_fail_user'] = user_sum_fail/user_count
test_df[ 'deviation_fail_user']=user_sum_std_fail/user_count
test_df["count_user"]=user_count
test_df['user_lecture_lv'] = user_lecture_sum / user_lecture_count
test_df['user_lecture_cumsum'] = user_lecture_sum
test_df['user_correctness'] = user_sum / user_count
test_df['user_correct_cumcount'] =user_count
test_df['user_correct_cumsum'] =user_sum
test_df['content_correctness'] = content_sum / content_count
test_df['content_count'] = content_count
test_df['content_sum'] = content_sum
test_df['task_container_correctness'] = task_container_sum / task_container_count
test_df['task_container_sum'] = task_container_sum
test_df['task_container_std'] = task_container_std
test_df['explanation_mean'] = explanation_sum / explanation_count
test_df['explanation_cumsum'] = explanation_sum
test_df['delta_prior_question_elapsed_time'] = delta_prior_question_elapsed_time
test_df["attempt_no"] = test_df[["user_id", "content_id"]].apply(lambda row: get_max_attempt(row["user_id"], row["content_id"]), axis=1)
 test_df["lagtime"]=lagtime
test_df["lagtime_mean"]=lagtime_mean
test_df['user_correctness'].fillna(1, inplace=True)
 test_df['attempt_no'].fillna(1, inplace=True)
 test_df.fillna(0, inplace=True)
 test_df['timestamp']=test_df['timestamp']/(1000*3600)
 test_df.timestamp=test_df.timestamp.astype('int16')
 sub_preds = np.zeros(test_df.shape[0])
 for i, model in enumerate(clfs, 1):
test_preds = model.predict(test_df[features])
 sub_preds += test_preds
lgbm_final = sub_preds / len(clfs)
 test_df[target] = np.array(SAKT_outs)* 0.5 + lgbm_final * 0.5
env.predict(test_df[['row_id', target]] )