Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_on_grid>
import numpy as np
import pandas as pd
import seaborn as sns
from seaborn import countplot
import matplotlib.pyplot as plt
from keras import optimizers
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Activation, BatchNormalization
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
def SVM(train, test, target):
clf_result=svm.SVC(kernel='rbf', gamma=1/2 , C=1.0,class_weight='balanced', random_state=0)
 clf_result.fit(train, target)
 predict= np.array(clf_result.predict(test))
return predict<categorify>
TRAIN_PATH = '.. /input/train.csv'
TEST_PATH = '.. /input/test.csv'
SUBMISSION_NAME = 'submission.csv'
BATCH_SIZE = 64
EPOCHS = 45
LEARNING_RATE = 0.001
HEIGHT = 28
WIDTH = 28
CANAL = 1
N_CLASSES = 10
def LogRes(ghost,ghoul,goblin, test, target,HowDo):
vsnp= np.empty(( 529,6),dtype="float64")
 submission=np.empty(( 529),dtype="int")
 ghost0=np.zeros(len(ghost)) ;ghost1=np.ones(len(ghost))
ghoul0=np.zeros(len(ghoul)) ;ghoul1=np.ones(len(ghoul))
goblin0=np.zeros(len(goblin)) ;goblin1=np.ones(len(goblin))
vs1 = ghost.append(ghoul, ignore_index=True);vs1t = np.append(ghost0,ghoul1)
 vs2 = ghoul.append(goblin, ignore_index=True);vs2t = np.append(ghoul0,goblin1)
 vs3 = goblin.append(ghost, ignore_index=True);vs3t = np.append(goblin0,ghost1)
 if HowDo == "log":
model1 = LogisticRegression() ;model1.fit(vs1,vs1t);vsnp[:,0:2]=model1.predict_proba(test)
 model2 = LogisticRegression() ;model2.fit(vs2,vs2t);vsnp[:,2:4]=model2.predict_proba(test)
 model3 = LogisticRegression() ;model3.fit(vs3,vs3t);vsnp[:,4:6]=model3.predict_proba(test)
 if HowDo == "net":
vsnp[:,0]=1-N_netlog(vs1, test, vs1t);vsnp[:,1]=1-vsnp[:,0]
vsnp[:,2]=1-N_netlog(vs2, test, vs2t);vsnp[:,3]=1-vsnp[:,2]
vsnp[:,4]=1-N_netlog(vs3, test, vs3t);vsnp[:,5]=1-vsnp[:,4]
for n in range(len(vsnp)) :
if np.argmax(vsnp[n,:])==0:
submission[n]=0
if np.argmax(vsnp[n,:])==1:
submission[n]=2
if np.argmax(vsnp[n,:])==2:
submission[n]=2
if np.argmax(vsnp[n,:])==3:
submission[n]=1
if np.argmax(vsnp[n,:])==4:
submission[n]=1
if np.argmax(vsnp[n,:])==5:
submission[n]=0
print(vsnp)
 return submission<categorify>
train = pd.read_csv(TRAIN_PATH)
 test = pd.read_csv(TEST_PATH)
 labels = train['label']
train = train.drop(['label'], axis=1 )
def LogRes2(ghost,ghoul,goblin, test, target,HowDo):
vsnp=np.empty(( 529,3),dtype="float64")
 submission=np.empty(( 529),dtype="int")
 ghost0=np.zeros(len(ghost)) ;ghost1=np.ones(len(ghost))
ghoul0=np.zeros(len(ghoul)) ;ghoul1=np.ones(len(ghoul))
goblin0=np.zeros(len(goblin)) ;goblin1=np.ones(len(goblin))
vs1 = ghost.append(ghoul, ignore_index=True)
 vs1 = vs1.append(goblin, ignore_index=True)
 vs1t = np.append(ghost0,ghoul1)
 vs1t = np.append(vs1t,goblin1)
 vs2 = ghoul.append(goblin, ignore_index=True)
 vs2 = vs2.append(ghost, ignore_index=True)
 vs2t = np.append(ghoul0,goblin1)
 vs2t = np.append(vs2t,ghost1)
 vs3 = goblin.append(ghost, ignore_index=True)
 vs3 = vs3.append(ghoul, ignore_index=True)
 vs3t = np.append(goblin0,ghost1)
 vs3t = np.append(vs3t,ghoul1)
 if HowDo == "log":
model1 = LogisticRegression() ;model1.fit(vs1,vs1t);vsnp[:,0]=model1.predict_proba(test)[:,0]
model2 = LogisticRegression() ;model2.fit(vs2,vs2t);vsnp[:,1]=model2.predict_proba(test)[:,0]
model3 = LogisticRegression() ;model3.fit(vs3,vs3t);vsnp[:,2]=model3.predict_proba(test)[:,0]
if HowDo == "net":
vsnp[:,0]=1-N_netlog(vs1, test, vs1t)
 vsnp[:,1]=1-N_netlog(vs2, test, vs2t)
 vsnp[:,2]=1-N_netlog(vs3, test, vs3t)
 for n in range(len(vsnp)) :
if np.argmax(vsnp[n,:])==0:
submission[n]=0
if np.argmax(vsnp[n,:])==1:
submission[n]=2
if np.argmax(vsnp[n,:])==2:
submission[n]=1
print(vsnp)
 return submission<create_dataframe>
labels.value_counts()
def syuunou(vote, ID):
pred:str=[]
for n in range(len(ID)) :
if np.argmax(vote[:,n])==0:
pred.append('Ghost')
 if np.argmax(vote[:,n])==2:
pred.append('Ghoul')
 if np.argmax(vote[:,n])==1:
pred.append('Goblin')
 s_c= pd.DataFrame({"id": ID, "type": pred})
 return s_c<compute_test_metric>
train = train.values.reshape(-1,HEIGHT,WIDTH,CANAL)
 test = test.values.reshape(-1,HEIGHT,WIDTH,CANAL)
 labels = labels.values
def tohyo(predict, vote):
vote[0] +=(predict==0);vote[1] +=(predict==1);vote[2] +=(predict==2 )<load_from_csv>
labels = pd.get_dummies(labels ).values
def main_n() :
train = pd.read_csv('./train.csv')
 test = pd.read_csv('./test.csv')
 type_array = pd.get_dummies(train['type']); del train['type']
COLOR = pd.get_dummies(train['color']); del train['color'] ;del train['id']
COLOR2 = pd.get_dummies(test['color']); del test['color']; ID = test["id"]; del test['id']
vote = np.zeros(( 3,529),dtype = 'int')
 target = pd.DataFrame(type_array['Ghost']* 0 + type_array['Ghoul'] * 2 + type_array['Goblin'] * 1)
 ghost=train[type_array['Ghost']==1]
ghoul=train[type_array['Ghoul']==1]
goblin=train[type_array['Goblin']==1]
tohyo(N_net(train, test, target), vote)
 tohyo(lightgbm(train, test, target), vote)
 tohyo(SVM(train, test, target), vote)
 tohyo(LogRes2(ghost,ghoul,goblin, test, target,"log"), vote)
 tohyo(LogRes2(ghost,ghoul,goblin, test, target,"net"), vote)
 train = train.join(COLOR)
 test = test.join(COLOR2)
 ghost=train[type_array['Ghost']==1]
ghoul=train[type_array['Ghoul']==1]
goblin=train[type_array['Goblin']==1]
tohyo(LogRes2(ghost,ghoul,goblin, test, target,"net"), vote)
 tohyo(N_net(train, test, target), vote)
 submission = syuunou(vote, ID)
 return submission<save_to_csv>
train = train / 255.0
test = test / 255.0
submission=main_n()
rows=submission
submission = rows
submission.to_csv("submission6.csv", index=False )<choose_model_class>
x_train, x_val, y_train, y_val = train_test_split(train, labels, test_size=0.1, random_state=1 )
<categorify>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
horizontal_flip=False,
vertical_flip=False,
rotation_range=15,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15)
 datagen.fit(x_train )
<load_from_csv>
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5),padding='Same', input_shape=(HEIGHT, WIDTH, CANAL)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(filters=32, kernel_size=(5,5),padding='Same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(3,3),padding='Same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(filters=64, kernel_size=(3,3),padding='Same'))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(N_CLASSES, activation = "softmax"))
optimizer = optimizers.adam(lr=LEARNING_RATE)
 model.compile(optimizer=optimizer , loss="categorical_crossentropy", metrics=["accuracy"] )
train_data=pd.read_csv('/kaggle/input/ghouls-goblins-and-ghosts-boo/train.csv.zip')
 train_data.head()<count_values>
print('Dataset size: %s' % train.shape[0])
 print('Epochs: %s' % EPOCHS)
 print('Learning rate: %s' % LEARNING_RATE)
 print('Batch size: %s' % BATCH_SIZE)
 print('Input dimension:(%s, %s, %s)' %(HEIGHT, WIDTH, CANAL))
train_data['type'].value_counts()<load_from_csv>
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS, validation_data=(x_val, y_val),
verbose=2, steps_per_epoch=x_train.shape[0] // BATCH_SIZE )
test_data=pd.read_csv('/kaggle/input/ghouls-goblins-and-ghosts-boo/test.csv.zip')
 test_data.head()<count_values>
predictions = model.predict_classes(test )
train_data['color'].value_counts()<count_values>
submission = pd.DataFrame({"ImageId": list(range(1, len(predictions)+ 1)) , "Label": predictions})
 submission.to_csv(SUBMISSION_NAME, index=False)
 submission.head(10 )