Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<categorify>
np.random.seed(42)
 %matplotlib inline
df.Embarked = df.Embarked.fillna(0)
 df['Embarked'] = round(df['Embarked'] ).astype(int)
 df.Fare = df.Fare.astype(int)
 df.info()<import_modules>
train_zip_dir = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'
test_zip_dir = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'
extract_dir = '/kaggle/working/extracted_data'
train_dir = '/kaggle/working/train'
test_dir = '/kaggle/working/test'
os.makedirs(train_dir+'/dog', exist_ok=True)
 os.makedirs(train_dir+'/cat', exist_ok=True)
 os.makedirs(test_dir+'/test_data', exist_ok=True )
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC<choose_model_class>
with zipfile.ZipFile(train_zip_dir, 'r')as zip_ref:
zip_ref.extractall(extract_dir)
 with zipfile.ZipFile(test_zip_dir, 'r')as zip_ref:
zip_ref.extractall(extract_dir )
DTC = DecisionTreeClassifier()
RFC = RandomForestClassifier(n_estimators = 500)
 LR = LogisticRegression()
GNB = GaussianNB()
MLPC = MLPClassifier()
svc = SVC(kernel = 'linear', C = 0.1, gamma = 'scale' )<split>
def get_num_files(path):
if not os.path.exists(path):
return 0
return sum([len(files)for r, d, files in os.walk(path)] )
X = np.asanyarray(df.drop(columns = ['Survived']))
y = np.asanyarray(df.Survived)
 x_train,x_test, y_train, y_test = train_test_split(X,y , test_size = 0.2,random_state = 0 )<predict_on_test>
Image_width,Image_height = 299,299
batch_size=64
total_samples = get_num_files(train_dir)
 val_split=0.2
n_train=total_samples*(1-val_split)
 n_val=total_samples*val_split
num_classes = 2
print(n_train,n_val )
DTC.fit(x_train,y_train)
 y_hat1 = DTC.predict(x_test)
 RFC.fit(x_train,y_train)
 y_hat2 = RFC.predict(x_test)
 LR.fit(x_train,y_train)
 y_hat3 = LR.predict(x_test)
 GNB.fit(x_train,y_train)
 y_hat4 = GNB.predict(x_test)
 MLPC.fit(x_train,y_train)
 y_hat5 = MLPC.predict(x_test)
 svc.fit(x_train,y_train)
 y_hat6 = svc.predict(x_test )<compute_test_metric>
train_image_gen = ImageDataGenerator(rescale=1/255,horizontal_flip=True,validation_split=val_split)
 train_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),
batch_size=batch_size,seed=42,subset='training',
shuffle=True,class_mode='categorical')
 val_generator = train_image_gen.flow_from_directory(train_dir,target_size=(Image_width,Image_height),
batch_size=batch_size,seed=42,subset='validation',
shuffle=True,class_mode='categorical')
print('Accuracy_yhat1_DTC=',accuracy_score(y_test,y_hat1))
print('Accuracy_yhat2_RFC=',accuracy_score(y_test,y_hat2))
print('Accuracy_yhat3_LR=',accuracy_score(y_test,y_hat3))
print('Accuracy_yhat4_GNB=',accuracy_score(y_test,y_hat4))
print('Accuracy_yhat5_MLPC=',accuracy_score(y_test,y_hat5))
print('Accuracy_yhat6_SVC=',accuracy_score(y_test,y_hat6))<compute_test_metric>
LR_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=2, factor=.5, min_lr=.00001)
 EarlyStop_callback = keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=7, restore_best_weights=True)
 my_callback=[EarlyStop_callback, LR_callback]
DTC.score(x_train,y_train )<compute_test_metric>
InceptionV3_base_model = InceptionV3(weights='imagenet', include_top=False)
 x = InceptionV3_base_model.output
x_pool = GlobalAveragePooling2D()(x)
 final_pred = Dense(num_classes,activation='softmax' )(x_pool)
 model = Model(inputs=InceptionV3_base_model.input,outputs=final_pred )
RFC.score(x_train,y_train )<train_model>
layer_to_Freeze=276
for layer in model.layers[:layer_to_Freeze]:
layer.trainable =False
for layer in model.layers[layer_to_Freeze:]:
layer.trainable=True
sgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)
 model.compile(optimizer=sgd,loss='binary_crossentropy',metrics=['accuracy'] )
ETC = ExtraTreesClassifier(n_estimators = 500)
 ETC.fit(x_train,y_train)
 y_hat7 = ETC.predict(x_test)
 ETC.score(x_train,y_train)
 print('Accuracy_yhat7_ETC=',accuracy_score(y_test,y_hat7))<load_from_csv>
history_transfer_learning = model.fit_generator(train_generator,epochs=30,
steps_per_epoch=n_train//batch_size,
validation_data=val_generator,
validation_steps=n_val//batch_size,
verbose=1,
callbacks=my_callback,
class_weight='auto' )
df1 = pd.read_csv('.. /input/test.csv')
 df1.head()<feature_engineering>
score = model.evaluate_generator(val_generator,verbose=1)
 print('Test loss: ', score[0])
 print('Test accuracy', score[1] )
df1['title'] = df1['Name'].str.extract('([A-Za-z]+)\.', expand = False)
 df1['title'] = df1.title.replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df1['title'] = df1['title'].replace('Mlle', 'Miss')
 df1['title'] = df1['title'].replace('Ms', 'Miss')
 df1['title'] = df1['title'].replace('Mme', 'Mrs' )<categorify>
test_image_gen = ImageDataGenerator(rescale=1/255)
 test_generator = test_image_gen.flow_from_directory(test_dir,target_size=(Image_width,Image_height),batch_size=1,seed=42,class_mode=None,shuffle=False )
df1.title = df1.title.fillna(0)
 df1['title'] = df1.title.map({'Rare':0, 'Master':1, 'Miss':2, 'Mr':3, 'Mrs':4})
 df1.Sex = df1.Sex.map({'female':0, 'male':1} ).astype(int )<data_type_conversions>
y_pred = model.predict_generator(generator=test_generator,verbose=1 )
df1 = df1.drop(columns = ['PassengerId', 'Name', 'Ticket', 'Cabin'])
 df1['Embarked'] = df1.Embarked.map({'S':0,'C':1,'Q':2} ).astype(int)
 df1.Age = df1.Age.fillna(df1.Age.mean())
 df1.Age = round(df1.Age ).astype(int)
<feature_engineering>
submission = pd.DataFrame({'id':pd.Series(test_generator.filenames),'label':pd.Series(y_pred.clip(min=0.02,max=0.98)[:,1])})
 submission['id'] = submission.id.str.extract('(\d+)')
 submission['id']=pd.to_numeric(submission['id'])
 submission.to_csv("MySubmission.csv",index=False )
<feature_engineering><EOS> 
submission.to_csv('DogVsCats_submission.csv',index=False)
 shutil.rmtree(train_dir)
 shutil.rmtree(test_dir )