Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
%matplotlib inline
labels = train['TARGET']
train = train.drop(columns=['TARGET'])
 test = test.drop(columns=['TARGET'] )<prepare_output>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
feature = list(train.columns)
 train.replace([np.inf, -np.inf], np.nan, inplace=True)
 test.replace([np.inf, -np.inf], np.nan, inplace=True)
 test_df = test.copy()
train_df = train.copy()
train_df['TARGET'] = labels<normalization>
y = train['label']
imputer = SimpleImputer(strategy = 'median')
 imputer.fit(train)
 train = imputer.transform(train)
 test = imputer.transform(test )<normalization>
train = train.drop(['label'],axis =1)
 train = train/255
scaler = MinMaxScaler(feature_range =(0, 1))
scaler.fit(train)
 train = scaler.transform(train)
 est = scaler.transform(test )<train_model>
X=train
log_reg = LogisticRegression(C = 0.0001)
 log_reg.fit(train, labels )<predict_on_test>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.01, random_state=42 )
log_reg_pred = log_reg.predict_proba(test)[:, 1] 
<prepare_output>
m = keras.Sequential()
m.add(keras.layers.Conv2D(32,(3, 3), activation="relu", kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
 m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.Conv2D(32,(3, 3), activation="relu", padding='same', kernel_initializer='he_uniform'))
m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.MaxPool2D(2, 2))
m.add(keras.layers.Dropout(0.20))
m.add(keras.layers.Conv2D(64,(3, 3), activation="relu", padding='same', kernel_initializer='he_uniform'))
m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.Conv2D(64,(3, 3), activation="relu", padding='same', kernel_initializer='he_uniform'))
m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.MaxPool2D(2, 2))
m.add(keras.layers.Dropout(0.2))
m.add(keras.layers.Conv2D(128,(3, 3), activation="relu", padding='same', kernel_initializer='he_uniform'))
m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.Conv2D(128,(3, 3), activation="relu", padding='same', kernel_initializer='he_uniform'))
m.add(keras.layers.BatchNormalization())
 m.add(keras.layers.MaxPool2D(2, 2))
m.add(keras.layers.Dropout(0.2))
m.add(keras.layers.Flatten())
 m.add(keras.layers.Dropout(0.2))
m.add(keras.layers.Dense(128, activation="relu"))
m.add(keras.layers.Dense(10, activation="softmax"))
m.compile(optimizer=keras.optimizers.RMSprop() , loss="sparse_categorical_crossentropy", metrics=["acc"] )
submit = test_df[['SK_ID_CURR']]
submit['TARGET'] = log_reg_pred 
<save_to_csv>
m.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=50000,batch_size=20, validation_batch_size=20 )
submit.to_csv('log_reg.csv', index = False )<drop_column>
test = test.values.reshape(-1,28,28,1)
 test = test/255.0
pred = np.argmax(m.predict(test), axis = 1 )
target = train_df.pop('TARGET')
 len_train = len(train_df)
 merged_df = pd.concat([train_df, test_df])
 meta_df = merged_df.pop('SK_ID_CURR')
 del test_df, train_df
gc.collect()<categorify>
Submission = pd.DataFrame({"ImageId" : range(1, 28001), "Label":pred} )
categorical_feats = merged_df.columns[merged_df.dtypes == 'object']
print('Using %d prediction variables'%(merged_df.shape[1]))
print('Encoding %d non-numeric columns...'%(merged_df.columns[merged_df.dtypes == 'object'].shape))
for feat in categorical_feats:
merged_df[feat].fillna('MISSING', inplace=True)
 encoder = LabelBinarizer()
new_columns = encoder.fit_transform(merged_df[feat])
 i=0
for u in merged_df[feat].unique() :
if i<new_columns.shape[1]:
merged_df[feat+'_'+u]=new_columns[:,i]
i+=1
merged_df.drop(feat, axis=1, inplace=True)
 print('Now using %d prediction variables'%(merged_df.shape[1])) 
<filter>
Submission.to_csv('Submission.csv',index=False )
null_counts = merged_df.isnull().sum()
null_counts = null_counts[null_counts > 0]
null_ratios = null_counts / len(merged_df)
<merge>
num = 3
print("Image label is:| ", pred[num])
 plt.imshow(test[num][:,:,0] )