Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<compute_train_metric>
DEV_MODE = False
data_root_path = pathlib.Path(".. /input" )
classifier = XGBClassifier()
classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test)
 xgb_acc = accuracy_score(y_pred, y_test)
 print('acc=',xgb_acc )<train_model>
def load_and_preprocess_image(path):
image = tf.io.read_file(path)
 image = tf.image.decode_jpeg(image, channels=3)
 image = tf.image.resize(image, [32, 32])
 image /= 255.0
return image
print('RF_acc=', Random_forest_acc)
 print('Logistic_acc=', LogisticReg_acc)
 print('SVC_acc=', SVC_acc)
 print('Gaussian_acc=', Gaussian_acc)
 print('DecisionTree_acc=', DT_acc)
 print('GradBoost_acc=', GB_acc)
 print('XGBoost_acc=', xgb_acc)
<train_model>
train_df = pd.read_csv(data_root_path/'train.csv', dtype={'id': 'str', 'has_cactus': np.int32})
 DATASET_SIZE = len(train_df)
 print("n =", DATASET_SIZE )
rf_model = RandomForestClassifier(criterion= 'gini', n_estimators = 100 ,max_depth = 6, random_state = 0)
 rf_model.fit(X, y)
 final_pred = rf_model.predict(test_set)
 final_pred<prepare_output>
no_cactus, yes_cactus = train_df.has_cactus.value_counts().sort_values().values
no_multiplier = int(yes_cactus/no_cactus)-1
no_cactus_rows = train_df[train_df.has_cactus == 0]
for i in range(no_multiplier):
train_df = train_df.append(no_cactus_rows )
survivors = pd.DataFrame(final_pred, columns = ['Survived'])
 len(survivors)
 survivors.insert(0, 'PassengerId', test['PassengerId'], True)
 survivors<save_to_csv>
ROOT_TRAIN_PATH = '.. /input/train/train/'
train_paths = ROOT_TRAIN_PATH + train_df['id']
survivors.to_csv('Submission.csv', index = False )<set_options>
path_ds = tf.data.Dataset.from_tensor_slices(train_paths)
 image_ds = path_ds.map(load_and_preprocess_image)
 label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(train_df['has_cactus'].values, tf.int32))
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))<load_from_csv>
IMAGE_SHAPE =(32, 32, 3)
 image_label_ds = tf.data.Dataset.zip(( image_ds, label_ds)).shuffle(DATASET_SIZE)
 BATCH_SIZE = 16
if DEV_MODE:
train_size, val_size = int(0.7 * DATASET_SIZE), int(0.3 * DATASET_SIZE)
 else:
train_size, val_size = DATASET_SIZE, 0
train_ds = image_label_ds.map(augment_image ).take(train_size ).batch(BATCH_SIZE ).repeat()
val_ds = image_label_ds.skip(train_size ).batch(BATCH_SIZE ).repeat()
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 train = pd.read_csv("/kaggle/input/titanic/train.csv" )<create_dataframe>
def get_model() :
model = Sequential()
model.add(Conv2D(32,(2, 2), input_shape=IMAGE_SHAPE, activation='relu'))
model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(4, 4), activation='relu'))
model.add(Conv2D(64,(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation="sigmoid"))
return model
model = get_model()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
 model.summary()
total = train.isnull().sum().sort_values(ascending=False)
 percent = round(train.isnull().sum() /train.isnull().count() *100, 1 ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', '%'])
 missing_data.head(5 )<feature_engineering>
train_steps = 2*(train_size//BATCH_SIZE)
 val_steps = val_size//BATCH_SIZE
if val_steps == 0:
val_steps = None
val_ds = None
history = model.fit(train_ds,
steps_per_epoch=train_steps,
epochs=50,
validation_data=val_ds,
validation_steps=val_steps,
callbacks=None )
train_ml = train.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1)
 train_ml["Embarked"] = train_ml["Embarked"].fillna("S")
 mean = train_ml["Age"].mean()
std = train_ml["Age"].std()
is_null = train_ml["Age"].isnull().sum()
rand_age = np.random.randint(mean - std, mean + std, size = is_null)
 age_slice = train_ml["Age"].copy()
age_slice[np.isnan(age_slice)] = rand_age
train_ml["Age"] = age_slice
train_ml.info()<data_type_conversions>
training_eval = tf.data.Dataset.zip(( image_ds, label_ds)).shuffle(DATASET_SIZE)
 training_eval = training_eval.take(DATASET_SIZE ).batch(DATASET_SIZE)
 model.evaluate(training_eval, steps=1 )
train_ml.loc[train_ml["Sex"] == "male", "Sex"] = 0
train_ml.loc[train_ml["Sex"] == "female", "Sex"] = 1
train_ml["Sex"] = train_ml["Sex"].astype(int)
 train_ml.loc[train_ml["Embarked"] == "S", "Embarked"] = 0
train_ml.loc[train_ml["Embarked"] == "C", "Embarked"] = 1
train_ml.loc[train_ml["Embarked"] == "Q", "Embarked"] = 2
train_ml["Embarked"] = train_ml["Embarked"].astype(int)
 train_ml.head()<feature_engineering>
test_paths = [path for path in sorted(pathlib.Path('.. /input/test/test/' ).glob('*.jpg')) ]
TEST_SIZE = len(test_paths)
 test_path_ds = tf.data.Dataset.from_tensor_slices([str(path)for path in test_paths])
 test_image_ds = test_path_ds.map(load_and_preprocess_image)
 test_image_ds = test_image_ds.take(TEST_SIZE ).batch(TEST_SIZE )
train_ml['family_members'] = train_ml.SibSp + train_ml.Parch<count_missing_values>
preds=model.predict(test_image_ds, steps = 1 )
<feature_engineering><EOS> 
test_df=pd.DataFrame({'id': [path.name for path in test_paths] })
 test_df['has_cactus']=preds
test_df.to_csv("submission.csv",index=False )