Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
np.random.seed(13 )
from sklearn.neural_network import MLPClassifier
from sklearn import metrics
import joblib<prepare_x_and_y>
num_classes = 10
batch_size = 128
epochs = 700
img_rows, img_cols = 28, 28
input_shape =(img_rows, img_cols,1 )
hidden_layer_sizes=(100,)
 activation = 'relu'
solver = 'adam'
batch_size = 'auto'
alpha = 0.0001
random_state = 0
max_iter = 10000
x = ['bone_length', 'rotting_flesh', 'hair_length', 'has_soul']
train_X = training[x]
y1 = ['type']
train_y1 = training[y1]
clf = MLPClassifier(
 hidden_layer_sizes=hidden_layer_sizes,
activation=activation,
solver=solver,
batch_size=batch_size,
alpha=alpha,
random_state=random_state,
max_iter=max_iter, 
)
 clf.fit(train_X, train_y1)
 SAVE_TRAINED_DATA_PATH = 'train1.learn'
joblib.dump(clf, SAVE_TRAINED_DATA_PATH)
 clf1 = joblib.load(SAVE_TRAINED_DATA_PATH)
 test_X1 = testing[x]
predict_y1 = clf1.predict_proba(test_X1)
 predict_Y1 = clf1.predict(test_X1)
 print(predict_Y1)
 print("学習データの精度: {:.3f}".format(clf.score(train_X, train_y1)))
 print("テスト結果の精度: {:.3f}".format(clf.score(test_X1, predict_Y1)) )<save_to_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
sub = pd.read_csv('sample_submission.csv')
 sub['type'] = list(predict_Y1)
 sub.to_csv('sample_submission.csv', index=False )<set_options>
y_train = train["label"]
x_train = train.drop(labels = ["label"],axis = 1 )
warnings.filterwarnings('ignore' )<load_pretrained>
x_train /= 255
test /= 255
zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/train.csv.zip' ).extractall()
zipfile.ZipFile('/kaggle/input/ghouls-goblins-and-ghosts-boo/test.csv.zip' ).extractall() 
<load_from_csv>
x_train = x_train.values.reshape(-1,img_rows,img_cols,1 ).astype('float32')
 test = test.values.reshape(-1,img_rows,img_cols,1 ).astype('float32' )
train = pd.read_csv('./train.csv')
 test = pd.read_csv('./test.csv')
<categorify>
y_train = keras.utils.to_categorical(y_train, num_classes = num_classes )
train2 = pd.get_dummies(train['type'])
 del train['type']
COLOR = pd.get_dummies(train['color'])
 del train['color']
del train['id']
target = pd.DataFrame(train2['Ghost']* 0 + train2['Ghoul'] * 2 + train2['Goblin'] * 1)
 target_GOB = pd.DataFrame(train2['Ghost']* 0 + train2['Ghoul'] * 0 + train2['Goblin'] * 1)
 target = target.rename(columns={'0': 'target'})
 COLOR2 = pd.get_dummies(test['color'])
 del test['color']
ID = test["id"]
del test['id']<set_options>
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size = 0.1 )
%matplotlib inline 
<merge>
def model_cnn(input_shape=input_shape, num_classes=num_classes):
model = Sequential()
model.add(Conv2D(32, kernel_size =(3,3), activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size =(3,3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size =(5,5), strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size =(3,3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size =(3,3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size =(5,5), strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(num_classes, activation = "softmax"))
model.compile(optimizer = Adam() , loss = "categorical_crossentropy", metrics=["accuracy"])
 return model
train_c = train.join(COLOR)
 test_c = test.join(COLOR2)
<choose_model_class>
def LeNet5(input_shape=input_shape,num_classes=num_classes):
model = Sequential()
model.add(Conv2D(6, kernel_size=(5, 5), strides=(1, 1), activation='relu', input_shape=input_shape, padding="same"))
model.add(AveragePooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid'))
model.add(Conv2D(16, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
model.add(Conv2D(120, kernel_size=(5, 5), strides=(1, 1), activation='relu', padding='valid'))
model.add(Flatten())
 model.add(Dense(84, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(optimizer = Adam() , loss = "categorical_crossentropy", metrics=["accuracy"])
 return model
def GOB() :
global y_pred_c
global train
global test
hidden_layer_sizes=(100,)
 activation = 'relu'
solver = 'adam'
batch_size = 'auto'
alpha = 0.0001
random_state = 0
max_iter = 10000
early_stopping = True
train_X = train_c
train_y1 = target_GOB
clf = MLPRegressor(
 hidden_layer_sizes=hidden_layer_sizes,
activation=activation,
solver=solver,
batch_size=batch_size,
alpha=alpha,
random_state=random_state,
max_iter=max_iter, 
)
 clf.fit(train_X, train_y1)
 SAVE_TRAINED_DATA_PATH = 'train1.learn'
joblib.dump(clf, SAVE_TRAINED_DATA_PATH)
 clf1 = joblib.load(SAVE_TRAINED_DATA_PATH)
 test_X1 = test_c
predict_Y1 = clf1.predict(train_X)
 print(predict_Y1)
 y_pred_c = pd.DataFrame(predict_Y1)
 train = train.join(y_pred_c)
 predict_Y1 = clf1.predict(test_X1)
 print(predict_Y1)
 y_pred_c = pd.DataFrame(predict_Y1)
 test = test.join(y_pred_c )<concatenate>
datagen = ImageDataGenerator(rotation_range=10, zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1)
 datagen.fit(x_train )
dataset = pd.concat([train, test], ignore_index = True)
 ',train.isnull().sum() )<choose_model_class>
models = []
for i in range(len(model)) :
model[i].fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, steps_per_epoch=x_train.shape[0] // batch_size,
validation_data =(x_test,y_test),
callbacks=[ReduceLROnPlateau(monitor='loss', patience=3, factor=0.1)],
verbose=2)
 models.append(model[i] )
def lightGBM_k() :
global y_pred_AA
hidden_layer_sizes=(100,)
 activation = 'relu'
solver = 'adam'
batch_size = 'auto'
alpha = 0.0001
random_state = 0
max_iter = 10000
early_stopping = True
train_X = train_k
train_y1 = target_k
clf = MLPRegressor(
 hidden_layer_sizes=hidden_layer_sizes,
activation=activation,
solver=solver,
batch_size=batch_size,
alpha=alpha,
random_state=random_state,
max_iter=max_iter, 
)
 clf.fit(train_X, train_y1)
 SAVE_TRAINED_DATA_PATH = 'train1.learn'
joblib.dump(clf, SAVE_TRAINED_DATA_PATH)
 clf1 = joblib.load(SAVE_TRAINED_DATA_PATH)
 test_X1 = test_k
predict_Y = clf1.predict(test_X1)
 print(predict_Y)
 predict_Y = np.round(predict_Y, decimals=1)
 predict_Y1 = np.round(predict_Y)
 print(predict_Y1)
 y_pred_AA = predict_Y1
print("学習データの精度: {:.3f}".format(clf.score(train_X, train_y1)))
 print("テスト結果の精度: {:.3f}".format(clf.score(test_X1, predict_Y1)) )<create_dataframe>
labels = []
for m in models:
predicts = np.argmax(m.predict(test), axis=1)
 labels.append(predicts)
 labels = np.array(labels)
 labels = np.transpose(labels,(1, 0))
labels = scipy.stats.mode(labels, axis=-1)[0]
labels = np.squeeze(labels )
<save_to_csv><EOS> 
pd.DataFrame({'ImageId' : np.arange(1, predicts.shape[0] + 1), 'Label' : labels } ).to_csv('submission.csv', index=False )