Kaggle competition: Aerial Cactus Identification
predicted_classes = model.predict_classes(X_test)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predicted_classes)+1)) ,
"Label": predicted_classes})
 submissions.to_csv("asd.csv", index=False, header=True )<save_model>
base_dir=".. /input/"
train_dir=os.path.join(base_dir,"train/train")
 test_dir=os.path.join(base_dir,"test/test")
 print("Training images :
{}".format(os.listdir(train_dir)[:10]), end='
')
 print("Testing images :
{}".format(os.listdir(test_dir)[:10]))
testing_dir=os.path.join(base_dir,"test" )
model.save('my_model_1.h5')
 json_string = model.to_json()<load_from_csv>
train_dataframe = pd.read_csv(".. /input/train.csv")
 train_dataframe["has_cactus"] = np.where(train_dataframe["has_cactus"] == 1, "yes", "no")
 print(train_dataframe.head() )
print(os.listdir(".. /input"))
sns.set(style="ticks")
 pd.options.display.max_rows = 200
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train_copy = train.copy()
print(train.info())
 print(train.isnull().sum() )<define_variables>
import keras
from keras.models import Sequential
from keras.layers import Flatten, Dense, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras import applications
from efficientnet import EfficientNetB3
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')) , 
('ohe', OneHotEncoder(handle_unknown='ignore')) ])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numeric_transformer, numeric_features), 
('cat', categorical_transformer, categorical_features)])
 clf = Pipeline(steps=[('preprocessor', preprocessor), 
('classifier', LogisticRegression())])
 X=train.drop(columns=['Survived'])
 y=train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=0)
 clf.fit(X_train, y_train)
 print("model score: %.3f" % clf.score(X_test, y_test)) 
<train_model>
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.10,
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 = train_dataframe,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(75,75),
subset="training",
batch_size=250,
shuffle=True,
class_mode="binary" 
)
 valid_generator = train_datagen.flow_from_dataframe(
 dataframe = train_dataframe,
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(75,75),
subset="validation",
batch_size=125,
shuffle=True,
class_mode="binary"
)
clf = Pipeline(steps=[('preprocessor', preprocessor), 
('classifier', RandomForestClassifier())])
 X=train.drop(columns=['Survived'])
 y=train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=0)
 clf.fit(X_train, y_train)
 print("model score: %.3f" % clf.score(X_test, y_test))<define_variables>
pretrained_net = EfficientNetB3(
 input_shape=(75,75,3),
include_top=False,
pooling='max', 
)
 model = Sequential()
model.add(pretrained_net)
 model.add(Dense(units = 120, activation='relu'))
model.add(Dense(units = 84, activation = 'relu'))
model.add(Dense(units = 1, activation='sigmoid'))
afeatures = ['Age', 'Fare'] + list(clf['preprocessor'].transformers_[1][1]['ohe'].get_feature_names(categorical_features)) 
<define_variables>
model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )
afeatures = ['Age', 'Fare','Embarked', 'Sex', 'Pclass']<count_values>
history = model.fit_generator(
 train_generator,
epochs = 20,
steps_per_epoch = 63,
validation_data = valid_generator,
validation_steps = 14 
)
train['Cabin'].fillna('0' ).str[:1].value_counts()<feature_engineering>
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )
train['modCabin'] = train['Cabin'].fillna('0' ).str[:1]
test['modCabin'] = train['Cabin'].fillna('0' ).str[:1]<count_values>
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
preds = preds.flatten()
data = {'id': image_ids, 'has_cactus':preds}
submission = pd.DataFrame(data)
 print(submission.head() )
train['Survived'].value_counts()<count_values>
submission.to_csv("submission.csv", index=False )
<groupby><EOS> 
submission.to_csv("submission.csv", index=False )