Kaggle competition: Digit Recognizer
cat_dist(tmp_train, var='CabinClass', hue='Survived', msg_show=False )<feature_engineering>
import keras
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras.optimizers import RMSprop
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
tmp_train['IsNumericTicket'] = tmp_train.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 tmp_train['TicketType'] = tmp_train.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])> 0 else 0 )<concatenate>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 y_train = train['label']
x_train = train.drop(labels=['label'] ,axis=1)
 del train
x_train.isnull().any().describe()
test.isnull().any().describe()
cat_dist(tmp_train, var='IsNumericTicket', hue='Survived' )<drop_column>
x_train = x_train / 255.0
test = test / 255.0
all_data['Age'] = all_data.Age.fillna(train.Age.median())
 all_data['Fare'] = all_data.Fare.fillna(train.Fare.median())
 all_data.dropna(subset=['Embarked'], inplace=True)
 cabins = all_data.Cabin
all_data.drop(['Cabin'], axis=1, inplace=True )<feature_engineering>
NUM_CLASSES = 10
y_train = to_categorical(y_train, num_classes=NUM_CLASSES )
all_data['CabinCnt'] = cabins.apply(lambda x: 0 if pd.isna(x)else len(x.split(' ')))
 all_data['CabinClass'] = cabins.apply(lambda x: str(x)[0])
 all_data['IsNumericTicket'] = all_data.Ticket.apply(lambda x: 1 if x.isnumeric() else 0)
 all_data['TicketType'] = all_data.Ticket.apply(lambda x: ''.join(x.split(' ')[:-1] ).replace('.','' ).replace('/','' ).lower() if len(x.split(' ')[:-1])> 0 else 0)
 all_data['Title'] = all_data.Name.apply(lambda x: x.split(',')[1].split('.')[0].strip())
 all_data['Family'] = all_data.SibSp + all_data.Parch<normalization>
random_seed = 2
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.1, random_state=random_seed )
numeric_vars = ['Age', 'SibSp', 'Parch', 'Fare', 'CabinCnt', 'Family']
ordinal_vars = ['Pclass']
nominal_vars = ['Name', 'Sex', 'Ticket', 'Embarked', 'CabinClass', 'IsNumericTicket', 'TicketType', 'Title']
all_data[nominal_vars] = all_data[nominal_vars].astype('str')
 for feature in numeric_vars:
all_data[feature] = np.log1p(all_data[feature])
 scaler = StandardScaler()
numeric_vars = all_data.columns[(all_data.dtypes != 'object')&(all_data.columns != 'PassengerId')&(all_data.columns != 'Survived')&(all_data.columns != 'IsTrain')]
all_data[numeric_vars] = scaler.fit_transform(all_data[numeric_vars] )<prepare_x_and_y>
%matplotlib inline
print(x_train.shape)
 test_show = plt.imshow(x_train[0][:,:,0] )
all_data.drop(['PassengerId', 'Name', 'Ticket'], axis=1, inplace=True)
 data_dummies = pd.get_dummies(all_data)
 X_train = data_dummies[data_dummies.Survived.notnull() ].drop(['Survived'], axis=1)
 y_train = data_dummies[data_dummies.Survived.notnull() ].Survived
X_test = data_dummies[data_dummies.Survived.isnull() ].drop(['Survived'], axis=1 )<count_values>
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(filters=32, kernel_size=(5,5), padding='Same', activation='relu'))
model.add(MaxPooling2D(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(MaxPooling2D(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(NUM_CLASSES, activation='softmax'))
all_data.Title = all_data.Title.apply(lambda x: 'Others' if x in list(all_data.Title.value_counts() [all_data.Title.value_counts() < 8].index)else x)
 all_data.TicketType = all_data.TicketType.apply(lambda x: 'Others' if x in list(all_data.TicketType.value_counts() [all_data.TicketType.value_counts() < 10].index)else x )<prepare_x_and_y>
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0), loss='categorical_crossentropy', metrics=['accuracy'])
 reduce_lr = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 30
batch_size = 86
data_dummies = pd.get_dummies(all_data)
 X_train = data_dummies[data_dummies.Survived.notnull() ].drop(['Survived'], axis=1)
 X_test = data_dummies[data_dummies.Survived.isnull() ].drop(['Survived'], axis=1 )<train_model>
params = {
'rotation_range':10,
'zoom_range':0.1,
'width_shift_range':0.1,
'height_shift_range':0.1,
'featurewise_center':False,
'samplewise_center':False,
'featurewise_std_normalization':False,
'samplewise_std_normalization':False,
'zca_whitening':False,
'horizontal_flip':False,
'vertical_flip':False
}
datagen = ImageDataGenerator(**params)
 datagen.fit(x_train )
allow_tuning = False<split>
hist = model.fit_generator(
 datagen.flow(x_train, y_train, batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
verbose=1,
steps_per_epoch=x_train.shape[0] // batch_size,
callbacks=[reduce_lr]
)
def xgb_gridsearch(params_grid_xgb, features, values, X, y, last=False):
x_train, x_test = train_test_split(X, test_size=.2, random_state=42)
 y_train_tmp, y_test_tmp = train_test_split(y, test_size=.2, random_state=42)
 cv = RepeatedStratifiedKFold(n_splits = 10, n_repeats = 3, random_state = 42)
 model_xgb = XGBClassifier(use_label_encoder = False,
objective = 'binary:logistic')
 for i in range(len(features)) :
params_grid_xgb[features[i]] = values[i]
search_xgb = GridSearchCV(model_xgb, params_grid_xgb, verbose = 0,
scoring = 'neg_log_loss', cv = cv ).fit(x_train, y_train_tmp, early_stopping_rounds = 15,
eval_set = [[x_test, y_test_tmp]],
eval_metric = 'logloss', verbose = False)
 for i in range(len(features)) :
print(f"{features[i]}: {search_xgb.best_params_[features[i]]}")
 if not last:
for k, v in search_xgb.best_params_.items() :
search_xgb.best_params_[k] = [v]
return search_xgb, search_xgb.best_params_<train_on_grid>
submission = pd.concat([pd.Series(range(1,28001), name="ImageId"), results], axis=1)
 submission.to_csv('mnist_datagen.csv', index=False )