Kaggle competition: Digit Recognizer
all_data = all_data.drop(columns=['Id'], axis=1 )<sort_values>
labels = traindata.iloc[:,0].values.astype('int32')
 x_train = traindata.iloc[:,1:].values.astype('int32')
 x_test = testdata.values.astype('int32')
 x_train = x_train.reshape(-1,28,28,1)
 x_test = x_test.reshape(-1,28,28,1)
 y_train = tf.keras.utils.to_categorical(labels)
 print(x_train.shape)
 print(x_test.shape)
 print(y_train )
def missingValuesInfo(df):
total = df.isnull().sum().sort_values(ascending = False)
 percent = round(df.isnull().sum().sort_values(ascending = False)/ len(df)*100, 2)
 temp = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 return temp.loc[(temp['Total'] > 0)]
missingValuesInfo(train_data )<feature_engineering>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, train_size=0.9)
 print(x_train.shape)
 print(y_train.shape)
 print(x_val.shape)
 print(y_val.shape)
def HandleMissingValues(df):
num_cols = [cname for cname in df.columns if df[cname].dtype in ['int64', 'float64']]
cat_cols = [cname for cname in df.columns if df[cname].dtype == "object"]
values = {}
for a in cat_cols:
values[a] = 'UNKNOWN'
for a in num_cols:
mean1=df[a].mean()
std1=df[a].std()
values[a]=random.randint(int(mean1-std1), int(mean1+std1))
df.fillna(value=values, inplace=True)
 HandleMissingValues(all_data)
 all_data.isnull().sum().sum()<categorify>
image_generator = ImageDataGenerator(
 rescale = 1/255.0,
rotation_range = 20,
width_shift_range = 0.1,
height_shift_range = 0.1,
zoom_range = 0.1)
 validation_generator = ImageDataGenerator(
 rescale = 1/255.0 )
def getObjectColumnsList(df):
return [cname for cname in df.columns if df[cname].dtype == "object"]
def PerformOneHotEncoding(df, columnsToEncode):
return pd.get_dummies(df, columns=columnsToEncode)
 cat_cols = getObjectColumnsList(all_data)
 all_data = PerformOneHotEncoding(all_data, cat_cols)
 all_data.head()<prepare_x_and_y>
model = models.Sequential()
model.add(layers.Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Conv2D(64,(3, 3), activation='relu'))
model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Flatten())
 model.add(layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dropout(0.25))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
target = train_data['SalePrice']
train_data = train_data.drop(['SalePrice'], axis=1)
 test_data = test_data.drop(['SalePrice'], axis=1)
 X, y = train_data, target
train_data.shape,test_data.shape<compute_train_metric>
model.compile(optimizer='Adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit_generator(image_generator.flow(x_train, y_train,batch_size = 512),
epochs =100,validation_data = validation_generator.flow(x_val, y_val, batch_size = 512), verbose=1 )
<save_to_csv><EOS> 
predict_generator = ImageDataGenerator(
 rescale = 1/255.0)
 predictions = model.predict_generator(predict_generator.flow(x_test, shuffle = False), verbose=0)
 predictions = np.argmax(predictions,axis=1)
 print(predictions)
 pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) , "Label": predictions} ).to_csv("preds.csv", index=False, header=True )