Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<groupby>
%matplotlib inline
print(os.listdir(".. /input/"))
meanAges = train_data.groupby(['Pclass'])['Age'].mean()
print(meanAges )<feature_engineering>
def loadImagesData(glob_path):
images = []
names = []
for img_path in glob.glob(glob_path):
names.append(os.path.basename(img_path))
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
 images.append(img)
 return(images,names)
 trainData = {}
namesData = {}
for label in os.listdir('.. /input/train/'): 
(images,names)= loadImagesData(f".. /input/train/{label}/*.jpg")
 print(f".. /input/train/{label}/*.jpg")
 trainData[label] = images
namesData[label] = names
print("train labels:", ",".join(trainData.keys()))
print(len(trainData['train']))
plt.figure(figsize=(4,2))
columns = 4
for i in range(0,8):
plt.subplot(8 / columns + 1, columns, i + 1)
 plt.imshow(trainData['train'][i])
 plt.show()
train_data['newGender'] = train_data['Sex']
train_data.loc[train_data['Age']<16., 'newGender'] = 'child'
train_data.drop(columns='Sex', inplace=True)
 print(train_data.head(8))<categorify>
train_meta = pd.read_csv('.. /input/train.csv')
 print(train_meta.shape)
 print(train_meta.has_cactus.value_counts())
 lookupY = {}
for i in range(0,len(train_meta)) :
row = train_meta.iloc[i,:]
lookupY[row.id] = row.has_cactus
train_meta.head()
train_data['Title'] = train_data['Name']
for name_string in train_data['Name']:
train_data['Title'] = train_data['Name'].str.extract('([A-Za-z]+)\.', expand=True)
 mapping = {'Mlle': 'Miss', 'Major': 'Rare', 'Col': 'Rare', 'Sir': 'Rare', 'Don': 'Rare', 'Mme': 'Miss',
'Jonkheer': 'Rare', 'Lady': 'Rare', 'Capt': 'Rare', 'Countess': 'Rare', 'Ms': 'Miss', 'Dona': 'Mrs', 'Rev': 'Rare'}
train_data.replace({'Title': mapping}, inplace=True)
 train_data.drop(columns='Name', inplace=True )<feature_engineering>
trainList = []
maxCount = 4364
counts = {'0':0,'1':0}
for(i,image)in enumerate(trainData['train']):
label = lookupY[namesData['train'][i]]
counts[str(label)] = 1 + counts[str(label)]
if counts[str(label)] < maxCount:
trainList.append({
'label': label,
'data': image
})
 random.shuffle(trainList)
 train_df = pd.DataFrame(trainList)
 gc.collect()
print(train_df.shape)
 print(train_df.label.value_counts())
 train_df.head()
train_data['familyNumber'] = train_data['SibSp'] + train_data['Parch']
print(train_data.head(8))
Outliers_to_drop = detect_outliers(train_data,1,["Age","familyNumber","Fare"])
 train_data = train_data.drop(Outliers_to_drop, axis = 0 ).reset_index(drop=True)
 train_data['familySize'] = np.nan
train_data.loc[train_data['familyNumber']==0, 'familySize'] = 'alone'
train_data.loc[train_data['familyNumber']==1, 'familySize'] = 'couple'
train_data.loc[train_data['familyNumber']==2, 'familySize'] = 'small'
train_data.loc[train_data['familyNumber']==3, 'familySize'] = 'medium'
train_data.loc[train_data['familyNumber']>=4, 'familySize'] = 'large'
train_data.drop(columns=['familyNumber', 'SibSp', 'Parch'], inplace=True )<categorify>
data_stack = np.stack(train_df['data'].values)
 dfloats = data_stack.astype(np.float)
 all_x = np.multiply(dfloats, 1.0 / 255.0)
 print(all_x.shape)
 print(type(all_x))
all_x[0,0,0,0]
train_data['newPclass'] = np.nan
train_data.loc[train_data['Pclass']==1, 'newPclass'] = 'first'
train_data.loc[train_data['Pclass']==2, 'newPclass'] = 'second'
train_data.loc[train_data['Pclass']==3, 'newPclass'] = 'third'
train_data.drop(columns='Pclass', inplace=True)
 columns = ['newGender', 'Embarked', 'Title', 'familySize', 'newPclass']
one_hot = pd.get_dummies(train_data.loc[:, columns], drop_first=True)
 train_data.drop(columns=columns, inplace=True)
 train_data = train_data.join(one_hot)
 print(train_data.head(2))
print(train_data.columns.values )<import_modules>
all_y = np.array(train_df.label ).astype(np.float)
 all_y[0:5]
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier<prepare_x_and_y>
train_x,test_x,train_y,test_y=train_test_split(all_x,all_y,test_size=0.2,random_state=7)
 print(train_x.shape,test_x.shape )
trainScaler = StandardScaler()
trainScaler.fit(train_data.drop(['Survived', 'PassengerId'], axis = 1))
X_train_scaled = trainScaler.transform(train_data.drop(['Survived', 'PassengerId'], axis = 1))
X_train = train_data.drop(['Survived', 'PassengerId'], axis = 1)
 y_train = train_data['Survived']<train_on_grid>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
rotation_range=60,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True)
 datagen.fit(train_x )
grid_params = {
'n_neighbors': [3, 5, 7, 9, 11, 13],
'weights': ['uniform', 'distance'],
'metric':['euclidean', 'manhattan']
}
KNN_CV = GridSearchCV(estimator = KNeighborsClassifier() , param_grid=grid_params, cv = 3)
 KNN_CV.fit(X_train_scaled, y_train)
 print(KNN_CV.best_score_)
 weights = KNN_CV.best_params_.get('weights')
 n = KNN_CV.best_params_.get('n_neighbors')
 metric = KNN_CV.best_params_.get('metric')
 bestKNN = KNeighborsClassifier(n_neighbors=n, weights=weights, metric=metric)
 bestKNN.fit(X_train_scaled, y_train )<train_on_grid>
num_filters = 8
input_shape = train_x.shape[1:]
output_shape = 1
m = Sequential()
def tdsNet(m):
m.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=input_shape))
m.add(Conv2D(16, kernel_size=3, activation='relu'))
m.add(Flatten())
 m.add(Dropout(0.5))
m.add(Dense(units = output_shape, activation='sigmoid'))
tdsNet(m)
 m.compile(optimizer = 'nadam',
loss = 'binary_crossentropy',
metrics = ['accuracy'])
 m.summary()
param_grid = {
'criterion' : ['gini'],
'n_estimators': [70, 80, 90, 100, 110, 120],
'max_features': ['auto', 'log2'],
'max_depth' : [5, 7, 9, 11, 13]
}
randomForest_CV = GridSearchCV(estimator = RandomForestClassifier() , param_grid = param_grid, cv = 3)
 randomForest_CV.fit(X_train, y_train)
 print(randomForest_CV.best_score_)
 criterion = randomForest_CV.best_params_.get('criterion')
 nRF = randomForest_CV.best_params_.get('n_estimators')
 max_features = randomForest_CV.best_params_.get('max_features')
 max_depth = randomForest_CV.best_params_.get('max_depth')
 bestRF = RandomForestClassifier(n_estimators=1000, max_depth=max_depth, max_features=max_features, criterion=criterion)
 bestRF.fit(X_train, y_train )<data_type_conversions>
batch_size = 32
history = m.fit_generator(datagen.flow(train_x, train_y,
batch_size=batch_size),
steps_per_epoch=(train_x.shape[0] // batch_size),
epochs = 4,
validation_data=(test_x, test_y),
workers=4 )
for i in range(3):
test_data.loc[test_data['Pclass']==int(i+1), 'Age'] = test_data.loc[test_data['Pclass']==int(i+1), 'Age'].fillna(meanAges.values[i])
 meanFare = test_data['Fare'].median()
test_data['Fare'].fillna(meanFare, inplace=True )<feature_engineering>
num_filters = 8
input_shape = train_x.shape[1:]
output_shape = 1
m = Sequential()
def cnnNet(m):
m.add(Conv2D(32, kernel_size=3, input_shape=input_shape))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(Conv2D(32, kernel_size=3))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(Dropout(0.25))
m.add(Conv2D(64, kernel_size=3))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(Dropout(0.25))
m.add(Conv2D(64, kernel_size=3))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(MaxPooling2D(2,2))
m.add(Dropout(0.25))
m.add(Conv2D(128, kernel_size=3))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(Dropout(0.25))
m.add(Conv2D(128, kernel_size=3))
m.add(BatchNormalization())
 m.add(Activation("relu"))
m.add(MaxPooling2D(2,2))
m.add(Dropout(0.25))
m.add(Flatten())
 m.add(Dense(64, activation='relu'))
m.add(BatchNormalization())
 m.add(Dropout(0.5))
m.add(Dense(units = output_shape, activation='sigmoid'))

 cnnNet(m)
 m.compile(optimizer = 'nadam',
loss = 'binary_crossentropy',
metrics = ['accuracy'])
 m.summary()
test_data['newGender'] = test_data['Sex']
test_data.loc[test_data['Age']<18., 'newGender'] = 'child'
test_data.drop(columns='Sex', inplace=True)
 test_data['Title'] = test_data['Name']
for name_string in test_data['Name']:
test_data['Title'] = test_data['Name'].str.extract('([A-Za-z]+)\.', expand=True)
 mapping = {'Mlle': 'Miss', 'Major': 'Rare', 'Col': 'Rare', 'Sir': 'Rare', 'Don': 'Rare', 'Mme': 'Miss',
'Jonkheer': 'Rare', 'Lady': 'Rare', 'Capt': 'Rare', 'Countess': 'Rare', 'Ms': 'Miss', 'Dona': 'Mrs', 'Rev': 'Rare'}
test_data.replace({'Title': mapping}, inplace=True)
 test_data.drop(columns='Name', inplace=True)
 test_data['familyNumber'] = test_data['SibSp'] + test_data['Parch']
test_data['familySize'] = np.nan
test_data.loc[test_data['familyNumber']==0, 'familySize'] = 'alone'
test_data.loc[test_data['familyNumber']==1, 'familySize'] = 'couple'
test_data.loc[test_data['familyNumber']==2, 'familySize'] = 'small'
test_data.loc[test_data['familyNumber']==3, 'familySize'] = 'medium'
test_data.loc[test_data['familyNumber']>=4, 'familySize'] = 'large'
test_data.drop(columns=['familyNumber', 'SibSp', 'Parch'], inplace=True)
 test_data['newPclass'] = np.nan
test_data.loc[test_data['Pclass']==1, 'newPclass'] = 'first'
test_data.loc[test_data['Pclass']==2, 'newPclass'] = 'second'
test_data.loc[test_data['Pclass']==3, 'newPclass'] = 'third'
test_data.drop(columns='Pclass', inplace=True )<categorify>
batch_size = 64
history = m.fit_generator(datagen.flow(train_x, train_y,
batch_size=batch_size),
steps_per_epoch=(train_x.shape[0] // batch_size),
epochs = 95,
validation_data=(test_x, test_y),
workers=4 )
test_data.drop(columns=['Cabin', 'Ticket'], inplace=True)
 one_hot = pd.get_dummies(test_data.loc[:, columns], drop_first=True)
 test_data.drop(columns=columns, inplace=True)
 test_data = test_data.join(one_hot)
 print("NaNs in each testing Feature")
 print(test_data.isnull().sum() )<save_to_csv>
trainList = []
for(i,image)in enumerate(trainData['train']):
label = lookupY[namesData['train'][i]]
trainList.append({
'label': label,
'data': image
})
 random.shuffle(trainList)
 train_df = pd.DataFrame(trainList)
 gc.collect()
data_stack = np.stack(train_df['data'].values)
 dfloats = data_stack.astype(np.float)
 all_x = np.multiply(dfloats, 1.0 / 255.0)
 all_x.shape
all_y = np.array(train_df.label ).astype(np.float)
 train_x,test_x,train_y,test_y=train_test_split(all_x,all_y,test_size=0.2,random_state=7)
 print(train_x.shape,test_x.shape )
X_test_scaled = trainScaler.transform(test_data.drop(['PassengerId'], axis = 1))
X_test = test_data.drop(columns='PassengerId')
 testPredictionsKNN = bestKNN.predict(X_test_scaled)
 testPredictionsRF = bestRF.predict(X_test)
 outputKNN = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': testPredictionsKNN})
 outputKNN.to_csv('my_submissionKNN.csv', index=False)
 outputRF = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': testPredictionsRF})
 outputRF.to_csv('my_submissionRF.csv', index=False )<train_on_grid>
batch_size = 128
history = m.fit_generator(datagen.flow(train_x, train_y,
batch_size=batch_size),
steps_per_epoch=(train_x.shape[0] // batch_size),
epochs = 95,
validation_data=(test_x, test_y),
workers=4 )
param_grid = {
'learning_rate' : [0.1, 0.2],
'n_estimators': [90, 100, 110],
'loss': ['deviance', 'exponential']
}
gradBoost_CV = GridSearchCV(estimator = GradientBoostingClassifier() , param_grid = param_grid, cv = 3)
 gradBoost_CV.fit(X_train, y_train)
 print(gradBoost_CV.best_score_)
 learning = gradBoost_CV.best_params_.get('learning_rate')
 nGB = gradBoost_CV.best_params_.get('n_estimators')
 loss = gradBoost_CV.best_params_.get('loss')
 bestGB = GradientBoostingClassifier(learning_rate=learning, n_estimators=nGB, loss=loss)
 bestGB.fit(X_train, y_train)
 testPredictionsGB = bestGB.predict(X_test)
 outputGB = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': testPredictionsGB})
 outputGB.to_csv('my_submissionGB.csv', index=False )<train_on_grid>
( test_images, test_names)= loadImagesData(f".. /input/test/test/*.jpg")
 data_stack = np.stack(test_images)
 dfloats = data_stack.astype(np.float32)
 unknown_x = np.multiply(dfloats, 1.0 / 255.0)
 predicted = np.ravel(m.predict(unknown_x))
submission_df = pd.DataFrame({'id':test_names,'has_cactus':predicted})
 submission_df.to_csv('submission.csv', index=False)
 len(submission_df )