Kaggle competition: Aerial Cactus Identification
train_copy = train.copy()
use_columns = ['Pclass','Sex','Title','CabinLetter','Age','TicketStub','FamilySize_Code','Fare','Embarked','Survived','PctLived']
drop_columns = set(train.columns)- set(use_columns)
 train.drop(drop_columns, axis=1,inplace=True)
 train.columns<feature_engineering>
def load_data(dataframe=None, batch_size=16, mode='categorical'):
if dataframe is None:
dataframe = pd.read_csv('.. /input/train.csv')
 dataframe['has_cactus'] = dataframe['has_cactus'].apply(str)
 gen = ImageDataGenerator(rescale=1./255., validation_split=0.1, horizontal_flip=True, vertical_flip=True)
 trainGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='training')
 testGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 return trainGen, testGen
non_convertible_columns = \
train.columns[(train.dtypes == 'float64')|(train.dtypes == 'category')|(train.columns=='Survived')]
convertible_columns = set(train.columns)- set(non_convertible_columns)
 for column in convertible_columns:
train[column] = pd.Categorical(train[column] )<categorify>
def baseline_model() :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(2, activation='softmax'))
return model
train_with_dummies=pd.get_dummies(train,drop_first=True)
 train_with_dummies.head(1 )<prepare_x_and_y>
def train_baseline() :
batch_size = 32
trainGen, valGen = load_data(batch_size=batch_size)
 model = baseline_model()
opt = Adam(1e-3)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-5, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=4922, epochs=3, validation_data=valGen,
validation_steps=493, shuffle=True, callbacks=cbs)
 return model
def predict_baseline(model):
testdf = pd.read_csv('.. /input/sample_submission.csv')
 pred = np.empty(( testdf.shape[0],))
for n in tqdm(range(testdf.shape[0])) :
data = np.array(Image.open('.. /input/test/test/'+testdf.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
testdf['has_cactus'] = pred
testdf.to_csv('sample_submission.csv', index=False )
X_full = np.array(train_with_dummies.loc[:,(train_with_dummies.columns !='Survived')])
 y_full = np.array(train_with_dummies.loc[:,(train_with_dummies.columns=='Survived')].iloc[:,0] )<split>
model = train_baseline()
predict_baseline(model )