Kaggle competition: Aerial Cactus Identification
common_titles = ["Mr", "Mrs", "Miss", "Master"]
titles = []
for name in df_combined['Name']:
title = name.split(',')[1].split('.')[0].strip()
if title in common_titles:
titles.append(title)
 elif title=="Mlle":
titles.append("Miss")
 elif title=="Mme":
titles.append("Mrs")
 else:
titles.append("Rare")
 df_titles = pd.DataFrame(titles, columns=['Titles'])
 title_dummies = pd.get_dummies(df_titles['Titles'], prefix='Title')
 title_dummies = title_dummies.reset_index(drop=True)
 df_combined = df_combined.reset_index(drop=True)
 df_combined = pd.concat([df_combined, title_dummies], axis=1 )<categorify>
train_dir = ".. /input/aerial-cactus-identification/train/train/"
test_dir = ".. /input/aerial-cactus-identification/test/test/"
train_df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 train_df.head()
label = train_df['has_cactus'].tolist()
fn = train_df['id'].tolist()
gender_dummies = pd.get_dummies(df_combined['Sex'])
 df_combined = pd.concat([df_combined, gender_dummies], axis=1)
 df_combined.drop('Sex', axis=1, inplace=True )<drop_column>
train_fn = fn[:14000]
val_fn = fn[14000:]
train_labels = label[:14000]
val_labels = label[14000:]
for i in zip(train_labels,train_fn):
if i[0] == 1:
copy2(str(train_dir+i[1]),str('.. /img_train/yes/'+i[1]))
elif i[0] == 0:
copy2(str(train_dir+i[1]),str('.. /img_train/no/'+i[1]))
for i in zip(val_labels,val_fn):
if i[0] == 1:
copy2(str(train_dir+i[1]),str('.. /img_val/yes/'+i[1]))
elif i[0] == 0:
copy2(str(train_dir+i[1]),str('.. /img_val/no/'+i[1]))
df_combined.drop(['PassengerId',"Name","Ticket"], axis=1, inplace=True )<split>
train_datagen = ImageDataGenerator(
 rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True 
)
 val_datagen = ImageDataGenerator(rescale=1./255)
 val_generator = val_datagen.flow_from_directory(
 '.. /img_val/',
target_size=(32,32),
color_mode="rgb",
batch_size=64,
class_mode='binary')
 train_generator = train_datagen.flow_from_directory(
 '.. /img_train/',
target_size=(32,32),
color_mode="rgb",
batch_size=64,
class_mode='binary')
 step_train = train_generator.n//train_generator.batch_size
step_val= val_generator.n//val_generator.batch_size
x_train = df_combined[:891].copy()
x_test = df_combined[891:].copy()
x_test.reset_index(inplace=True, drop=True)
 x_train.shape, x_test.shape<choose_model_class>
base_model = ResNet50(weights=".. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5",include_top=False,input_shape=(32,32,3))
for layer in base_model.layers:
layer.trainable = True
random_forest = RandomForestClassifier()
random_forest.fit(x_train, y_train)
 y_pred_random_forest = random_forest.predict(x_test)
 random_forest_accuracy = round(random_forest.score(x_train, y_train)*100, 2 )<choose_model_class>
x = base_model.output
x = GlobalAveragePooling2D()(x)
 x = Dropout(rate=0.6 )(x)
 predictions = Dense(1, activation = 'sigmoid',activity_regularizer=regularizers.l1(0.01))(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['accuracy'])
log_regres = LogisticRegression()
log_regres.fit(x_train, y_train)
 y_pred_log_regres = log_regres.predict(x_test)
 log_regres_accuracy = round(log_regres.score(x_train, y_train)*100, 2 )<compute_train_metric>
cp = ModelCheckpoint('ResNet50.hdf5' , monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=3, min_lr=0.0001)
 model.fit_generator(
 train_generator,
steps_per_epoch=step_train,
validation_data= val_generator,
validation_steps = step_val,
epochs=20,
callbacks=[cp,reduce_lr] )
knn = KNeighborsClassifier(n_neighbors=3)
 knn.fit(x_train, y_train)
 y_pred_knn = knn.predict(x_test)
 knn_accuracy = round(knn.score(x_train, y_train)*100, 2 )<predict_on_test>
model.load_weights("ResNet50.hdf5")
 print(os.listdir(".. /input/aerial-cactus-identification/"))
<compute_train_metric><EOS> 
test_set = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 pred = np.empty(( test_set.shape[0],))
for n in range(4000):
data = np.array(Image.open('.. /input/aerial-cactus-identification/test/test/'+test_set.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))
 test_set['has_cactus'] = pred
test_set.to_csv('sample_submission.csv', index=False )