Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<sort_values>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
combine[['Title', 'Survived']].groupby(['Title'], as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<drop_column>
print(torch.cuda.is_available() , torch.backends.cudnn.enabled )
combine = combine.drop('Name', axis = 1)
 combine.head()<feature_engineering>
train_df = pd.read_csv(path/"train.csv")
 train_df.head()
combine['Family_Size'] = combine['SibSp'] + combine['Parch'] + 1
combine[['SibSp', 'Parch', 'Family_Size']].head(10 )<sort_values>
test_df = pd.read_csv(path/"test.csv")
 test_df.head()
combine[['Family_Size', 'Survived']].groupby('Family_Size', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<feature_engineering>
TRAIN = Path(".. /train")
 TEST = Path(".. /test" )
combine['Alone'] = 0
combine.loc[combine['Family_Size'] == 1, 'Alone'] = 1<sort_values>
if os.path.isdir(TRAIN):
print('Train directory has been created')
 else:
print('Train directory creation failed.')
 if os.path.isdir(TEST):
print('Test directory has been created')
 else:
print('Test directory creation failed.' )
combine[['Alone', 'Survived']].groupby('Alone', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<drop_column>
from PIL import Image
combine = combine.drop(['SibSp', 'Parch', 'Family_Size'], axis = 1)
 combine.head()<feature_engineering>
def pix2img(pix_data, filepath):
img_mat = pix_data.reshape(28,28)
 img_mat = img_mat.astype(np.uint8())
 img_dat = Image.fromarray(img_mat)
 img_dat.save(filepath )
combine['Minor'] = combine['Age'] <= 17
combine['Major'] = 1 - combine['Minor']<sort_values>
tfms = get_transforms(do_flip = False )
combine[['Major', 'Survived']].groupby('Major', as_index = False ).mean().sort_values(by = 'Survived', ascending = False )<feature_engineering>
print('test : ',TEST)
 print('train: ', TRAIN)
 print(type(TEST))
combine.loc[(combine['Age'] <= 17), 'Major'] = 0
combine.loc[(combine['Age'] > 17), 'Major'] = 1<drop_column>
path =(".. /train")
combine = combine.drop(['Age', 'Minor'], axis = 1)
 combine.head()<categorify>
np.random.seed(42)
 data = ImageDataBunch.from_folder(path, train=".", test =(".. /test"), valid_pct=0.2,
ds_tfms=get_transforms() , size=28, num_workers=0 ).normalize(imagenet_stats )
combine = pd.get_dummies(combine, columns = ['Title'])
 combine = pd.get_dummies(combine, columns = ['Embarked'], prefix = 'Em')
 combine.head()<feature_engineering>
learn = cnn_learner(data, base_arch = models.resnet34, metrics = accuracy,model_dir="/tmp/models", callback_fns=ShowGraph )
combine.loc[combine['Fare'] <= 1.56, 'Fare'] = 0
combine.loc[(combine['Fare'] > 1.56)&(combine['Fare'] <= 3.119), 'Fare'] = 1
combine.loc[(combine['Fare'] > 3.119)&(combine['Fare'] <= 4.679), 'Fare'] = 2
combine.loc[combine['Fare'] > 4.679, 'Fare'] = 3<data_type_conversions>
learn.fit_one_cycle(5, 1e-03 )
combine['Fare'] = combine['Fare'].astype('int' )<drop_column>
learn.save('model1' )
combine = combine.drop('Fare_Band', axis = 1 )<split>
learn.fit_one_cycle(5 , 1e-04 )
train = combine[:len(train)]
test = combine[len(train):]<drop_column>
learn.fit_one_cycle(5 , slice(1e-05,1e-04))
train = train.drop('PassengerId', axis = 1)
 train.head()<data_type_conversions>
learn.fit_one_cycle(5 , slice(1e-06,1e-05))
train['Survived'] = train['Survived'].astype('int')
 train.head()<drop_column>
np.random.seed(42)
 data = ImageDataBunch.from_folder(path, train=".", test =(".. /test"), valid_pct=0.2,
ds_tfms=get_transforms() , size=69, num_workers=0 ).normalize(imagenet_stats)
 learn.data = data
data.train_ds[0][0].shape
test = test.drop('Survived', axis = 1)
 test.head()<data_type_conversions>
lr=1e-03
train['Cabin'] = train['Cabin'].astype('int' )<data_type_conversions>
learn.fit_one_cycle(5, slice(lr))
test['Cabin'] = test['Cabin'].astype('int' )<train_model>
learn.save('stage-4' )
X_train = train.drop('Survived', axis = 1)
 Y_train = train['Survived']
X_test = test.drop('PassengerId', axis = 1 ).copy()
print("X_train shape: ", X_train.shape)
 print("Y_train shape: ", Y_train.shape)
 print("X_test shape: ", X_test.shape )<compute_train_metric>
learn.fit_one_cycle(5, slice(1e-5, lr/5))
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
 Y_pred = logreg.predict(X_test)
 acc_log = round(logreg.score(X_train, Y_train)* 100, 2)
 acc_log<compute_train_metric>
learn.fit_one_cycle(10, 1e-05, wd=0.5 )
svc = SVC()
svc.fit(X_train, Y_train)
 Y_pred = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, Y_train)* 100, 2)
 acc_svc<predict_on_test>
class_score , y = learn.get_preds(DatasetType.Test )
knn = KNeighborsClassifier(n_neighbors = 5)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 acc_knn<predict_on_test>
probabilities = class_score[0].tolist()
[f"{index}: {probabilities[index]}" for index in range(len(probabilities)) ]
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian<predict_on_test>
class_score = np.argmax(class_score, axis=1 )
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(perceptron.score(X_train, Y_train)* 100, 2)
 acc_perceptron<compute_train_metric>
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 sample_submission.head()
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
 Y_pred = linear_svc.predict(X_test)
 acc_linear_svc = round(linear_svc.score(X_train, Y_train)* 100, 2)
 acc_linear_svc<compute_train_metric>
ImageId = [os.path.splitext(path)[0] for path in os.listdir(TEST)]
ImageId = [int(path)for path in ImageId]
ImageId = [ID+1 for ID in ImageId]
<choose_model_class><EOS> 
submission = pd.DataFrame({
"ImageId": ImageId,
"Label": class_score
})
 submission.to_csv("submission.csv", index=False )