Kaggle competition: Categorical Feature Encoding Challenge II
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),
results],axis = 1)
 submission.to_csv("cnn_mnist_datagen20210209.csv",index=False )<import_modules>
df_train = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/train.csv')
 df_test = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/test.csv')
 df_sub = pd.read_csv('/kaggle/input/cat-in-the-dat-ii/sample_submission.csv' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, BatchNormalization
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools<load_from_csv>
df_train.sort_index(inplace=True)
 df_train.head()
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<prepare_x_and_y>
y_train = df_train['target']
test_id = df_test['id']
df_train.drop(['target', 'id'], axis=1, inplace=True)
 df_test.drop('id', axis=1, inplace=True )
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]<categorify>
cat_feat_to_encode = df_train.columns.tolist()
smoothing=0.20
oof = pd.DataFrame([])
 for tr_idx, oof_idx in StratifiedKFold(n_splits=5, random_state= 1024, shuffle=True ).split(df_train, y_train):
ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(df_train.iloc[tr_idx, :], y_train.iloc[tr_idx])
 oof = oof.append(ce_target_encoder.transform(df_train.iloc[oof_idx, :]), ignore_index=False)
 ce_target_encoder = ce.TargetEncoder(cols = cat_feat_to_encode, smoothing=smoothing)
 ce_target_encoder.fit(df_train, y_train)
 df_train = oof.sort_index()
df_test = ce_target_encoder.transform(df_test )
X_train = X_train.values.reshape(-1, 28, 28, 1)/255.
 test = test.values.reshape(-1, 28, 28, 1)/255.
 y_train = to_categorical(y_train, 10 )<choose_model_class>
x_train = df_train.iloc[:,:].values
x_test = df_test.iloc[:,:].values
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.2
)<choose_model_class>
cw = class_weight.compute_class_weight('balanced',np.unique(y_train),y_train )
def create_model() :
model = Sequential()
model.add(Conv2D(32,(3,3), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Conv2D(128,(3,3), padding='same', activation='relu'))
model.add(Conv2D(128,(3,3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(2,2))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model<train_model>
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
EPOCHS = 30
BATCH_SIZE = 50
ENSEMBLES = 5
result_list = []
histories = []
results = np.zeros(( test.shape[0],10))
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.25, patience=2),
EarlyStopping(monitor='val_loss', min_delta=0.0005, patience=4)
 ]
for i in range(ENSEMBLES):
X_train_tmp, X_val, y_train_tmp, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=i)
 model = create_model()
print('training No.', i)
 history = model.fit(datagen.flow(X_train_tmp, y_train_tmp, batch_size=BATCH_SIZE),
verbose=0,
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train_tmp.shape[0] // BATCH_SIZE)
 histories.append(history)
 result = model.predict(test)
 results += result
result_list.append(result )<save_to_csv>
classifier = Sequential()
classifier.add(Dense(units = 512, kernel_initializer = 'uniform', activation = 'relu', input_dim = 23))
classifier.add(Dense(units = 256, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 128, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 64, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
classifier.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False))
results = np.argmax(results, axis=1)
 results = pd.Series(results, name='Label')
 submission = pd.concat([pd.Series(range(1,28001), name='ImageID'), results], axis=1)
 submission.to_csv('submission.csv', index=False )<import_modules>
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1 )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
<set_options>
classifier.fit(x_train, y_train, batch_size = 1000, epochs = 100, verbose=0,callbacks=[es], class_weight=cw )
CFG = {
'img_size': 512,
'tta': 3,
'valid_bs': 16,
'device': 'cuda' if torch.cuda.is_available() else 'cpu',
'effnet_models': ['model_4.pt', 'model_5.pt', 'model_6.pt', 'model_7.pt'],
'resnet_models': ['model_6.pt', 'model_8.pt', 'model_9.pt', 'model_10.pt']
}<categorify>
y_pred = classifier.predict_proba(x_test)[:,0]
<load_from_csv><EOS> 
df_sub = pd.DataFrame()
df_sub['id'] = test_id
df_sub['target'] = y_pred
df_sub.to_csv('submission.csv', index=False )