Kaggle competition: Tabular Playground Series - Feb 2021
prev_test_df=None
for(test_df, sample_prediction_df)in iter_test:
test_df=test_df[['row_id', 'user_id', 'content_id',
'content_type_id', 'prior_group_answers_correct']].merge(
 questions_df[['content_id', 'part']],how='left',on='content_id')
 test_df.part=test_df.part.fillna(5)
 update_group(test_df, prev_test_df)
 prev_test_df=test_df.copy()
test_df=test_df[test_df.content_type_id==0]
test_df['prev_seq']=test_df.user_id.apply(lambda user_id: group[user_id] if user_id in group else(np.array([]), np.array([]), np.array([])))
 test_dataset=TestDataset(test_df, max_seq=100)
 test_dataloader=torch.utils.data.DataLoader(test_dataset,
batch_size=40960, shuffle=False, pin_memory=True, num_workers=4)
 model.eval()
y_answered=[]
with torch.no_grad() :
for(q, p, r, label_mask)in test_dataloader:
q=q.to(device)
 p=p.to(device)
 r=r.to(device)
 y=model(q, p, r)
 y_answered.extend([torch.sigmoid(y[idx][label_id] ).cpu().item() for idx, label_id in enumerate(label_mask)])
 test_df['answered_correctly']=y_answered
env.predict(test_df[['row_id', 'answered_correctly']])
 del test_df
gc.collect()<install_modules>
train = pd.read_csv(input_path / 'train.csv', index_col='id')
 display(train.head() )
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1<import_modules>
test = pd.read_csv(input_path / 'test.csv', index_col='id')
 display(test.head() )
import numpy as np
import random
import pandas as pd
import joblib<set_options>
submission = pd.read_csv(input_path / 'sample_submission.csv', index_col='id')
 display(submission.head() )
_ = np.seterr(divide='ignore', invalid='ignore' )<define_variables>
for c in train.columns:
if train[c].dtype=='object':
lbl = LabelEncoder()
lbl.fit(list(train[c].values)+ list(test[c].values))
train[c] = lbl.transform(train[c].values)
 test[c] = lbl.transform(test[c].values)
 display(train.head() )
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'<load_from_csv>
target = train.pop('target' )
print('start read train data...')
 train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv', columns=set(data_types_dict.keys())).to_pandas()<train_model>
scaler = StandardScaler()
train = scaler.fit_transform(train)
 test = scaler.transform(test )
print('start handle lecture data...' )<load_from_csv>
train = DataFrame(train)
 test = DataFrame(test )
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv' )<categorify>
X_train, X_test, y_train, y_test = train_test_split(train, target, train_size=0.90 )
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_')]<merge>
rf = ensemble.RandomForestRegressor()
rf.fit(X_train,y_train)
 y_preds = rf.predict(X_test)
 print(mean_squared_error(y_test,y_preds))
train_lectures = train_df[train_df.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left' )<groupby>
lgbm = LGBMRegressor()
lgbm.fit(X_train,y_train)
 y_pred = lgbm.predict(X_test)
 mse_l = mean_squared_error(y_test,y_pred)
 print(mse_l )
user_lecture_stats_part = train_lectures.groupby('user_id',as_index = False)[part_lectures_columns + types_of_lectures_columns].sum()<data_type_conversions>
xgr = xg.XGBRegressor()
xgr.fit(X_train,y_train)
 y_preds = xgr.predict(X_test)
 print(mean_squared_error(y_test,y_preds))
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 )<data_type_conversions>
estimators=[('RandomForest', rf),('LightGBM',lgbm),('xgboost', xgr)]
ensemble = VotingRegressor(estimators )
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' )<drop_column>
ensemble.fit(X_train,y_train)
 y_preds = ensemble.predict(X_test)
 print(mean_squared_error(y_test,y_preds))
del(train_lectures)
 gc.collect()<categorify>
lgbm.fit(train, target)
 submission['target'] = lgbm.predict(test)
 submission.to_csv('lgbm.csv')