Kaggle competition: Aerial Cactus Identification
final['Ticket'] = final['Ticket'].astype(str)
 final['Ticket_length'] = final.Ticket.apply(len)
 final['Ticket_length'].astype(int)
 final['Ticket_length'].unique()<feature_engineering>
base_dir = ".. /input/"
train_dir = os.path.join(base_dir,"train/train")
 test_dir = os.path.join(base_dir, "test/test")
 print("Training images :
{}".format(os.listdir(train_dir)[:10]), end='
')
 print("Testing images :
{}".format(os.listdir(test_dir)[:10]))
testing_dir = os.path.join(base_dir, "test" )
final['Ticket_length'] = np.where(((final.Ticket_length == 3)|(final.Ticket_length == 4)|(final.Ticket_length == 5)) ,4,final.Ticket_length)
 final['Ticket_length'] = np.where(((final.Ticket_length == 6)) ,5,final.Ticket_length)
 final['Ticket_length'] = np.where(((final.Ticket_length == 7)|(final.Ticket_length == 8)|(final.Ticket_length == 9)|(final.Ticket_length == 10)|(final.Ticket_length == 13)
 |(final.Ticket_length == 17)|(final.Ticket_length == 16)|(final.Ticket_length == 13)|(final.Ticket_length == 12)|(final.Ticket_length == 15)
 |(final.Ticket_length == 11)|(final.Ticket_length == 18)) ,12,final.Ticket_length)
<count_values>
train_dataframe = pd.read_csv(".. /input/train.csv")
 train_dataframe["has_cactus"] = np.where(train_dataframe["has_cactus"] == 1, "yes", "no")
 print(train_dataframe.head() )
final['Ticket_length'].value_counts()<feature_engineering>
import keras
from keras.models import Sequential
from keras.layers import *
from keras.preprocessing.image import ImageDataGenerator
from keras import applications
from efficientnet import EfficientNetB3
final['Ticket_length'] = final['Ticket_length'].astype(str)
 final['Ticket_length'] = np.where(((final.Ticket_length == '4')) ,'Below 6',final.Ticket_length)
 final['Ticket_length'] = np.where(((final.Ticket_length == '5')) ,'At 6',final.Ticket_length)
 final['Ticket_length'] = np.where(((final.Ticket_length == '12')) ,'Above 6',final.Ticket_length)
<categorify>
train_datagen = ImageDataGenerator(
 rescale=1/255,
validation_split=0.10,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest' 
)
 shuffeled_df=train_dataframe.sample(frac=1)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = shuffeled_df[:14000],
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
batch_size=250,
shuffle=True,
class_mode="binary" 
)
 valid_datagen = ImageDataGenerator(
 rescale=1/255 
)
 valid_generator = valid_datagen.flow_from_dataframe(
 dataframe = shuffeled_df[14000:],
directory = train_dir,
x_col="id",
y_col="has_cactus",
target_size=(32,32),
batch_size=125,
shuffle=True,
class_mode="binary"
)
conversion = pd.get_dummies(final.Ticket_length, prefix = 'Ticket Length')
 final = pd.concat([final , conversion], axis = 1)
 final.drop(['Ticket','Ticket_length'],axis = 1, inplace = True )<categorify>
pretrained_net = EfficientNetB3(
 weights='imagenet',
input_shape=(32,32,3),
include_top=False,
pooling='avg' 
)
 model = Sequential()
model.add(pretrained_net)
 model.add(Dropout(rate = 0.1))
model.add(Dense(units = 1, activation='sigmoid'))
final = pd.get_dummies(final, columns = ["Sex"],prefix="Gender_" )<drop_column>
model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'] )
final.head()
final.drop(['PassengerId'],axis = 1,inplace = True)
 final.drop(['SibSp','Parch','Family'],axis = 1,inplace = True )<count_missing_values>
history = model.fit_generator(
 train_generator,
epochs = 20,
steps_per_epoch = train_generator.n//train_generator.batch_size,
validation_data = valid_generator,
validation_steps = valid_generator.n//valid_generator.batch_size
)
final.isnull().sum()<concatenate>
K.set_value(model.optimizer.lr,0.00001 )
correlation = final.copy()
sur = pd.concat([df['Survived'],result['Survived']],axis = 0)
 correlation = pd.concat([correlation,sur],axis = 1 )<import_modules>
history = model.fit_generator(
 train_generator,
epochs = 5,
steps_per_epoch = train_generator.n//train_generator.batch_size,
validation_data = valid_generator,
validation_steps = valid_generator.n//valid_generator.batch_size
)
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score,confusion_matrix
from sklearn import metrics
from sklearn.ensemble import VotingClassifier<normalization>
preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
 )
x_train = final[:891]
feature_scaler = MinMaxScaler()
x_train = feature_scaler.fit_transform(x_train )<normalization>
image_ids = [name.split('/')[-1] for name in test_generator.filenames]
predictions = preds.flatten()
data = {'id': image_ids, 'has_cactus':predictions}
submission = pd.DataFrame(data)
 print(submission.head() )
y_train = final[891:]
feature_scaler = MinMaxScaler()
y_train = feature_scaler.fit_transform(y_train )<prepare_x_and_y>
submission.to_csv("submission.csv", index=False )
x_test = df1['Survived']<prepare_x_and_y>
submission.to_csv("submission.csv", index=False )
<define_variables><EOS> 
def clip(x):
if x>=0.5:
return 0.999999
else:
return 0
submission.has_cactus=submission.has_cactus.apply(clip)
 submission.to_csv("./submission_clipped.csv",index=False )