Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<prepare_output>
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
from sklearn.linear_model import LogisticRegression
import os
evaluate_model(ridge_regression_improved_model )<train_model>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
lasso_regression_model = Lasso()
lasso_regression_model.fit(Xt_train, yt_train )<prepare_output>
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224
evaluate_model(lasso_regression_model )<train_model>
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )
lasso_regression_improved_model = Lasso(alpha=optimised_alpha)
 lasso_regression_improved_model.fit(Xt_train, yt_train )<import_modules>
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]
import xgboost as xgb
from xgboost.sklearn import XGBRegressor
from sklearn.model_selection import GridSearchCV<define_search_model>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
xg_reg = xgb.XGBRegressor()<train_model>
train_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB0_ns.npy')
 test_EB0_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB0_ns.npy')
 train_EB1_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB1_ns.npy')
 test_EB1_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB1_ns.npy')
 train_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB2_ns.npy')
 test_EB2_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB2_ns.npy')
 train_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB3_ns.npy')
 test_EB3_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB3_ns.npy')
 train_EB4_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB4_ns.npy')
 test_EB4_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB4_ns.npy')
 train_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB5_ns.npy')
 test_EB5_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB5_ns.npy')
 train_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB6_ns.npy')
 test_EB6_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB6_ns.npy')
 train_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')
 test_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy' )
xg_reg.fit(Xt_train, yt_train )<prepare_output>
X_train, X_val, y_train, y_val = train_test_split(train_EB0_ns, target, test_size=0.1, random_state=42 )
evaluate_model(xg_reg )<init_hyperparams>
%%time
lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train )
<init_hyperparams>
val_preds_EB0 = lr.predict_proba(X_val)[:,1]
p_dict = {'colsample_bytree': 0.8, 'gamma': 0, 'learning_rate': 0.03, 'max_depth': 3, 'min_child_weight': 2, 'n_estimators': 1000, 'objective': 'reg:linear', 'silent': 1, 'subsample': 0.7}<choose_model_class>
roc_auc_score(y_val, val_preds_EB0 )
xg_reg_improved = xgb.XGBRegressor(objective = p_dict['objective'], colsample_bytree = p_dict['colsample_bytree'],
learning_rate = p_dict['learning_rate'], max_depth = p_dict['max_depth'], min_child_weight = p_dict['min_child_weight'],
n_estimators = p_dict['n_estimators'], silent=p_dict['silent'], subsample=p_dict['subsample'] )<train_model>
log_loss(y_val, val_preds_EB0 )
xg_reg_improved.fit(Xt_train, yt_train )<prepare_output>
test_preds_EB0 = lr.predict_proba(test_EB0_ns)[:,1]
evaluate_model(xg_reg_improved )<predict_on_test>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB1_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB1 = lr.predict_proba(X_val)[:,1]
test_preds_EB1 = lr.predict_proba(test_EB1_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB1))
print(log_loss(y_val, val_preds_EB1))
def return_predictions_model(model,inputs):
predictions = model.predict(inputs)
 return predictions<predict_on_test>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB2_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB2 = lr.predict_proba(X_val)[:,1]
test_preds_EB2 = lr.predict_proba(test_EB2_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB2))
print(log_loss(y_val, val_preds_EB2))
lasso_regression_predict = return_predictions_model(lasso_regression_improved_model,Xt_test)
 xg_reg_predict = return_predictions_model(xg_reg_improved,Xt_test)
 ridge_regression_predict = return_predictions_model(ridge_regression_improved_model,Xt_test )<compute_test_metric>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB3_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB3 = lr.predict_proba(X_val)[:,1]
test_preds_EB3 = lr.predict_proba(test_EB3_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB3))
print(log_loss(y_val, val_preds_EB3))
stacked_predictions = np.array([lasso_regression_predict,xg_reg_predict,ridge_regression_predict])
 stacked_predictions_avg = np.average(stacked_predictions, axis=0)
 rmse_test = np.sqrt(mean_squared_error(yt_test,stacked_predictions_avg))
print("RMSE on Test set(non-weighted):", rmse_test)
 stacked_predictions_weighted =(3*lasso_regression_predict + 2*xg_reg_predict + ridge_regression_predict)/6
rmse_test = np.sqrt(mean_squared_error(yt_test,stacked_predictions_weighted))
print("RMSE on Test set(weighted):", rmse_test)
<train_model>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB4_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.019, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB4 = lr.predict_proba(X_val)[:,1]
test_preds_EB4 = lr.predict_proba(test_EB4_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB4))
print(log_loss(y_val, val_preds_EB4))
lasso_regression_improved_model.fit(X_train, y)
 xg_reg_improved.fit(X_train, y)
 ridge_regression_improved_model.fit(X_train, y )<predict_on_test>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB5_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB5 = lr.predict_proba(X_val)[:,1]
test_preds_EB5 = lr.predict_proba(test_EB5_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB5))
print(log_loss(y_val, val_preds_EB5))
lasso_regression_predict = return_predictions_model(lasso_regression_improved_model,X_test)
 xg_reg_predict = return_predictions_model(xg_reg_improved,X_test)
 ridge_regression_predict = return_predictions_model(ridge_regression_improved_model,X_test )<define_variables>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB6_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.02, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB6 = lr.predict_proba(X_val)[:,1]
test_preds_EB6 = lr.predict_proba(test_EB6_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB6))
print(log_loss(y_val, val_preds_EB6))
stacked_predictions_weighted =(3*lasso_regression_predict + 2*xg_reg_predict + ridge_regression_predict)/6<prepare_output>
%%time
X_train, X_val, y_train, y_val = train_test_split(train_EB7_ns, target, test_size=0.1, random_state=42)
 lr = LogisticRegression(C=0.026, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds_EB7 = lr.predict_proba(X_val)[:,1]
test_preds_EB7 = lr.predict_proba(test_EB7_ns)[:,1]
print(roc_auc_score(y_val, val_preds_EB7))
print(log_loss(y_val, val_preds_EB7))
d = {'Id': test.index, 'SalePrice': stacked_predictions_weighted}
predictions_df = pd.DataFrame(data=d)
 predictions_df.head(5 )<prepare_output>
print(roc_auc_score(y_val, 0.6*val_preds_EB1+0.4*val_preds_EB0))
print(log_loss(y_val, 0.6*val_preds_EB1+0.4*val_preds_EB0))
predictions_df.SalePrice = predictions_df.SalePrice.apply(np.exp)
 predictions_df.head(5 )<save_to_csv>
sample_submission['id'] = ids
sample_submission['label'] = 0.8*test_preds_EB3+0.2*(0.6*test_preds_EB2+0.4*(0.6*test_preds_EB1+0.4*test_preds_EB0))
sample_submission.to_csv('submission_0.csv', index=False)
 sample_submission.head()
predictions_df.to_csv('output.csv', index=False)
 predictions_df.head(5 )<import_modules>
sample_submission['id'] = ids
sample_submission['label'] = 0.5*test_preds_EB4+0.5*test_preds_EB5
sample_submission.to_csv('submission_1.csv', index=False)
 sample_submission.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import keras
import tensorflow
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report,accuracy_score
from keras.models import Sequential
from keras.layers import Convolution2D,MaxPooling2D,Flatten,Dense,Dropout
from keras.utils.np_utils import to_categorical
from keras.datasets import mnist<load_from_csv>
sample_submission['id'] = ids
sample_submission['label'] = 0.9*(0.5*test_preds_EB4+0.5*test_preds_EB5)+0.1*test_preds_EB6
sample_submission.to_csv('submission_2.csv', index=False)
 sample_submission.head()
<count_missing_values><EOS> 
sample_submission['id'] = ids
sample_submission['label'] = 0.9*test_preds_EB7 + 0.1*(0.9*(0.5*test_preds_EB4+0.5*test_preds_EB5)+0.1*test_preds_EB6)
 sample_submission.to_csv('submission_3.csv', index=False)
 sample_submission.head()