Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_x_and_y>
%matplotlib inline
train_names = train_data[['Name','Age']].dropna().reset_index()
train_names['Name'] = train_names['Name'].str.lower()
new_date = []
for i in train_names['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df['Age'] = train_names['Age']
features_train = df['Name']
target_train = df['Age']<feature_engineering>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test= pd.read_csv(".. /input/digit-recognizer/test.csv")
count_tf_idf = TfidfVectorizer()
tf_idf = count_tf_idf.fit_transform(features_train)
 count_tf_idf_ts = TfidfVectorizer()
tf_id_ts = count_tf_idf.transform(features_train )<categorify>
X_train =(train.iloc[:,1:].values ).astype('float32')
 y_train = train.iloc[:,0].values.astype('int32')
 X_test = test.values.astype('float32')
clf_t = RandomForestRegressor(random_state=42,
criterion='mae' ).fit(count_tf_idf.transform(features_train), target_train )<feature_engineering>
img_rows, img_cols = 28, 28
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols,1)
 X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols,1 )
ind = train_data.loc[train_data['Age'].isnull() ==True].index
ddf = train_data.loc[ind, ['Name']]
ddf['Name'] = ddf['Name'].str.lower()
new_date = []
for i in ddf['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df.index=ind
features_train = df['Name']<predict_on_test>
mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 def standardize(x):
return(x-mean_px)/std_px
X_train = standardize(X_train)
 X_test = standardize(X_test )
d = round(pd.DataFrame(clf_t.predict(count_tf_idf.transform(features_train)) ,columns=['new_adge']))
d.index=ind
d<data_type_conversions>
y_train = to_categorical(y_train)
 num_classes = y_train.shape[1]
for i in d.index:
for a in train_data.index:
if i == a:
train_data.loc[i,['Age']] = int(d.loc[i,['new_adge']])
 train_data['Age'] = train_data['Age'].astype('int')
 train_data.info()<feature_engineering>
gen = ImageDataGenerator(rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1,
shear_range = 0.1 )
ind = test_data.loc[test_data['Age'].isnull() ==True].index
ddf = test_data.loc[ind, ['Name']]
ddf['Name'] = ddf['Name'].str.lower()
new_date = []
for i in ddf['Name']:
d = [" ".join(re.sub(r"[^a-z ]", ' ', str(i)).split())]
new_date.append(d)
 df = pd.DataFrame(new_date, columns = ['Name'])
 df.index=ind
features_test = df['Name']<predict_on_test>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=4)
 batches = gen.flow(X_train, y_train, batch_size=64)
 val_batches=gen.flow(X_val, y_val, batch_size=64 )
d = round(pd.DataFrame(clf_t.predict(count_tf_idf.transform(features_test)) ,columns=['new_adge']))
d.index=ind
for i in d.index:
for a in test_data.index:
if i == a:
test_data.loc[i,['Age']] = int(d.loc[i,['new_adge']])
 test_data['Age'] = test_data['Age'].astype('int')
 test_data.info()<filter>
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(img_rows, img_cols, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
train_data.loc[5,['Age']]<count_values>
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer='adam',
metrics=['accuracy'] )
train_data['Embarked'].value_counts()<feature_engineering>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 history=model.fit_generator(generator=batches,
steps_per_epoch=batches.n,
epochs=3,
validation_data=val_batches,
validation_steps=val_batches.n,
callbacks=[learning_rate_reduction])
train_data['Embarked'] = train_data['Embarked'].fillna('S')
 train_data.info()<categorify>
predictions = model.predict_classes(X_test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("submission.csv", index=False, header=True )