Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<count_unique_values>
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import itertools
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
print("Có %d nhãn ở cột cat_3." % train['cat_3'].nunique() )<count_values>
mnist_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 mnist_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )
train['name'].value_counts() [:10]<categorify>
mnist_train
lb_item_condition_id = LabelBinarizer(sparse_output=True)
 train_condition = lb_item_condition_id.fit_transform(train['item_condition_id'])
 test_condition = lb_item_condition_id.transform(test['item_condition_id'] )<categorify>
x_train = mnist_train.drop(labels = "label", axis = 1)
 x_train
lb_shipping = LabelBinarizer(sparse_output=True)
 train_shipping = lb_shipping.fit_transform(train['shipping'])
 test_shipping = lb_shipping.transform(test['shipping'] )<categorify>
y_train = mnist_train["label"]
y_train
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'])
<feature_engineering>
x_train = x_train/255.0
mnist_test = mnist_test/255.0
count_vec = CountVectorizer()
train_name = count_vec.fit_transform(train['name'])
 test_name = count_vec.transform(test['name'] )<categorify>
x_train = x_train.values.reshape(-1,28,28,1)
 mnist_test = mnist_test.values.reshape(-1,28,28,1)
 x_train.shape
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'] )<categorify>
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10 )
lb_cat_1 = LabelBinarizer(sparse_output=True)
 train_cat_1 = lb_cat_1.fit_transform(train['cat_1'])
 test_cat_1 = lb_cat_1.transform(test['cat_1'])
 lb_cat_2 = LabelBinarizer(sparse_output=True)
 train_cat_2 = lb_cat_2.fit_transform(train['cat_2'])
 test_cat_2 = lb_cat_2.transform(test['cat_2'])
 lb_cat_3 = LabelBinarizer(sparse_output=True)
 train_cat_3 = lb_cat_3.fit_transform(train['cat_3'])
 test_cat_3 = lb_cat_3.transform(test['cat_3'] )<import_modules>
datagen = ImageDataGenerator(rotation_range=12, zoom_range = 0.15, width_shift_range=0.15, height_shift_range=0.13)
 datagen.fit(x_train)
 datagen.fit(x_test )
from scipy.sparse import hstack
import gc<define_variables>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.15)
 print(x_train.shape)
 print(x_val.shape )
sparse_matrix_list =(train_name, train_des, train_brand_name, train_condition,
train_shipping, train_cat_1, train_cat_2, train_cat_3 )<compute_test_metric>
input_layer = tf.keras.layers.Input(shape =(28, 28, 1))
hidden_layer_1 = tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation=tf.keras.activations.relu )(input_layer)
 norm_1 = tf.keras.layers.BatchNormalization()(hidden_layer_1)
 max_1 = tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2))(norm_1)
 dropout_layer_1 = tf.keras.layers.Dropout(0.25 )(max_1)
 flatten_layer = tf.keras.layers.Flatten()(dropout_layer_1)
 hidden_layer_3 = tf.keras.layers.Dense(256, activation=tf.keras.activations.relu )(flatten_layer)
 hidden_layer_4 = tf.keras.layers.Dense(128, activation=tf.keras.activations.relu )(hidden_layer_3)
 dropout_layer_3 = tf.keras.layers.Dropout(0.5 )(hidden_layer_4)
 output_layer = tf.keras.layers.Dense(10, activation=tf.keras.activations.sigmoid )(dropout_layer_3 )
def rmsle(y, y_preds):
assert len(y)== len(y_preds)
 return np.sqrt(np.mean(np.power(np.log1p(y)-np.log1p(y_preds), 2)) )<split>
model = tf.keras.Model(inputs = input_layer, outputs = output_layer)
 model.summary()
def run_model(model, matrix_list):
X = hstack(matrix_list ).tocsr()
X_train, x_test, Y_train, y_test = train_test_split(X, np.log1p(train['price']), test_size=0.2)
 model.fit(X_train, Y_train)
 preds = model.predict(x_test)
 del X, X_train, x_test, Y_train
gc.collect()
return preds, y_test<choose_model_class>
model.compile(optimizer = tf.keras.optimizers.Adam() , loss = tf.keras.losses.CategoricalCrossentropy() , metrics = ['accuracy'] )
model = ridge = Ridge()
Ridge_preds, y_test = run_model(model, matrix_list=sparse_matrix_list)
<compute_test_metric>
his = model.fit(x_train, y_train, batch_size = 1000, epochs = 40, validation_data=(x_val, y_val))
print("rmsle: "+str(rmsle(np.expm1(y_test), np.expm1(Ridge_preds))))<prepare_x_and_y>
results = model.predict(mnist_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<compute_test_metric><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("mnist_result.csv",index=False)
 submission