Kaggle competition: Aerial Cactus Identification
train.groupby('Sex')['Age'].agg(['size', 'count', 'mean'] )<groupby>
data = pd.read_csv('.. /input/train.csv')
 data['path'] = '.. /input/train/train/'
data['path'] = data[['path','id']].apply(lambda x: "".join(x), axis=1)
 data.drop(['id'],axis=1,inplace=True)
 data.head()
train.groupby('Embarked')['Age'].agg(['size', 'count', 'mean'] )<choose_model_class>
X_train,X_val,Y_train,Y_val = train_test_split(imgs,labels,test_size=0.3, stratify=labels)
 train_datagen = ImageDataGenerator(rescale=1./255,
zoom_range=0.3,
rotation_range=50,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
 val_datagen = ImageDataGenerator(rescale=1./255)
 train_generator = train_datagen.flow(X_train, Y_train, batch_size=32)
 val_generator = val_datagen.flow(X_val, Y_val, batch_size=32 )
np.random.seed(2019)
 numeric_features = ['Age', 'Fare']
numeric_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='median')) , 
('scaler', StandardScaler())])
 categorical_features = ['Embarked', 'Sex', 'Pclass']
categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant', fill_value='missing')) , 
('onehot', OneHotEncoder(handle_unknown='ignore')) ])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numeric_transformer, numeric_features), 
('cat', categorical_transformer, categorical_features)])
 clf = Pipeline(steps=[('preprocessor', preprocessor), 
('classifier', LogisticRegression(solver='lbfgs')) ])
 Xt_train, Xt_test, yt_train, yt_test = train_test_split(train, y_train,stratify=y_train, test_size=0.3)
 clf.fit(Xt_train, yt_train)
 print("model score: %.3f" % clf.score(Xt_test, yt_test))
print(cross_val_score(clf, train, y_train, cv=5))<concatenate>
inp = Input(IMG_DIM)
 conv_1 = Conv2D(64, kernel_size=(2,2), activation='relu' )(inp)
 pool_1 = MaxPooling2D(pool_size=(2,2))(conv_1)
 conv_2 = Conv2D(32, kernel_size=(2,2), activation='relu' )(pool_1)
 pool_2 = MaxPooling2D(pool_size=(2,2))(conv_2)
 flatten = Flatten()(pool_2)
 dropout_1 = Dropout(0.3 )(flatten)
 dense_1 = Dense(512, activation='relu' )(dropout_1)
 dropout_2 = Dropout(0.3 )(dense_1)
 dense_2 = Dense(64,activation='relu' )(dropout_2)
 dropout_3 = Dropout(0.2 )(dense_2)
 output = Dense(1, activation='sigmoid' )(dropout_3)
 model = Model(inp,output)
 model.compile(loss='binary_crossentropy', optimizer=optimizers.Adam(lr=1e-4), metrics=['accuracy'])
 model.summary()
features= ['Age', 'Fare','SibSp', 'Parch','Sex','Embarked','Pclass']
num_features = ['SibSp','Parch']
num_features_to_scale = ['Age', 'Fare']
cat_features = ['Sex', 'Embarked','Pclass']
train_test_concat = pd.concat([train[features],test[features]] )<normalization>
model_checkpoint = ModelCheckpoint('model_best_checkpoint.h5', save_best_only=True, monitor='val_acc', mode='max', verbose=2)
 early_stopping = EarlyStopping(monitor='val_loss', patience=15, mode='min')
 callback_list = [model_checkpoint]
history = model.fit_generator(train_generator, steps_per_epoch=150, epochs=400,
validation_data=val_generator, validation_steps=50,
verbose=1,callbacks=callback_list )
scaler = StandardScaler()
scaler.fit(train_test_concat[num_features_to_scale] )<normalization>
tests = glob.glob('.. /input/test/test/*')
 imgs = np.array([ img_to_array(load_img(file, target_size = IMG_DIM)) for file in tests ])/255
submission = pd.DataFrame({'id':tests})
 submission.id = submission.id.apply(lambda x: x.split('/')[-1])
 submission.head()
train_scaled = train[num_features_to_scale].copy()
train_scaled = scaler.transform(train_scaled)
 train_scaled = pd.DataFrame(train_scaled,columns=num_features_to_scale)
 train_scaled = pd.concat([train_scaled,train[cat_features],train[num_features]],axis=1)
 print(train_scaled.info() )<normalization>
submission['has_cactus'] = np.squeeze(model.predict(imgs))
submission.to_csv('samplesubmission.csv',index=False)
 submission.head()