Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<compute_test_metric>
warnings.filterwarnings("ignore" )
cvres = grid_search_svm.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params )<find_best_params>
train = pd.read_csv('.. //input//train.csv')
 print(train.shape)
 print(train.head(10))
grid_search_svm.best_estimator_<find_best_params>
train.has_cactus.value_counts()
feature_importances = grid_search_forest.best_estimator_.feature_importances_<create_dataframe>
def create_training_model_data(dataframe, batch_size=32, mode='categorical'):
dataframe['has_cactus'] = dataframe['has_cactus'].astype(str)
 IDG = ImageDataGenerator(rescale=1./255.,
horizontal_flip=True,
vertical_flip=True)
 train_data = IDG.flow_from_dataframe(dataframe=dataframe[:15925],
directory='.. //input//train//train',
x_col='id',
y_col='has_cactus',
class_mode='binary',
batch_size=batch_size,
color_mode='grayscale',
target_size=(32,32))
validation_data = IDG.flow_from_dataframe(dataframe=dataframe[15925:],
directory='.. //input//train//train//',
x_col='id',
y_col='has_cactus',
class_mode='binary',
batch_size=batch_size,
color_mode='grayscale',
target_size=(32,32))
return train_data, validation_data
indices = np.argsort(feature_importances)[::-1]
ranking = pd.DataFrame({'features':[attributes[indices[f]] for f in range(len(attributes[:10])) ],
'values': [feature_importances[indices[f]] for f in range(len(attributes[:10])) ]})
 ranking<find_best_params>
train_data, validation_data = create_training_model_data(train )
final_model = grid_search_svm.best_estimator_
final_model<create_dataframe>
model_alexnet = Sequential()
model_alexnet.add(Conv2D(32,(3,3),
input_shape=(32, 32, 1),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(64,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(2,2), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(64,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(128,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Conv2D(128,(3,3),
padding='same',
activation='relu'))
model_alexnet.add(MaxPooling2D(pool_size=(3,3), padding='same'))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Flatten())
 model_alexnet.add(Dense(128,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.10))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(256,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.20))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(512,
activation='relu', kernel_initializer='glorot_uniform'))
model_alexnet.add(Dropout(0.2))
model_alexnet.add(BatchNormalization())
 model_alexnet.add(Dense(1, activation='sigmoid'))
model_alexnet.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
 callbacks = [EarlyStopping(monitor='val_loss', patience=20),
ModelCheckpoint(filepath='best_model_alexnet.h5', monitor='val_loss', save_best_only=True)]
model_alexnet.summary()
X_test=test_data.copy()<categorify>
history = model_alexnet.fit_generator(train_data,
epochs=100,
steps_per_epoch=(15925/32),
callbacks=callbacks,
validation_data = validation_data,
validation_steps=(1575/32),
class_weight=class_weights,
verbose=2 )
X_test_prepared = preprocess_pipeline.transform(X_test )<predict_on_test>
test_IDG = ImageDataGenerator(rescale=1./255.)
test_data = test_IDG.flow_from_directory(
 directory='.. //input//test//',
target_size=(32,32),
color_mode='grayscale',
class_mode='binary',
batch_size=1,
shuffle=False )
y_pred = final_model.predict(X_test_prepared)
 print(y_pred.shape)
 y_pred.ravel()<feature_engineering>
y_pred = model_alexnet.predict_generator(test_data,steps=4000)
 y_pred = np.hstack(y_pred ).tolist()
test_data['SalePrice'] = np.exp(y_pred.ravel())
 test_data[['Id','SalePrice']].head()<save_to_csv>
has_cactus = [0 if proba<0.50 else 1 for proba in y_pred]
print(Counter(has_cactus ).keys())
 print(Counter(has_cactus ).values() )
test_data[['Id','SalePrice']].to_csv('submission.csv', index=False )<set_options>
files=[]
files = [f for f in sorted(os.listdir('.. //input//test//test')) ]
%matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 print("Python version: {}".format(sys.version))
print("pandas version: {}".format(pd.__version__))
print("matplotlib version: {}".format(matplotlib.__version__))
print("NumPy version: {}".format(np.__version__))
print("SciPy version: {}".format(sp.__version__))
print("IPython version: {}".format(IPython.__version__))
print("scikit-learn version: {}".format(sklearn.__version__))
print('-'*25)
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
RANDOM_SEED = 19901129
np.random.seed(RANDOM_SEED )<load_from_csv>
submission = pd.DataFrame({'id':files,
'has_cactus':y_pred} )
train = pd.read_csv('.. /input/train.csv', header = 0, dtype={'Age': np.float64})
 test = pd.read_csv('.. /input/test.csv' , header = 0, dtype={'Age': np.float64})
 full_data = [train, test]
print(train.info() )<feature_engineering>
submission.to_csv('submission.csv', index=False )
train['Name_length'] = train['Name'].apply(len)
 test['Name_length'] = test['Name'].apply(len)
 train['Has_Cabin'] = train["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 test['Has_Cabin'] = test["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 for dataset in full_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for dataset in full_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S')
 for dataset in full_data:
dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median())
 train['CategoricalFare'] = pd.qcut(train['Fare'], 4)
 for dataset in full_data:
age_avg = dataset['Age'].mean()
age_std = dataset['Age'].std()
age_null_count = dataset['Age'].isnull().sum()
age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 train['CategoricalAge'] = pd.cut(train['Age'], 5)
 def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in full_data:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in full_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 for dataset in full_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] 						 = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] 							 = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 dataset.loc[ dataset['Age'] <= 16, 'Age'] 					 = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
dataset['Age*Class'] = dataset['Age'] * dataset['Pclass']<drop_column>
submission.to_csv('submission.csv', index=False )