Kaggle competition: Aerial Cactus Identification
trn = train.copy()
tst = test.copy()<prepare_x_and_y>
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
y = train['SalePrice']
train.drop('SalePrice',inplace=True,axis=1)
 len_train = len(train)
 full_data = pd.concat([train,test],axis=0 ).reset_index(drop=True)
 full_data = pd.get_dummies(full_data)
 train = full_data[:len_train]
test = full_data[len_train:]<count_values>
import random
overfit = []
for col in train.columns:
counts = train[col].value_counts()
zeros = counts.iloc[0]
if zeros / len(train)* 100 >99:
overfit.append(col)
 print(len(overfit))<train_model>
train_data = pd.read_csv('.. /input/train.csv' )
train.drop(overfit,axis=1,inplace=True)
 test.drop(overfit,axis=1,inplace=True )<prepare_x_and_y>
train_data.has_cactus.value_counts()
X = train<train_model>
def image_generator(batch_size = 16, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
if all_data:
indexes = np.arange(train_data.shape[0])
 else:
indexes = np.arange(train_data[:15000].shape[0])
 if shuffle:
np.random.shuffle(indexes)
 else:
indexes = np.arange(train_data[15000:].shape[0])
 N = int(len(indexes)/ batch_size)
 for i in range(N):
current_indexes = indexes[i*batch_size:(i+1)*batch_size]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 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)]
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))
X_Train,X_Test,y_Train,y_Test = train_test_split(X,y,test_size=0.25)
 print("X_Train Shape:",X_Train.shape)
 print("X_Test Shape:",X_Test.shape)
 print("y_Train Shape:",y_Train.shape)
 print("y_Test Shape:",y_Test.shape )<train_on_grid>
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'))
lr = LinearRegression()
lr.fit(X_Train,y_Train)
 print("Linear Regression, Training Set RMSE:",rmse(y_Train,lr.predict(X_Train)))
 print("Linear Regression,Training R Squared:",calc_r2(lr,y_Train,X_Train))
print("
Linear Regression,Testing Set RMSE:",rmse(y_Test,lr.predict(X_Test)))
 print("Linear Regression,Testing R Squared:",calc_r2(lr,y_Test,X_Test))
print("
Linear Regression,Cross Validation Score:",cv_score(lr))<train_on_grid>
opt = keras.optimizers.Adam(0.0001)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )
lasso = Lasso(alpha=0.001,max_iter=5000)
 lasso.fit(X_Train,y_Train)
 print("Lasso Regression, Training Set RMSE:",rmse(y_Train,lasso.predict(X_Train)))
 print("Lasso Regression,Training R Squared:",calc_r2(lasso,y_Train,X_Train))
print("
Lasso Regression,Testing Set RMSE:",rmse(y_Test,lasso.predict(X_Test)))
 print("Lasso Regression,Testing R Squared:",calc_r2(lasso,y_Test,X_Test))
print("
Lasso Regression,Cross Validation Score:",cv_score(lasso))<create_dataframe>
model.fit_generator(image_generator() , steps_per_epoch= train_data.shape[0] / 16, epochs=30 )
coeffs = pd.DataFrame(list(zip(X.columns, lasso.coef_)) , columns=['Predictors', 'Coefficients'])
 coeffs.sort_values(by='Coefficients',ascending=False )<find_best_params>
keras.backend.eval(model.optimizer.lr.assign(0.00001))
scores = []
alpha = [0.001,0.005,0.01,0.05,0.1,0.5,1,2,5,10]
for i in alpha:
las = Lasso(alpha=i,max_iter=10000)
 las.fit(X_Train,y_Train)
 scores.append(rmse(y_Test,las.predict(X_Test)))
 print("Lasso Scores with Different Alpha Values
",scores )<train_on_grid>
model.fit_generator(image_generator() , steps_per_epoch= train_data.shape[0] / 16, epochs=15 )
ridge = Ridge()
ridge.fit(X_Train,y_Train)
 print("Ridge Regression,Training Set RMSE:",rmse(y_Train,ridge.predict(X_Train)))
 print("Ridge Regression,Training R Squared:",calc_r2(ridge,y_Train,X_Train))
print("
Ridge Regression,Testing Set RMSE:",rmse(y_Test,ridge.predict(X_Test)))
 print("Ridge Regression,Testing R Squared:",calc_r2(ridge,y_Test,X_Test))
print("
Ridge Regression,Cross Validation Score:",cv_score(ridge))<find_best_params>
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 )
scores = []
alpha = [0.001,0.005,0.01,0.05,0.1,0.5,1,2,5,10]
for i in alpha:
ridge = Ridge(alpha=i)
 ridge.fit(X_Train,y_Train)
 scores.append(rmse(y_Test,ridge.predict(X_Test)))
 print("Ridge Scores with Different Alpha Values
",scores )<train_on_grid>
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 )
en = ElasticNet(max_iter=5000)
 params = {"alpha":[0.0001,0.0002,0.001,0.005,0.01,0.05,0.1,0.5,1,2,5,10],
"l1_ratio":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]}
grid = GridSearchCV(estimator=en,param_grid=params,cv=5,n_jobs=-1,scoring='neg_mean_squared_error')
 grid.fit(X,y )<find_best_params>
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 )
grid.best_estimator_,grid.best_params_,grid.best_score_<train_on_grid>
test_files = os.listdir('.. /input/test/test/' )
en = grid.best_estimator_
en.fit(X_Train,y_Train)
 print("Elastic Net Regression,Training Set RMSE:",rmse(y_Train,en.predict(X_Train)))
 print("Elastic Net Regression,Training R Squared:",calc_r2(en,y_Train,X_Train))
print("
Elastic Net Regression,Testing Set RMSE:",rmse(y_Test,en.predict(X_Test)))
 print("Elastic Net Regression,Testing R Squared:",calc_r2(en,y_Test,X_Test))
print("
Elastic Net Regression,Cross Validation Score:",cv_score(en))<save_to_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]
en_submission = np.expm1(en.predict(test))
lasso_submission = np.expm1(lasso.predict(test))
ridge_submission = np.expm1(ridge.predict(test))
average =(en_submission+lasso_submission+ridge_submission)/3
Submission['SalePrice'] = average
Submission.to_csv("Stacked7.csv",index=None )<load_from_csv>
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )
<define_variables><EOS> 
sub_file.to_csv('sample_submission.csv', index=False )