Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<compute_test_metric>
%matplotlib inline
style.use('fivethirtyeight')
 sns.set(style='whitegrid', color_codes=True)
 print(os.listdir(".. /input"))
final_sub = submission_xgb<save_to_csv>
random.seed(42)
 np.random.seed(42 )
save = pd.DataFrame({'ImageId':sample_submission.ImageId,'label':final_sub})
 print(save.head(10))
save.to_csv('submission.csv',index=False )<set_options>
PATH = ".. /input"
IMAGE_SIZE =(32, 32)
 train_dir=f'{PATH}/train/train'
test_dir=f'{PATH}/test/test'
train_img = os.listdir(train_dir)
 test_img = os.listdir(test_dir)
 train_df =pd.read_csv(f'{PATH}/train.csv')
 test_df = pd.read_csv(f'{PATH}/sample_submission.csv')
 train_new_dir = 'train'
if os.path.exists(train_new_dir):
shutil.rmtree(train_new_dir)
 else:
os.makedirs(train_new_dir)
 print(f"The number of rows in train and test set are {len(train_df)} and {len(test_df)}" )
%matplotlib inline
sns.set() 
<load_from_csv>
print(f"The number of rows in train and test set are {len(os.listdir(train_new_dir)) } and {len(test_df)}" )
gender_submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv" )<count_missing_values>
train, valid = train_test_split(train_df, stratify=train_df.has_cactus, test_size=0.33, random_state=2019 )
train.isnull().sum()<count_missing_values>
drop_rate=0.2
model = Sequential()
model.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(32,32,3)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(drop_rate))
model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(drop_rate))
model.add(GlobalAveragePooling2D())
 model.add(BatchNormalization())
 model.add(Dropout(drop_rate))
model.add(Dense(1,activation='sigmoid'))
train.isnull().sum()<count_missing_values>
model.compile(loss='binary_crossentropy',optimizer=Adam() , metrics=['accuracy'] )
test.isnull().sum()<count_missing_values>
file_path = 'best_weights.h5'
callbacks = [
ModelCheckpoint(file_path, monitor = 'val_acc', verbose = 1, save_best_only = True, mode = 'max'),
ReduceLROnPlateau(monitor = 'val_loss', factor = 0.8, patience = 4, verbose = 1, mode = 'min', min_lr = 1e-8),
EarlyStopping(monitor = 'val_loss', min_delta = 1e-10, patience = 32, verbose = 1, restore_best_weights = True)
 ]
test.isnull().sum()<feature_engineering>
epochs=128
history=model.fit_generator(train_generator,
steps_per_epoch=train_generator.n//train_generator.batch_size,
epochs=epochs,
verbose = 1,
shuffle=True,
validation_data=valid_generator,
validation_steps=valid_generator.n//valid_generator.batch_size,
callbacks = callbacks )
train_test_data= [train, test]
for dataset in train_test_data:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.',expand = False )<count_values>
model.load_weights(file_path )
train['Title'].value_counts()<count_values>
test_gen=ImageDataGenerator(
 rescale=1./255, 
)
 test_generator=test_gen.flow_from_dataframe(
 x_col='id',
y_col='has_cactus',
dataframe=test_df,
directory=test_dir,
class_mode='other',
color_mode='rgb',
batch_size=1,
target_size=IMAGE_SIZE,
shuffle=False
)
test['Title'].value_counts()<categorify>
test_generator.reset()
pred = model.predict_generator(test_generator, verbose=1, steps=test_generator.n)
 pred[pred>0.99]=1
pred[pred<0.01]=0
<drop_column><EOS> 
test_df['has_cactus'] = pred
test_df.to_csv('submission.csv', index = False )