Kaggle competition: Aerial Cactus Identification
submission = pd.DataFrame({'Id': df_test.Id, 'SalePrice': np.exp(y_pred)})
 submission.to_csv('submissionEM.csv',index=False)
 submission.head()<set_options>
import os
import time
import pandas as pd
import numpy as np
import cv2
from tqdm import tqdm
import matplotlib.pyplot as plt
from keras.models import Sequential,load_model
from keras.layers import Dense,Conv2D,Dropout,MaxPooling2D,Flatten,BatchNormalization
from keras.callbacks import EarlyStopping
from keras import optimizers
from sklearn.metrics import confusion_matrix
import seaborn as sns
sns.set(style="darkgrid")
 np.random.seed(42)
 %matplotlib inline
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12<load_from_csv>
train_path='.. /input/train/train'
test_path='.. /input/test/test'
def load_housing_data(filename, house_path):
csv_path = os.path.join(house_path, filename)
 return pd.read_csv(csv_path )<load_from_csv>
label_train=pd.read_csv(".. /input/train.csv")
 label_train=label_train.sort_values(by=['id'])
 id=label_train['id'].values
l=label_train['has_cactus'].values
train=[]
X=[]
Y=[]
a=0
for i in tqdm(sorted(os.listdir(train_path))):
path=os.path.join(train_path,i)
 i=cv2.imread(path,cv2.IMREAD_COLOR)
 X.append(i)
 train.append([np.array(i),l[a]])
 a=a+1
train=np.array(train)
 Y=train[:,1]
train=train[:,0]
X=np.array(X)
 X.shape
X=X/255
train=train/255
train_data = load_housing_data('train.csv',".. /input")
 test_data = load_housing_data('test.csv','.. /input' )<create_dataframe>
test_viz=[]
X_test=[]
for i in tqdm(os.listdir(test_path)) :
id=i
path=os.path.join(test_path,i)
 i=cv2.imread(path,cv2.IMREAD_COLOR)
 X_test.append(i)
 test_viz.append([np.array(i),id])
 X_test=np.array(X_test)
 X_test.shape
test_viz=np.array(test_viz)
 id_test=test_viz[:,1]
test_viz=test_viz[:,0]
test_viz.shape
X_test=X_test/255
test_viz=test_viz/255
housing = train_data.copy()<choose_model_class>
m=Sequential()
m.add(Conv2D(filters=32,kernel_size=2,padding="same",activation="relu",input_shape=(32,32,3)))
 m.add(Conv2D(filters=32,kernel_size=2,padding="same",activation="relu"))
m.add(Conv2D(filters=32,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=1))
m.add(Dropout(0.2))
m.add(Conv2D(filters=64,kernel_size=2,padding="same",activation="relu"))
m.add(Conv2D(filters=64,kernel_size=2,padding="same",activation="relu"))
m.add(Conv2D(filters=64,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=1))
m.add(Dropout(0.2))
m.add(Conv2D(filters=128,kernel_size=2,padding="same",activation="relu"))
m.add(Conv2D(filters=128,kernel_size=2,padding="same",activation="relu"))
m.add(Conv2D(filters=128,kernel_size=4,padding="same",activation="relu"))
m.add(MaxPooling2D(pool_size=2,strides=1))
m.add(Dropout(0.2))
m.add(Flatten())
 m.add(Dense(32,activation="relu"))
m.add(Dense(1,activation="sigmoid"))
m.summary()
class ReduceVIF(BaseEstimator, TransformerMixin):
def __init__(self, thresh=5.0, impute=True, impute_strategy='median'):
self.thresh = thresh
if impute:
self.imputer = SimpleImputer(strategy=impute_strategy)
 def fit(self, X, y=None):
print('ReduceVIF fit')
 print(self.imputer)
 if hasattr(self, 'imputer'):
self.imputer.fit(X)
 return self
def transform(self, X, y=None):
print('ReduceVIF transform')
 columns = X.columns.tolist()
if hasattr(self, 'imputer'):
X = pd.DataFrame(self.imputer.transform(X), columns=columns)
 return ReduceVIF.calculate_vif(X, self.thresh)
 @staticmethod
def calculate_vif(X, thresh=5.0):
dropped=True
while dropped:
variables = X.columns
dropped = False
vif = [variance_inflation_factor(X[variables].values, X.columns.get_loc(var)) for var in X.columns]
max_vif = max(vif)
 if max_vif > thresh:
maxloc = vif.index(max_vif)
 print(f'Dropping {X.columns[maxloc]} with vif={max_vif}')
 X = X.drop([X.columns.tolist() [maxloc]], axis=1)
 dropped=True
print(X.columns)
 return X<feature_engineering>
m.compile(loss="binary_crossentropy",optimizer='adam',metrics=["accuracy"])
 s=time.time()
h=m.fit(X,Y,batch_size=128,validation_split=0.2,epochs=100)
 e=time.time()
t=e-s
print("Addestramento completato in %d minuti e %d secondi" %(t/60,t*60))
transformer = ReduceVIF(thresh=15)
 num_attributes = list(transformer.fit_transform(housing.select_dtypes(include=['int64','float64'])).columns.values )<sort_values>
acc=h.history['acc']
val_acc=h.history['val_acc']
loss=h.history['loss']
val_loss=h.history['val_loss']
<filter><EOS> 
pred=m.predict(X_test)
 ids=[]
label=[]
a=0
for i in tqdm(os.listdir(test_path)) :
id=i
ids.append(id)
 label.append(pred[a])
 a=a+1
label=np.array(label,dtype='float64')
 out=pd.DataFrame({'id': ids,'has_cactus':label[:,0]})
 out.to_csv('cactus_identifier_net.csv',index=False,header=True )