Kaggle competition: Digit Recognizer
train = full_data[:num_train]
test = full_data[num_train:]
print("Train :",train.shape)
 print("Test:",test.shape)
 test<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Dropout
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
mean_revenue_per_city = train[['City_Cluster', 'revenue']].groupby('City_Cluster', as_index=False ).mean()
mean_revenue_per_city.head()
mean_revenue_per_city['revenue'] = mean_revenue_per_city['revenue'].apply(lambda x: int(x/1e6))
mean_revenue_per_city
mean_dict = dict(zip(mean_revenue_per_city.City_Cluster, mean_revenue_per_city.revenue))
mean_dict<create_dataframe>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
city_rev = []
for i in full_data['City_Cluster']:
for key, value in mean_dict.items() :
if i == key:
city_rev.append(value)
 df_city_rev = pd.DataFrame({'city_rev':city_rev})
 full_data = pd.concat([full_data,df_city_rev],axis=1)
 full_data.head
nominal_list.extend(['City_Cluster'])
 nominal_list.remove('City' )<categorify>
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]
le = LabelEncoder()
le_count = 0
for i in range(len(nominal_list)) :
if len(list(full_data[nominal_list[i]].unique())) <= 2:
le.fit(full_data[nominal_list[i]])
 full_data[nominal_list[i]] = le.transform(full_data[nominal_list[i]])
 le_count += 1
print('%d columns were label encoded.' % le_count )<categorify>
X_train = X_train.values.reshape(-1, 28, 28, 1)/255.
 test = test.values.reshape(-1, 28, 28, 1)/255.
 y_train = to_categorical(y_train, 10 )
full_data = pd.get_dummies(full_data)
 print('full_dataing Features shape: ', full_data.shape )<randomize_order>
random_seed = 0
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=random_seed )
def tukey_outliers(x):
q1 = np.percentile(x,25)
 q3 = np.percentile(x,75)
 iqr = q3-q1
min_range = q1 - iqr*1.5
max_range = q3 + iqr*1.5
outliers = x[(x<min_range)|(x>max_range)]
return outliers<sort_values>
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1
)
skewed_data = train[num_list].apply(lambda x: skew(x)).sort_values(ascending=False)
 skewed_data[:10]<train_model>
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(5,5), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
train = full_data[:num_train]
test = full_data[num_train:]
print("Train :",train.shape)
 print("Test:",test.shape )<train_model>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
( X_train, X_test, y_train, y_test)= train_test_split(train_X, train_y , test_size = 0.3 , random_state = 0)
 print("X_train: "+str(X_train.shape))
print("X_test: "+str(X_test.shape))
print("y_train: "+str(y_train.shape))
print("y_test: "+str(y_test.shape))<import_modules>
EPOCHS = 20
BATCH_SIZE = 20
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.25, patience=1, verbose=1, mode='auto',
min_delta=0.0001)
 ]
history = model.fit(datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE )
<choose_model_class><EOS> 
results = model.predict(test)
 results = np.argmax(results, axis=1)
 results = pd.Series(results, name='Label')
 submission = pd.concat([pd.Series(range(1,28001), name='ImageID'), results], axis=1)
 submission.to_csv('submission.csv', index=False )