Kaggle competition: Aerial Cactus Identification
train_test['Cabin_code'] = train_test.Cabin.str.get(0 ).fillna('Z')
 train_test[['Survived','Cabin_code']].groupby('Cabin_code' ).mean()<count_missing_values>
data_dir = '/kaggle/input/aerial-cactus-identification'
labels = pd.read_csv(data_dir+"/train.csv")
 labels['has_cactus'] = labels['has_cactus'].astype('str')
 labels.head()
train.Embarked.isnull().sum() 
<groupby>
train_dir = "/kaggle/temp/train"
test_dir = "/kaggle/temp/test"
with zipfile.ZipFile(data_dir+"/train.zip","r")as z:
z.extractall("/kaggle/temp")
 with zipfile.ZipFile(data_dir+"/test.zip","r")as z:
z.extractall("/kaggle/temp/test_dummy" )
train[['Embarked','Survived']].groupby('Embarked' ).mean()<data_type_conversions>
im = cv2.imread(train_dir+"/0004be2cfeaba1c0361d39e2b000257b.jpg")
 plt.imshow(im )
train_test['Family_size']=train_test.SibSp+train_test.Parch
train_test['Is_alone']=1
train_test.loc[train_test['Family_size'] > 1,'Is_alone'] = 0
train_test['Family_size'].astype('int64')
 train_test['Is_alone'].astype('int64' )<categorify>
!pip install git+https://github.com/qubvel/efficientnet
<feature_engineering>
efficient_net = EfficientNetB7(
 weights='imagenet',
input_shape=(32,32,3),
include_top=False,
pooling='max' 
)
 model = Sequential()
model.add(efficient_net)
 model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
train_test['Age_band'] = pd.cut(train_test.Age, bins=10, precision=0 )<feature_engineering>
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.25,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest' 
)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = labels,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="training",
batch_size=256,
shuffle=True,
class_mode="binary" 
)
 val_generator = train_datagen.flow_from_dataframe(
 dataframe = labels,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
subset="validation",
batch_size=256,
shuffle=True,
class_mode="binary" 
)
 test_datagen = ImageDataGenerator(
 rescale=1./255.
)
 test_generator = test_datagen.flow_from_directory(
 '/kaggle/temp/test_dummy/',
target_size=(32,32),
batch_size=256,
shuffle=False,
class_mode=None
)
train_test['Fare_band'] = pd.qcut(train_test.Fare_log, q=13, precision=2 )<normalization>
early_stopping = EarlyStopping(
 monitor='val_accuracy',
patience=20,
mode='auto',
verbose=2 )
MM_scaler = MinMaxScaler(feature_range=(0,1))
train_test[['Age_scaled']]=MM_scaler.fit_transform(train_test[['Age']])
 sns.distplot(a=train_test.Age_scaled )<normalization>
history = model.fit_generator(
 train_generator,
epochs = 2000,
steps_per_epoch = 15,
validation_data = val_generator,
validation_steps = 10,
callbacks=[early_stopping]
)
St_scaler = StandardScaler()
train_test[['Fare_scaled']]=St_scaler.fit_transform(train_test[['Fare_log']])
 sns.distplot(a=train_test.Fare_scaled )<categorify>
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
)
 preds.shape
le = OrdinalEncoder()<categorify>
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )
train_test[['Age_code']] = le.fit_transform(train_test[['Age_band']])
 train_test[['Fare_code']] = le.fit_transform(train_test[['Fare_band']] )<data_type_conversions>
submission.to_csv("submission.csv", index=False )
for row in train_test:
train_test.loc[train_test['Family_size']==0, 'Family_type']=1
train_test.loc[(1<=train_test['Family_size'])&(train_test['Family_size']<=3), 'Family_type']=2
train_test.loc[(3<=train_test['Family_size'])&(train_test['Family_size']<=6), 'Family_type']=3
train_test.loc[7<=train_test['Family_size'], 'Family_type']=4
train_test.Family_type = train_test.Family_type.astype('int64' )<categorify>
submission.to_csv("submission.csv", index=False )