Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 sample_df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )
random_forest = RandomForestClassifier(n_estimators = 100)
 random_forest.fit(X_train, Y_train)
 Y_pred = random_forest.predict(X_test)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest<choose_model_class>
class MnistDataset(Dataset):
def __init__(self, df, transforms=None, train=True):
self.train = train
self.transforms = transforms
self.X = df.loc[:, df.columns != 'label'].to_numpy(float)
 self.X = torch.from_numpy(self.X)
 if train:
self.y = df.get('label' ).to_numpy()
self.y = torch.from_numpy(self.y)
 def __len__(self):
return self.X.shape[0]
def __getitem__(self, i):
x = self.X[i].view(1, 28, 28 ).expand(3, 28, 28)
 if self.transforms: x = self.transforms(x)
 if self.train: return x, self.y[i]
return x
catboost = CatBoostClassifier()
catboost.fit(X_train, Y_train)
 Y_pred = catboost.predict(X_test)
 acc_catboost = round(catboost.score(X_train, Y_train)* 100, 2 )<create_dataframe>
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225] )
models = pd.DataFrame({'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron', 'Stochastic Gradient Decent',
'Linear SVC', 'Decision Tree', 'CatBoost'],
'Score': [acc_svc, acc_knn, acc_log, acc_random_forest, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree, acc_catboost]})
 models.sort_values(by = 'Score', ascending = False, ignore_index = True )<choose_model_class>
ds = MnistDataset(train_df, normalize)
 len(ds )
classifiers = []
classifiers.append(LogisticRegression())
 classifiers.append(SVC())
 classifiers.append(KNeighborsClassifier(n_neighbors = 5))
classifiers.append(GaussianNB())
 classifiers.append(Perceptron())
 classifiers.append(LinearSVC())
 classifiers.append(SGDClassifier())
 classifiers.append(DecisionTreeClassifier())
 classifiers.append(RandomForestClassifier())
 classifiers.append(CatBoostClassifier())
 len(classifiers )<compute_train_metric>
val_len = int(len(ds)*0.01)
 train_len = len(ds)- val_len
train_ds, val_ds = random_split(ds, [train_len, val_len])
 len(train_ds), len(val_ds )
cv_results = []
for classifier in classifiers:
cv_results.append(cross_val_score(classifier, X_train, Y_train, scoring = 'accuracy', cv = 10))<create_dataframe>
bs = 512
num_workers = 2
train_dl = DataLoader(train_ds, bs, num_workers=num_workers)
 val_dl = DataLoader(val_ds, bs, num_workers=num_workers )
cv_res = pd.DataFrame({'Cross Validation Mean': cv_mean, 'Cross Validation Std': cv_std, 'Algorithm': ['Logistic Regression', 'Support Vector Machines', 'KNN', 'Gausian Naive Bayes', 'Perceptron', 'Linear SVC', 'Stochastic Gradient Descent', 'Decision Tree', 'Random Forest', 'CatBoost']})
 cv_res.sort_values(by = 'Cross Validation Mean', ascending = False, ignore_index = True )<train_on_grid>
resnet18 = models.resnet18(pretrained=True)
 resnet18.fc
param_grid = {'learning_rate': [0.03, 0.1],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 3, 5, 7, 9]}
grid = GridSearchCV(CatBoostClassifier() , param_grid = param_grid,
cv = 3, refit=True, verbose = True)
 grid.fit(X_train, Y_train )<train_model>
lin_in = resnet18.fc.in_features
resnet18.fc = nn.Sequential(
 nn.Linear(lin_in, 10)
 )
print("Best parameters: ", grid.best_params_)
 print("Best estimator: ", grid.best_estimator_ )<predict_on_test>
device = torch.device('cuda:0')if torch.cuda.is_available() else 'cpu'
net = net.to(device)
 device
catboost = CatBoostClassifier(depth=10, l2_leaf_reg=5, learning_rate=0.1)
 catboost.fit(X_train, Y_train)
 Y_pred = catboost.predict(X_test)
 acc_catboost = round(catboost.score(X_train, Y_train)* 100, 2 )<compute_train_metric>
epochs = 10
loss_fn = nn.CrossEntropyLoss()
o = optim.Adam(net.parameters() ,lr=0.001 )
cross_val_score(catboost, X_train, Y_train, scoring = 'accuracy', cv = 10 ).mean()<create_dataframe>
bs = 640
test_ds = MnistDataset(test_df, normalize, train = False)
 test_dl = DataLoader(test_ds, bs )
output = pd.DataFrame({'PassengerId': test['PassengerId'], 'Survived': Y_pred} )<save_to_csv>
outputs = []
with torch.no_grad() :
for images in test_dl:
images = images.to(device, dtype=torch.float)
 out = net(images)
 outputs.extend(out.argmax(dim=1 ).tolist() )
<train_model><EOS> 
sample_df['Label'] = outputs
sample_df.to_csv('submission.csv', index=False )