Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<count_missing_values>
get_ipython().run_line_magic('matplotlib', 'inline')
 sns.set_style("whitegrid")
test_data.isnull().sum()<count_values>
train = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 test = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 validation = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
train_data['IsBadBuy'].value_counts()<count_values>
train.groupby(train["label"] ).size()
train_data['Model'].value_counts()<drop_column>
validation.groupby(validation["label"] ).size()
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop("Model",axis=1,inplace=True )<count_values>
train_labels = to_categorical(train.iloc[:,0])
 train = train.iloc[:, 1:].values
X_validation = validation.iloc[:, 1:].values
y_validation = to_categorical(validation.iloc[:,0])
 test_id = test.iloc[:, 0]
test = test.iloc[:, 1:].values
train_data['Trim'].value_counts()<drop_column>
train = train/255
X_validation = X_validation/255
test = test/255
train_data.drop('Trim',inplace=True,axis=1)
 test_data.drop('Trim',inplace=True,axis=1)
<count_values>
train_datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=12,
zoom_range=0.25 
)
 valid_datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.25,
height_shift_range=0.25,
shear_range=12,
zoom_range=0.25)
 valid_datagen_simple = ImageDataGenerator()
train_data['SubModel'].value_counts()<drop_column>
X_train, X_test, y_train, y_test = train_test_split(train, train_labels, test_size = 0.2, random_state = 84)
train_data.drop('SubModel',inplace=True,axis=1)
 test_data.drop('SubModel',inplace=True,axis=1 )<count_values>
def build_model() :
model = Sequential()
model.add(Conv2D(32,(3,3), activation = "relu", input_shape =(28,28,1), padding = "same"))
model.add(BatchNormalization())
 model.add(Conv2D(32,(5,5), strides =(2,2),activation = "relu", padding = "same"))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), activation = "relu", input_shape =(28,28,1), padding = "same"))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5,5), strides =(2,2),activation = "relu", padding = "same"))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), activation = "relu", padding = "same"))
model.add(BatchNormalization())
 model.add(Conv2D(128,(5,5), strides =(2,2), activation = "relu", padding = "same"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Conv2D(256,(3,3), activation = "relu", padding = "same"))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(10, activation = "softmax"))
model.compile(optimizer = "adam", loss = categorical_crossentropy, metrics = ["accuracy"])
 return(model)
train_data['Color'].value_counts()<count_values>
model = build_model()
test_data['Color'].value_counts()<data_type_conversions>
history = model.fit_generator(
 train_datagen.flow(X_train, y_train, batch_size = 1024),
epochs = 50,
steps_per_epoch=50,
validation_data =(X_test, y_test))
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )<count_missing_values>
y_test_labels = []
for i in y_test:
for j, val in enumerate(i):
if val == 0.:
pass
else:
y_test_labels.append(j )
print("Number of null values in Color column "+str(train_data['Color'].isnull().sum()))
print("Number of null values in Color column "+str(test_data['Color'].isnull().sum()))<count_values>
preds = history.model.predict_classes(X_test )
train_data['Transmission'].value_counts()<count_values>
accuracy_score(preds, np.array(y_test_labels))
test_data['Transmission'].value_counts()<filter>
model = build_model()
train_data[train_data['Transmission']=='Manual']<rename_columns>
history_full = model.fit_generator(
 train_datagen.flow(train, train_labels, batch_size = 1024),
epochs = 50,
steps_per_epoch=train.shape[0]//1024,
validation_data = valid_datagen.flow(X_validation, y_validation))
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )<count_values>
preds = history_full.model.predict_classes(X_validation)
train_data['Transmission'].value_counts()<data_type_conversions>
accuracy_score(preds, np.argmax(y_validation, axis = 1))
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )<count_values>
X_train_total = np.concatenate(( train, X_validation))
y_train_total = np.concatenate(( train_labels, y_validation))
train_data['WheelTypeID'].value_counts()<drop_column>
model = build_model()
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )<count_values>
history_final = model.fit_generator(train_datagen.flow(
 X_train_total, y_train_total, batch_size=1024),
epochs = 50,
steps_per_epoch = X_train_total.shape[0]//1024,
validation_data = valid_datagen.flow(X_validation, y_validation))
train_data['WheelType'].value_counts()<count_values>
preds = history_final.model.predict_classes(test)
test_data['WheelType'].value_counts()<data_type_conversions>
submission = pd.DataFrame(data = [pd.Series(test_id, name = "id"), pd.Series(preds, name = "label")], ).T
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )<count_values>
submission.to_csv("submission.csv", index = False)