Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<save_to_csv>
%matplotlib inline
random.seed(0)
solution = pd.DataFrame({"id":house_test.Id, "SalePrice":0.7*lasso_preds + 0.3*xgb_preds})
 solution.to_csv("House_price.csv", index = False )<load_from_csv>
batch_size = 64
target_size =(32, 32)
 class_mode = 'binary'
epochs = 100
input_shape =(32,32,3)
 num_classes = 2
data_dir = ".. /input/train/train/"
validation_split = 0.8
color_mode='rgb'
x_col = 'id'
y_col='has_cactus'
dropout_dense_layer = 0.5
train = pd.read_csv('.. /input/train.csv' , index_col = 'PassengerId')
 label = train['Survived']
test = pd.read_csv('.. /input/test.csv', index_col = 'PassengerId')
 index = test.index<feature_engineering>
df = pd.read_csv('.. /input/train.csv')
 df.has_cactus = df.has_cactus.astype(str)
 msk = np.random.rand(len(df)) < validation_split
train = df[msk]
validation = df[~msk]
train['Deck'] = train.Cabin.str.get(0)
 test['Deck'] = test.Cabin.str.get(0)
 train['Deck'] = train['Deck'].fillna('NOTAVL')
 test['Deck'] = test['Deck'].fillna('NOTAVL')
 train.Deck.replace('T' , 'G' , inplace = True)
 train.drop('Cabin' , axis = 1 , inplace =True)
 test.drop('Cabin' , axis = 1 , inplace =True )<count_missing_values>
train_datagen = ImageDataGenerator(rescale=1/255, horizontal_flip=True, vertical_flip=True)
 train_generator = train_datagen.flow_from_dataframe(train, directory=data_dir, x_col=x_col, y_col=y_col, target_size=target_size, color_mode=color_mode, class_mode=class_mode, batch_size=batch_size, shuffle=True)
 validation_generator = train_datagen.flow_from_dataframe(validation, directory=data_dir, x_col=x_col, y_col=y_col, target_size=target_size, color_mode=color_mode, class_mode=class_mode, batch_size=batch_size, shuffle=True )
train.isna().sum()<count_missing_values>
class_weights = class_weight.compute_class_weight('balanced', np.unique(train_generator.classes), train_generator.classes)
 class_weights
test.isna().sum()<feature_engineering>
model = Sequential()
model.add(Conv2D(32,(3, 3), padding='same', activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32,(3, 3), padding='same', activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Conv2D(64,(3, 3),padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Conv2D(128,(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(Conv2D(256,(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(dropout_dense_layer))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss=keras.losses.binary_crossentropy,
optimizer='adam',
metrics=['accuracy'])
 model.summary()
callbacks = [EarlyStopping(monitor='val_loss', patience=20),
ReduceLROnPlateau(patience=10, verbose=1),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', verbose=0, save_best_only=True)]
history = model.fit_generator(train_generator,
validation_data=validation_generator,
epochs=epochs,
verbose=1,
shuffle=True,
callbacks=callbacks,
class_weight=class_weights )
train.loc[train.Embarked.isna() , 'Embarked'] = 'S'<groupby>
model.load_weights("best_model.h5")
 history.history['val_acc'][np.argmin(history.history['val_loss'])]
age_to_fill = train.groupby(['Pclass' , 'Sex' , 'Embarked'])[['Age']].median()
age_to_fill<data_type_conversions>
test_folder = ".. /input/test/"
test_datagen = ImageDataGenerator(
 rescale=1./ 255)
 test_generator = test_datagen.flow_from_directory(
 directory=test_folder,
target_size=target_size,
batch_size=1,
class_mode=None,
shuffle=False )
for cl in range(1,4):
for sex in ['male' , 'female']:
for E in ['C' , 'Q' , 'S']:
filll = pd.to_numeric(age_to_fill.xs(cl ).xs(sex ).xs(E ).Age)
 train.loc[(train.Age.isna() &(train.Pclass == cl)&(train.Sex == sex)
 &(train.Embarked == E)) , 'Age'] =filll
test.loc[(test.Age.isna() &(test.Pclass == cl)&(test.Sex == sex)
 &(test.Embarked == E)) , 'Age'] =filll<data_type_conversions>
sample_submission = pd.read_csv('.. /input/sample_submission.csv')
 filenames = [path.split('/')[-1] for path in test_generator.filenames]
probabilities = list(model.predict_generator(test_generator)[:,0])
 sample_submission.id = filenames
sample_submission.has_cactus = probabilities
sample_submission.to_csv('sample_submission.csv', index=False )