Kaggle competition: Kannada MNIST
train_data[train_data['Transmission']=='Manual']<rename_columns>
path = '.. /input/Kannada-MNIST/'
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )<count_values>
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
train_data['Transmission'].value_counts()<data_type_conversions>
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
train_data['Transmission'].fillna(value="Transmission_unk",inplace=True)
 test_data['Transmission'].fillna(value="Transmission_unk",inplace=True )<count_values>
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import EarlyStopping
train_data['WheelTypeID'].value_counts()<drop_column>
train_data = pd.read_csv(path + 'train.csv')
 train_data
train_data.drop('WheelTypeID',inplace=True,axis=1)
 test_data.drop('WheelTypeID',inplace=True,axis=1 )<count_values>
train_data.label.value_counts()
train_data['WheelType'].value_counts()<count_values>
dig_data = pd.read_csv(path + 'Dig-MNIST.csv')
 dig_data
test_data['WheelType'].value_counts()<data_type_conversions>
dig_data.label.value_counts()
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )<count_values>
train_labels = to_categorical(train_data.label)
 train_labels
train_data['WheelType'].value_counts()<count_values>
show_random_image(train_images_2D )
train_data['Nationality'].value_counts()<count_values>
train_images_2D = train_images_2D.astype('float')
 train_images_2D /= 255
test_data['Nationality'].value_counts()<data_type_conversions>
dig_labels = to_categorical(dig_data.label)
 dig_labels
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )<count_values>
show_random_image(dig_images_2D )
train_data['Size'].value_counts()<count_values>
dig_images_2D = dig_images_2D.astype('float')
 dig_images_2D /= 255
test_data['Size'].value_counts()<data_type_conversions>
images_train,imames_val,labels_train,labels_val = train_test_split(train_images_2D, train_labels,
random_state=42,test_size=0.15 )
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value="Size_unk",inplace=True )<count_values>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(images_train )
train_data['TopThreeAmericanName'].value_counts()<count_values>
test_data = pd.read_csv(path + 'test.csv', index_col='id')
 test_data
test_data['TopThreeAmericanName'].value_counts()<data_type_conversions>
submission = pd.read_csv(path + 'sample_submission.csv', index_col='id')
 submission
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )<count_values>
test_images_2D = test_images_2D.astype('float')
 test_images_2D /= 255
train_data['PRIMEUNIT'].value_counts()<data_type_conversions>
input_shape =(dim, dim, 1)
 num_classes = 10
train_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True)
 test_data['PRIMEUNIT'].fillna(value="Prime_unk",inplace=True )<categorify>
optimizer = 'rmsprop'
loss = 'categorical_crossentropy'
metrics = ['accuracy']
train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True)
 train_data['AUCGUART'].fillna(value="AUC_unk",inplace=True)
 test_data['AUCGUART'].fillna(value="AUC_unk",inplace=True )<drop_column>
epochs = 100
batch_size = 1024
early_stop = EarlyStopping(monitor='val_loss',
min_delta=0,
patience=3,
verbose=True,
mode='auto',
baseline=None,
restore_best_weights=False)
 callbacks = [early_stop]
train_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice','MMRAcquisitionAuctionCleanPrice',
'MMRAcquisitionRetailAveragePrice','MMRAcquisitonRetailCleanPrice',
'MMRCurrentAuctionAveragePrice','MMRCurrentAuctionCleanPrice',
'MMRCurrentRetailAveragePrice','MMRCurrentRetailCleanPrice'],
inplace=True,axis=1)
 train_data.drop('PurchDate',axis=1,inplace=True)
 test_data.drop("PurchDate",axis=1,inplace=True )<drop_column>
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'<drop_column>
kernel_size =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size, activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']<drop_column>
kernel_size =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size, activation='relu',
input_shape=input_shape))
model.add(Conv2D(32, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=kernel_size, activation='relu'))
model.add(Conv2D(64, kernel_size=kernel_size, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']<feature_engineering>
kernel_size_1 =(7, 7)
 kernel_size_2 =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same',
input_shape=input_shape))
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test<drop_column>
kernel_size_1 =(7, 7)
 kernel_size_2 =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same',
input_shape=input_shape))
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']<filter>
kernel_size_1 =(7, 7)
 kernel_size_2 =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same',
input_shape=input_shape))
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
train_data[categorical[0]]<categorify>
kernel_size_1 =(7, 7)
 kernel_size_2 =(5, 5)
 model = Sequential()
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same',
input_shape=input_shape))
model.add(Conv2D(32, kernel_size=kernel_size_1, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=kernel_size_2, activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
pd.get_dummies(train_data[categorical[0]] )<categorify>
model.compile(optimizer=optimizer, loss=loss, metrics=metrics )
for i in categorical:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )<feature_engineering>
model.fit_generator(datagen.flow(images_train, labels_train,
batch_size=batch_size),
epochs=epochs,
verbose=True,
callbacks=callbacks,
validation_data=(imames_val, labels_val))
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))<feature_engineering>
pred_train = model.predict_classes(train_images_2D)
 pred_train.shape
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
hits =(pred_train == train_data.label)
 print('Hits: {}, i.e.{:.2f}%'.format(hits.sum() , hits.sum() / pred_train.shape[0] * 100))
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
miss =(pred_train != train_data.label)
 print('Misses: {}, i.e.{:.2f}%'.format(miss.sum() , miss.sum() / pred_train.shape[0] * 100))
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
cm = confusion_matrix(y_true=train_data.label, y_pred=pred_train)
 cm = pd.DataFrame(cm, index=range(num_classes), columns=range(num_classes))
cm
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<train_model>
eval_metrics = model.evaluate(x=train_images_2D, y=train_labels,
batch_size=batch_size, verbose=True, callbacks=callbacks)
 pd.DataFrame(eval_metrics, index=model.metrics_names, columns=['metric'] )
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
pred_Dig = model.predict_classes(dig_images_2D)
 pred_Dig.shape
KNN.score(X_test,y_test )<predict_on_test>
hits =(pred_Dig == dig_data.label)
 print('Hits: {}, i.e.{:.2f}%'.format(hits.sum() , hits.sum() / pred_Dig.shape[0] * 100))
predict=KNN.predict(test_data.drop('RefId',axis=1))<prepare_output>
miss =(pred_Dig != dig_data.label)
 print('Misses: {}, i.e.{:.2f}%'.format(miss.sum() , miss.sum() / pred_Dig.shape[0] * 100))
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()<prepare_output>
cm = confusion_matrix(y_true=dig_data.label, y_pred=pred_Dig)
 cm = pd.DataFrame(cm, index=range(num_classes), columns=range(num_classes))
cm
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )<save_to_csv>
eval_metrics = model.evaluate(x=dig_images_2D, y=dig_labels,
batch_size=batch_size, verbose=True, callbacks=callbacks)
 pd.DataFrame(eval_metrics, index=model.metrics_names, columns=['metric'] )
Submission.head()
Submission.to_csv('Submission.csv' )<load_from_csv>
epochs = early_stop.stopped_epoch + 1
model.fit(train_images_2D, train_labels,
batch_size=batch_size, epochs=epochs,
verbose=True )
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
pred_test = model.predict_classes(test_images_2D )
<count_missing_values><EOS> 
submission.label = pred_test
submission.to_csv('submission.csv' )