Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<categorify>
%matplotlib inline
lb_shipping = LabelBinarizer(sparse_output=True)
 train_shipping = lb_shipping.fit_transform(train['shipping'])
 test_shipping = lb_shipping.transform(test['shipping'])
 train_shipping.shape<categorify>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 y_train = train["label"]
x_train = train.drop(labels = ["label"], axis = 1)
 x_train = x_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 del train
X_train /= 255
test /= 255
lb_brand_name = LabelBinarizer(sparse_output=True)
 train_brand_name= lb_brand_name.fit_transform(train['brand_name'])
 test_brand_name = lb_brand_name.transform(test['brand_name'])
 train_brand_name.shape<feature_engineering>
y_train = to_categorical(y_train, num_classes = 10 )
count_vec = CountVectorizer()
train_name = count_vec.fit_transform(train['name'])
 test_name = count_vec.transform(test['name'])
 print(train_name.shape )<categorify>
random_seed = 2
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train,
test_size = 0.2, random_state = random_seed )
tfidf_des = TfidfVectorizer(max_features=50000, ngram_range=(1, 3), stop_words='english')
 train_des = tfidf_des.fit_transform(train['item_description'])
 test_des = tfidf_des.transform(test['item_description'])
 train_des.shape<categorify>
def create_model() :
def add_conv_block(model, num_filters):
model.add(Conv2D(num_filters,3,activation='relu', padding = 'same'))
model.add(BatchNormalization())
 model.add(Conv2D(num_filters,3, activation= 'relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.5))
return model
model = tf.keras.models.Sequential()
model.add(Input(shape=(28,28,1)))
 model = add_conv_block(model, 32)
 model = add_conv_block(model, 64)
 model = add_conv_block(model, 128)
 model.add(Flatten())
 model.add(Dense(128, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(10,activation = 'softmax'))
model.compile(
 loss = 'categorical_crossentropy',
optimizer = 'adam', metrics = ['accuracy'] 
)
 return model
model = create_model()
model.summary()
lb_cat_1 = LabelBinarizer(sparse_output=True)
 train_cat_1 = lb_cat_1.fit_transform(train['subcat_1'])
 test_cat_1 = lb_cat_1.transform(test['subcat_1'])
 lb_cat_2 = LabelBinarizer(sparse_output=True)
 train_cat_2 = lb_cat_2.fit_transform(train['subcat_2'])
 test_cat_2 = lb_cat_2.transform(test['subcat_2'])
 lb_cat_3 = LabelBinarizer(sparse_output=True)
 train_cat_3 = lb_cat_3.fit_transform(train['subcat_3'])
 test_cat_3 = lb_cat_3.transform(test['subcat_3'] )<set_options>
h = model.fit(
 x_train,y_train,
validation_data =(x_val,y_val),
epochs=20,batch_size=30
)
sparse_matrix_list =(train_name, train_des, train_brand_name, train_condition,
train_shipping, train_cat_1, train_cat_2, train_cat_3)
 X_train = hstack(sparse_matrix_list ).tocsr()
print(X_train.shape)
 del X_train
gc.collect()<compute_test_metric>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<split><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )