Kaggle competition: Digit Recognizer
test=pd.read_csv('.. /input/titanic/test.csv' )<categorify>
import pandas as pd
from matplotlib import pyplot
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from numpy import asarray, unique, argmax
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
from keras.optimizers import RMSprop
embark=pd.get_dummies(test['Embarked'] )<categorify>
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 data.head()
gender=pd.get_dummies(test['Sex'] )<data_type_conversions>
unique = data['label'].unique()
print("Unique Numbers :",unique)
 n_classes = len(unique)
 print("Number of classes :",n_classes )
test['Age'].fillna(30,inplace=True)
 test['Age']=test['Age'].astype('int' )<feature_engineering>
x = data.drop(labels = ["label"], axis=1)
 print("X= ",x )
test['Age_grp']=test['Age'].apply(lambda x: age_grp(x))<categorify>
y = data['label']
print("Target Variable: ",y )
age=pd.get_dummies(test['Age_grp'] )<concatenate>
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.20, random_state=42 )
test_df=pd.concat([test,embark,gender,age],axis=1 )<feature_engineering>
print("X_train: ", x_train)
 print("X_test: ", y_test)
 print("Y_train: ", y_train)
 print("Y_test: ", y_test )
test_df['Family']=test_df['Parch']+1+test_df['SibSp']
test_df['Parch']=test_df['Parch'].apply(lambda x: is_var(x))
test_df['SibSp']=test_df['SibSp'].apply(lambda x: is_var(x))<correct_missing_values>
x_train = x_train.values.reshape(-1,28,28,1)
 x_test = x_test.values.reshape(-1,28,28,1 )
test_df.fillna(0,inplace=True )<prepare_x_and_y>
x_train = x_train.astype('float32')/ 255.0
x_test = x_test.astype('float32')/ 255.0
test_X=test_df[sel_cols]<predict_on_test>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape = in_shape))
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
test_y=lr.predict(test_X )<load_from_csv>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
sub=pd.read_csv('.. /input/titanic/gender_submission.csv' )<prepare_output>
model.compile(optimizer= 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])
sub['Survived']=test_y<save_to_csv>
model.fit(x_train, y_train, validation_split=0.2, epochs=100, batch_size=128, verbose= 1)
sub.to_csv('submission.csv',index=False )<install_modules>
loss, accuracy = model.evaluate(x_test, y_test, verbose=1)
 print('Accuracy: %.3f' %accuracy)
 print('Loss: ',loss )
!pip install autogluon --user
clear_output()<import_modules>
image = x_test[2]
ypred = model.predict(asarray([image]))
print('Prediction: Class =%d' %argmax(ypred))
import numpy as np
import pandas as pd
import os
import random
from autogluon.tabular import TabularDataset, TabularPredictor<define_variables>
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 test_data.head()
TRAIN_PATH = ".. /input/titanic/train.csv"
TEST_PATH = ".. /input/titanic/test.csv"
SAMPLE_SUBMISSION_PATH = ".. /input/titanic/gender_submission.csv"
SUMISSION_PATH = "submission.csv"
TARGET = 'Survived'
EVAL_METRIC = "roc_auc"
SAVE_PATH = 'agModels-predictClass'
DEFAULT_RANDOM_SEED = 2021
def seedBasic(seed=DEFAULT_RANDOM_SEED):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 seedBasic()<create_dataframe>
test = test_data/255.0
train = TabularDataset(TRAIN_PATH)
 test = TabularDataset(TEST_PATH )<train_model>
test_final_Data = test.values.reshape(-1,28,28,1 )
predictor = TabularPredictor(label=TARGET,eval_metric=EVAL_METRIC, path=SAVE_PATH ).fit(train )<predict_on_test>
label = model.predict(test_final_Data)
 print(label )
y_pred = predictor.predict(test )<save_to_csv>
label = np.argmax(label, axis=1)
submission = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 submission[TARGET] = y_pred
submission.to_csv(SUMISSION_PATH,index=False)
 submission.head()<set_options>
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 sample_submission.head()
init_notebook_mode(connected=True)
<set_options>
index = test_data.index.values + 1
data = {'ImageId' : index, "Label" : label}
df = pd.DataFrame(data=data)
 df.head()
warnings.filterwarnings('ignore' )<load_from_csv>
submit_file = pd.DataFrame({'ImageId' : index, "Label" : label.astype(int ).ravel() })
 submit_file.to_csv("submission.csv",index = False)