Kaggle competition: Aerial Cactus Identification
ranfor_model = RandomForestClassifier(random_state=1,
max_depth=10,
max_leaf_nodes=10,
min_samples_leaf=2,
min_samples_split=2)
 ranfor_model.fit(X,y)
 y_pred = ranfor_model.predict(test_df_new )<save_to_csv>
with zipfile.ZipFile(".. /input/aerial-cactus-identification/train.zip","r")as z:
z.extractall("/kaggle/temp/")
 with zipfile.ZipFile(".. /input/aerial-cactus-identification/test.zip","r")as z:
z.extractall("/kaggle/temp/test/")
 print(len(os.listdir(".. /temp/train")))
 print(len(os.listdir(".. /temp/test/test")) )
submission = pd.DataFrame({
"PassengerId": range(892,1310),
"Survived": y_pred
})
 submission.to_csv('titanic.csv', index=False )<set_options>
train_dir = ".. /temp/train"
test_dir = ".. /temp/test"
labels = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 labels.has_cactus = labels.has_cactus.astype(str)
 print(labels['has_cactus'].value_counts() )
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
validation_split = 0.8
idxs = np.random.permutation(range(len(labels)))< validation_split*len(labels)
 train_labels = labels[idxs]
val_labels = labels[~idxs]
print(len(train_labels), len(val_labels))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.describe(include="all" )<count_missing_values>
train_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1/255, horizontal_flip=True, vertical_flip=True)
 batch_size = 128
train_generator = train_datagen.flow_from_dataframe(train_labels,directory=train_dir,x_col='id',
y_col='has_cactus',class_mode='binary',batch_size=batch_size,
target_size=(32,32))
val_generator = train_datagen.flow_from_dataframe(val_labels,directory=train_dir,x_col='id',
y_col='has_cactus',class_mode='binary',batch_size=batch_size,
target_size=(32,32))
print(pd.isnull(train ).sum() )<drop_column>
input_shape =(32, 32, 3)
 model = keras.models.Sequential()
model.add(Conv2D(32,(3, 3), padding='same', activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(128,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Conv2D(128,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1 )<drop_column>
model.compile(loss = keras.losses.binary_crossentropy,
optimizer = 'adam',
metrics = ['acc'])
 callbacks = [EarlyStopping(monitor='val_loss', patience=20, verbose=1, restore_best_weights=True),
ReduceLROnPlateau(patience=10, verbose=1),
]
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )<data_type_conversions>
epochs = 100
history = model.fit(train_generator,
epochs = epochs,
verbose = 1,
callbacks = callbacks,
validation_data = val_generator,
)
train = train.fillna({"Embarked": "S"} )<feature_engineering>
idx = np.argmax(history.history['val_acc'])
 print(history.history['val_loss'][idx], history.history['val_acc'][idx])
 idx = np.argmin(history.history['val_loss'])
 print(history.history['val_loss'][idx], history.history['val_acc'][idx] )
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
test_datagen = keras.preprocessing.image.ImageDataGenerator(rescale = 1/255)
 test_generator = test_datagen.flow_from_directory(
 directory = test_dir,
target_size =(32, 32),
batch_size = 1,
class_mode = None,
shuffle = False)
 probabilities = model.predict(test_generator )
<categorify><EOS> 
sample_submission = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv')
 df = pd.DataFrame({'id': sample_submission['id']})
 df['has_cactus'] = probabilities
df.to_csv("submission.csv", index=False )