Kaggle competition: Dog Breed Identification
knn = KNeighborsClassifier()
cv = cross_val_score(knn,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
from tensorflow.python.keras.applications import InceptionV3
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Activation, Flatten, Dropout, BatchNormalization
from tensorflow.python.keras import optimizers, regularizers
knn = KNeighborsClassifier()
cv = cross_val_score(knn,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
traindf=pd.read_csv('.. /input/dog-breed-identification/labels.csv')
 testdf=pd.read_csv('.. /input/dog-breed-identification/sample_submission.csv' )
rf = RandomForestClassifier(random_state = 1)
 cv = cross_val_score(rf,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
num_classes = 120
inception_weights_path = '.. /input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
my_new_model = Sequential()
my_new_model.add(InceptionV3(include_top=False, pooling='avg', weights=inception_weights_path))
my_new_model.add(Dense(512))
my_new_model.add(Activation('relu'))
my_new_model.add(Dropout(0.5))
my_new_model.add(Dense(num_classes, activation='softmax'))
my_new_model.layers[0].trainable = False
rf = RandomForestClassifier(random_state = 1)
 cv = cross_val_score(rf,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
my_new_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
svc = SVC(probability = True)
 cv = cross_val_score(svc,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
image_size = 299
datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
rescale=1./255.,
horizontal_flip=True,
width_shift_range = 0.2,
height_shift_range = 0.2,
validation_split=0.25 )
svc_poly = SVC(probability = True, kernel='poly', degree=2, gamma='auto', coef0=1, C=5)
 cv = cross_val_score(svc,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
train_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="training",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))
svc_rbf = SVC(probability = True, kernel='rbf', gamma=0.5, C=0.1)
 cv = cross_val_score(svc,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<compute_train_metric>
valid_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="validation",
batch_size=1,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))
xgb = XGBClassifier(random_state =1)
 cv = cross_val_score(xgb,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<choose_model_class>
test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(
 dataframe=testdf,
directory=".. /input/dog-breed-identification/test/",
x_col="id",
y_col=None,
has_ext=False,
batch_size=1,
seed=42,
shuffle=False,
class_mode=None,
target_size=(image_size, image_size))
voting_clf = VotingClassifier(estimators = [('lr',lr),('knn',knn),('rf',rf),('gnb',gnb),('svc',svc),('xgb',xgb), 
('svc_poly', svc_poly),('svc_rbf', svc_rbf)], voting = 'soft' )<compute_train_metric>
STEP_SIZE_TRAIN=train_generator.n
STEP_SIZE_VALID=valid_generator.n
print(STEP_SIZE_TRAIN)
 print(STEP_SIZE_VALID)
 my_new_model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=1
)
cv = cross_val_score(voting_clf,X_train,y_train,cv=5)
 print(cv)
 print(cv.mean() )<save_to_csv>
my_new_model.evaluate_generator(generator=valid_generator )
svc_poly.fit(X_train,y_train)
 test_result = pd.Series(svc_poly.predict(test), name = "Survived" ).astype(int)
 results = pd.concat([test_data["PassengerId"], test_result],axis = 1)
 results.to_csv("titanic_submission.csv", index = False )<import_modules>
test_generator.reset()
pred=my_new_model.predict_generator(test_generator,verbose=1 )
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV<compute_test_metric>
labels =(train_generator.class_indices)
 labels = list(labels.keys())
 df = pd.DataFrame(data=pred,
columns=labels)
 columns = list(df)
 columns.sort()
df = df.reindex(columns=columns)
 filenames = testdf["id"]
df["id"] = filenames
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
df.head(5)
 df.to_csv("results_tl_01.csv",index=False )