Kaggle competition: Aerial Cactus Identification
train_data['FareGroup'] = pd.cut(train_data['Fare'],3)
 print(train_data[['FareGroup', 'Survived']].groupby('FareGroup', as_index=False ).mean().sort_values('Survived', ascending=False))<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
import scipy
import cv2
import keras
def group_fare(fare):
if fare <= 170: return 0
if fare > 170 and fare <= 340: return 1
if fare > 340: return 2
for i, row in train_data.iterrows() :
train_data.at[i,'Fare Group'] = group_fare(row["Fare"])
 for i, row in test_data.iterrows() :
test_data.at[i,'Fare Group'] = group_fare(row["Fare"] )<drop_column>
import random
def calc_age(df, cl, sx, tl):
a = df.groupby(["Pclass", "Sex", "Titles"])["Age"].median()
return a[cl][sx][tl]
age_train = train_data.copy()
age_train.drop('PassengerId', axis=1, inplace=True)
 age_train.drop('Survived',axis=1, inplace=True)
 age_test = test_data.copy()
age_test.drop('PassengerId', axis=1, inplace=True)
 df = pd.concat([age_train, age_test], sort=False ).reset_index(drop=True)
 for i, row in train_data.iterrows() :
if pd.isna(row['Age']):
newage =(calc_age(df, row["Pclass"], row["Sex"], row["Titles"]))
train_data.at[i,'Age'] = newage
else: continue
for i, row in test_data.iterrows() :
if pd.isna(row['Age']):
newage =(calc_age(df, row["Pclass"], row["Sex"], row["Titles"]))
test_data.at[i,'Age'] = newage
else: continue<sort_values>
train_data = pd.read_csv('.. /input/train.csv' )
train_data['AgeGroup'] = pd.cut(train_data['Age'],5)
 print(train_data[['AgeGroup', 'Survived']].groupby('AgeGroup', as_index=False ).mean().sort_values('Survived', ascending=False))<feature_engineering>
train_data.has_cactus.value_counts()
train_data["Family"] = train_data["SibSp"] + train_data["Parch"]
test_data["Family"] = test_data["SibSp"] + test_data["Parch"]<feature_engineering>
positive_examples = train_data[train_data.has_cactus==1]
negative_examples = train_data[train_data.has_cactus==0]
train_data["Embarked"] = train_data["Embarked"].fillna('S' )<sort_values>
def augment_img(img):
batch_input = []
batch_input += [img]
batch_input += [img[::-1, :, :]]
batch_input += [img[:, ::-1, :]]
batch_input += [np.rot90(img)]
temp_img = np.zeros_like(img)
 temp_img[:28, :, :] = img[4:, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, :28, :] = img[:, 4:, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[4:, :, :] = img[:28, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, 4:, :] = img[:, :28, :]
batch_input += [temp_img]
batch_input += [cv2.resize(img[2:30, 2:30, :],(32, 32)) ]
batch_input += [scipy.ndimage.interpolation.rotate(img, 10, reshape=False)]
batch_input += [scipy.ndimage.interpolation.rotate(img, 5, reshape=False)]
return batch_input
print(train_data[['Embarked', 'Survived']].groupby('Embarked', as_index=False ).mean().sort_values('Survived', ascending=False))<feature_engineering>
def image_generator(batch_size = 8, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
indexes = positive_examples.index.tolist()
neg_indexes = negative_examples.index.tolist()
if shuffle:
np.random.shuffle(indexes)
 np.random.shuffle(neg_indexes)
 N = int(len(indexes)/(batch_size/2))
neg_N = int(len(neg_indexes)/(batch_size/2))
j = 0
for i in range(N):
current_indexes = indexes[i*(batch_size//2):(i+1)*(batch_size//2)]
current_neg_indexes = neg_indexes[j*(batch_size//2):(j+1)*(batch_size//2)]
j =(j + 1)% neg_N
batch_input = []
batch_output = []
for ind in range(len(current_indexes)) :
index = current_indexes[ind]
neg_index = current_neg_indexes[ind]
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input.extend(augment_img(img))
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
neg_img = mpimg.imread('.. /input/train/train/' + train_data.id[neg_index])
 batch_input.extend(augment_img(neg_img))
for _ in range(11):
batch_output += [train_data.has_cactus[neg_index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))
def embarked_rate(embarked_port):
if embarked_port == 'C': return 2
if embarked_port == 'Q': return 1
if embarked_port == 'S': return 0
for i, row in train_data.iterrows() :
train_data.at[i,'Emb Rate'] = embarked_rate(row["Embarked"])
 for i, row in test_data.iterrows() :
test_data.at[i,'Emb Rate'] = embarked_rate(row["Embarked"] )<categorify>
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,(5, 5), input_shape=(32, 32, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(64,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(512,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(100))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Dense(1, activation='sigmoid'))
sex_mapping = {"male": 0, "female": 1}
train_data['Sex Map'] = train_data['Sex'].map(sex_mapping)
 test_data['Sex Map'] = test_data['Sex'].map(sex_mapping )<prepare_x_and_y>
opt = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )
cols_to_drop = ["SibSp", "Parch", "Name", "Age", "Fare", "Embarked", "Cabin", "Ticket", "Sex", "Titles"]
new_train = train_data.drop(cols_to_drop, axis=1)
 new_test = test_data.drop(cols_to_drop, axis=1)
 y = train_data["Survived"]
features = ["Pclass", "Sex Map", "Family", "Title Map", "Age Group", "Fare Group", "Emb Rate"]
X = pd.get_dummies(new_train[features])
 X_test = pd.get_dummies(new_test[features])
<train_model>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
model1 = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model1.fit(X, y)
 y1_test = model1.predict(X_test)
 model2 = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.05)
 model2.fit(X, y)
 y2_test = model2.predict(X_test)
 model3 = SVC(random_state=1)
 model3.fit(X,y)
 y3_test = model3.predict(X_test)
 model4 = GradientBoostingClassifier(random_state=42)
 model4.fit(X, y)
 y4_test = model4.predict(X_test )<compute_train_metric>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[::-1, :, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
model1_preds = cross_val_predict(model1, X, y, cv=10)
 model1_acc = accuracy_score(y, model1_preds)
 model2_preds = cross_val_predict(model2, X, y, cv=10)
 model2_acc = accuracy_score(y, model2_preds)
 model3_preds = cross_val_predict(model3, X, y, cv=10)
 model3_acc = accuracy_score(y, model3_preds)
 model4_preds = cross_val_predict(model4, X, y, cv=10)
 model4_acc = accuracy_score(y, model4_preds)
 print("Random Forest Accuracy:", model1_acc)
 print("XGBoost Accuracy:", model2_acc)
 print("SVC Accuracy:", model3_acc)
 print("GB Accuracy:", model4_acc )<save_to_csv>
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[:, ::-1, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y2_test})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
test_files = os.listdir('.. /input/test/test/' )
%matplotlib inline
InteractiveShell.ast_node_interactivity = "all"
logging.getLogger('tensorflow' ).setLevel(logging.ERROR)
 tf.compat.v1.set_random_seed(0 )<load_from_csv>
batch = 40
all_out = []
for i in range(int(4000/batch)) :
images = []
for j in range(batch):
img = mpimg.imread('.. /input/test/test/'+test_files[i*batch + j])
 images += [img]
out = model.predict(np.array(images))
all_out += [out]
train = pd.read_csv(r'.. /input/titanic/train.csv')
 test = pd.read_csv(r'.. /input/titanic/test.csv' )<compute_test_metric>
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )
def calculate_error_types(prediction_array, true_array):
type1_errors = pd.Series(( true_array==0)&(true_array!=prediction_array))
type2_errors = pd.Series(( true_array==1)&(true_array!=prediction_array))
num_type1 = len(type1_errors.loc[type1_errors==True])
 num_type2 = len(type2_errors.loc[type2_errors==True])
 pct_type1 = num_type1 / len(type1_errors)
 pct_type2 = num_type2 / len(type2_errors)
 pct_errors =(num_type1 + num_type2)/ len(type1_errors)
 return num_type1, pct_type1, num_type2, pct_type2, pct_errors 
<compute_train_metric>
sub_file.to_csv('sample_submission.csv', index=False )