Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<create_dataframe>
%matplotlib inline
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<predict_on_test>
train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('Training on CPU...')
 else:
print('Training on GPU...')
test_pred = gbm.predict(test_set)
 test_result = pd.DataFrame(
 {
'Id': test_set_Id,
'SalePrice': np.exp(test_pred),
} )<save_to_csv>
device = torch.device('cuda:0' )
test_result.to_csv('submission_gbm.csv', index=False )<load_from_csv>
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
p_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 p_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )<feature_engineering>
df_train.isna().any().any()
def create_new_columns(dataset):
dataset['total_square_footage'] = dataset['TotalBsmtSF'] + dataset['GrLivArea']
dataset['remodelling_done'] = dataset.apply(lambda x: verify_remodelling(x['YearRemodAdd'], x['YearBuilt']), axis=1)
 dataset['selling_season'] = dataset.apply(lambda x: selling_period(x['MoSold']), axis=1)
 dataset['total_area_of_house'] = dataset['TotalBsmtSF'] + dataset['1stFlrSF'] + dataset['2ndFlrSF']
dataset['age_of_building'] = dataset['YearBuilt'].apply(lambda x: pd.datetime.now().year - x)
 return dataset
def map_ordinal_features(dataset):
for col in ordinal_cat_features:
dataset[col] = dataset[col].map(ordinal_ranking)
 dataset[col] = dataset[col].fillna(0)
 return dataset
def log_continious(dataset):
imp_mean = SimpleImputer(missing_values=np.nan, strategy='mean')
 for col in continious_features:
imp_mean.fit(dataset[col].values.reshape(-1,1))
dataset[col] = imp_mean.transform(dataset[col].values.reshape(-1,1))
dataset[col] = np.log(dataset[col] + 1)
 return dataset
def nominal_one_hot(dataset):
for col in norminal_features:
dataset[col] = dataset[col].astype(str)
 dataset[col] = dataset[col].fillna('Unknown')
 return dataset
def discrete_fill(dataset):
for col in discrete_features:
dataset[col] = dataset[col].fillna(0)
 return dataset
def target_feature(train):
target = np.log(train['SalePrice'] + 1)
 return target<feature_engineering>
train_data = df_train.drop('label',axis=1 ).values
target = target_feature(p_train )<categorify>
train_data = train_data/255.0
df_test = df_test/255.0
pipes = Pipeline(steps = [('new_columns', FunctionTransformer(create_new_columns)) , 
('continious_cleanup', FunctionTransformer(log_continious)) , 
('nominal_filling', FunctionTransformer(nominal_one_hot)) , 
('discrete_filling',FunctionTransformer(discrete_fill)) , 
('ordinal_cleanup', FunctionTransformer(map_ordinal_features)) ,
])
 pipes.fit(p_train )<categorify>
mask = np.random.rand(len(df_train)) < 0.8
df_val = df_train[~mask]
df_train = df_train[mask]
print('Train size: ', df_train.shape)
 print('Val size: ', df_val.shape)
 print('Test size: ', df_test.shape)
 df_train.head()
p_train = pipes.transform(p_train )<categorify>
class DatasetMNIST(torch.utils.data.Dataset):
def __init__(self, data, transform=None):
self.data = data
self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
item = self.data.iloc[index]
image = item[1:].values.astype(np.uint8 ).reshape(( 28, 28))
label = item[0]
if self.transform is not None:
image = self.transform(image)
 return image, label
p_test = pipes.transform(p_test )<split>
train_transform = transforms.Compose(
 [
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=train_data.mean() , std=train_data.std()),
])
 val_transform = transforms.Compose(
 [
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=train_data.mean() , std=train_data.std()),
])
 test_transform = val_transform
p_test_Id = p_test['Id']
p_train = p_train.drop(columns=['SalePrice', 'Id'])
 p_test = p_test.drop(columns = 'Id')
 p_train_x, p_val_x, p_train_y, p_val_y = train_test_split(p_train, target, train_size=0.8, test_size=0.2, random_state = RANDOM_STATE )<categorify>
train_dataset = DatasetMNIST(df_train, transform = train_transform)
 validation_dataset = DatasetMNIST(df_val, transform = val_transform)
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=16)
 validation_loader = torch.utils.data.DataLoader(dataset=validation_dataset, batch_size=16)
categorical_transformer = OneHotEncoder(handle_unknown = "ignore", sparse=False)
 numeric_transformer = Pipeline(steps=[ 
('scaler', StandardScaler())])
 preprocessor = ColumnTransformer(remainder='passthrough',
transformers=[ 
('num', numeric_transformer, selector(dtype_exclude="object")) , 
('cat', categorical_transformer, selector(dtype_include="object"))
] )<choose_model_class>
class CNN(nn.Module):
def __init__(self):
super(CNN,self ).__init__()
self.conv1 = nn.Sequential(
 nn.Conv2d(1, 32, 3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(32),
nn.Conv2d(32, 32, 3, stride=2, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(32),
nn.MaxPool2d(2, 2),
nn.Dropout(0.25)
)
 self.conv2 = nn.Sequential(
 nn.Conv2d(32, 64, 3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(64),
nn.Conv2d(64, 64, 3, stride=2, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(64),
nn.MaxPool2d(2, 2),
nn.Dropout(0.25)
)
 self.conv3 = nn.Sequential(
 nn.Conv2d(64, 128, 3, padding=1),
nn.ReLU() ,
nn.BatchNorm2d(128),
nn.MaxPool2d(2, 2),
nn.Dropout(0.25)
)
 self.fc = nn.Sequential(
 nn.Linear(128, 10), 
)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = x.view(x.size(0), -1)
 return self.fc(x)
 model = CNN().to(device)
params = {'n_estimators': 1000,
'max_depth': 5,
'min_samples_split': 6,
'learning_rate': 0.01,
'loss': 'ls',
'max_features':0.25,
'random_state':RANDOM_STATE}
model_gbm = GradientBoostingRegressor(**params)
 gbm_pipeline = Pipeline(steps=[ 
('preprocess', preprocessor), 
('model', model_gbm)
 ])
 gbm_pipeline.fit(p_train_x, p_train_y)
 gbm_preds = gbm_pipeline.predict(p_val_x)
 score = evaluation_metric(gbm_preds, p_val_y)
 print('RMSE:', score )<save_to_csv>
LEARNING_RATE = 0.001680
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr=LEARNING_RATE )
p_test_gbm = gbm_pipeline.predict(p_test)
 test_result = pd.DataFrame(
 {
'Id': p_test_Id,
'SalePrice': np.exp(p_test_gbm),
})
 test_result.to_csv('pipeline_gbm.csv', index=False)
<install_modules>
def train_model(model,train_loader, validation_loader, optimizer, n_epochs=100):
N_test=len(validation_dataset)
 accuracy_list=[]
loss_list=[]
for epoch in range(n_epochs):
for x, y in train_loader:
x, y = x.to(device), y.to(device)
 model.train()
optimizer.zero_grad()
z = model(x)
 loss = criterion(z, y)
 loss.backward()
optimizer.step()
loss_list.append(loss.data)
 correct=0
for x_test, y_test in validation_loader:
x_test,y_test = x_test.to(device), y_test.to(device)
 model.eval()
z = model(x_test)
 _, yhat = torch.max(z.data, 1)
 correct +=(yhat == y_test ).sum().item()
accuracy = correct / N_test
accuracy_list.append(accuracy)
 return accuracy_list, loss_list
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl > /dev/null 2>&1<set_options>
accuracy_list, loss_list=train_model(model=model,n_epochs=100,train_loader=train_loader,validation_loader=validation_loader,optimizer=optimizer )
_ = np.seterr(divide='ignore', invalid='ignore')
<define_variables>
print(accuracy_list, loss_list )
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>
test_dataset = DatasetMNIST(df_test, transform = val_transform)
 test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=16 )
print('start read train data...')
 train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv', columns=set(data_types_dict.keys())).to_pandas() 
<load_from_csv>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 x_test = np.expand_dims(x_test, axis=1)
 x_test = torch.from_numpy(x_test ).float().to(device)
 x_test.type()
lectures_df = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/lectures.csv')
<categorify>
model.eval()
with torch.no_grad() :
ps = model(x_test)
 prediction = torch.argmax(ps, 1)
 print('Prediction',prediction )
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>
df_export = pd.DataFrame(prediction.cpu().tolist() , columns = ['Label'])
 df_export['ImageId'] = df_export.index +1
df_export = df_export[['ImageId', 'Label']]
df_export.head()
train_lectures = train_df[train_df.content_type_id == True].merge(lectures_df, left_on='content_id', right_on='lecture_id', how='left')
 user_lecture_stats_part = train_lectures.groupby('user_id',as_index = False)[part_lectures_columns + types_of_lectures_columns].sum() 
<data_type_conversions>
df_export.to_csv('output.csv', index=False )