{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --use-feature=2020-resolver https://s3-us-west-2.amazonaws.com/xgboost-nightly-builds/xgboost-1.3.0_SNAPSHOT%2B00b0ad1293b4fa74d6aca5da4e9ab7a9d16777f0-py3-none-manylinux2010_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:41:30.773341Z","iopub.execute_input":"2023-05-06T05:41:30.773670Z","iopub.status.idle":"2023-05-06T05:41:34.340660Z","shell.execute_reply.started":"2023-05-06T05:41:30.773640Z","shell.execute_reply":"2023-05-06T05:41:34.339742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport zipfile\nfrom sklearn.model_selection import train_test_split, KFold\nfrom sklearn.metrics import log_loss, roc_auc_score\nfrom sklearn.linear_model import LogisticRegression\nimport gc\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T05:41:49.460075Z","iopub.execute_input":"2023-05-06T05:41:49.460429Z","iopub.status.idle":"2023-05-06T05:41:51.119974Z","shell.execute_reply.started":"2023-05-06T05:41:49.460393Z","shell.execute_reply":"2023-05-06T05:41:51.118669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_FOLDER = '../input/cats-and-dogs-embedded-data/train/train/'\nTEST_FOLDER =  '../input/cats-and-dogs-embedded-data/test/test/'\nIMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:42:03.979699Z","iopub.execute_input":"2023-05-06T05:42:03.980030Z","iopub.status.idle":"2023-05-06T05:42:03.984546Z","shell.execute_reply.started":"2023-05-06T05:42:03.980000Z","shell.execute_reply":"2023-05-06T05:42:03.983237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_EB7_ns = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')\ntest_EB7_ns = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy')\ntrain_EB4_ns = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB4_ns.npy')\ntest_EB4_ns = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB4_ns.npy')","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:42:19.746148Z","iopub.execute_input":"2023-05-06T05:42:19.746523Z","iopub.status.idle":"2023-05-06T05:42:26.623615Z","shell.execute_reply.started":"2023-05-06T05:42:19.746491Z","shell.execute_reply":"2023-05-06T05:42:26.622784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy')","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:43:13.729499Z","iopub.execute_input":"2023-05-06T05:43:13.729836Z","iopub.status.idle":"2023-05-06T05:43:13.743882Z","shell.execute_reply.started":"2023-05-06T05:43:13.729806Z","shell.execute_reply":"2023-05-06T05:43:13.743168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_EB7_ns, target, test_size=0.1, random_state=42)\nlr = LogisticRegression(C=0.026, max_iter=10000)\nlr.fit(X_train, y_train)\nval_preds_EB7_lr = lr.predict_proba(X_val)[:,1]\ntest_preds_EB7_lr = lr.predict_proba(test_EB7_ns)[:,1]\nprint(roc_auc_score(y_val, val_preds_EB7_lr))\nprint(log_loss(y_val, val_preds_EB7_lr))","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:43:15.691436Z","iopub.execute_input":"2023-05-06T05:43:15.691794Z","iopub.status.idle":"2023-05-06T05:43:17.595838Z","shell.execute_reply.started":"2023-05-06T05:43:15.691762Z","shell.execute_reply":"2023-05-06T05:43:17.594908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_params = {\n    'eta': 0.05,\n    'max_depth': 4,\n    'subsample': 0.8,\n    'colsample_bytree': 0.6,\n    'alpha': 0.01,\n    'lambda': 1.00,\n    'gamma' : 0.02,\n    'max_bin': 256,\n    'objective': 'reg:logistic',\n    'eval_metric': 'auc',\n    'verbosity': 0,\n    'tree_method': 'gpu_hist', \n    'predictor': 'gpu_predictor'\n}\n\ndtrain = xgb.DMatrix(X_train, y_train)\ndval = xgb.DMatrix(X_val, y_val)\ndtest = xgb.DMatrix(test_EB7_ns)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:43:21.218615Z","iopub.execute_input":"2023-05-06T05:43:21.218932Z","iopub.status.idle":"2023-05-06T05:43:23.870521Z","shell.execute_reply.started":"2023-05-06T05:43:21.218901Z","shell.execute_reply":"2023-05-06T05:43:23.869506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nmodel = xgb.train(xgb_params, dtrain, num_boost_round=100)\nval_preds_xgb_1 = model.predict(dval)\ntest_preds_xgb_1 = model.predict(dtest)\nprint(roc_auc_score(y_val, val_preds_xgb_1))\nprint(log_loss(y_val, val_preds_xgb_1))","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:43:34.381395Z","iopub.execute_input":"2023-05-06T05:43:34.381717Z","iopub.status.idle":"2023-05-06T05:43:36.951781Z","shell.execute_reply.started":"2023-05-06T05:43:34.381685Z","shell.execute_reply":"2023-05-06T05:43:36.950383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_EB4_ns, target, test_size=0.1, random_state=42)\nlr = LogisticRegression(C=0.019, max_iter=10000)\nlr.fit(X_train, y_train)\nval_preds_EB4_lr = lr.predict_proba(X_val)[:,1]\ntest_preds_EB4_lr = lr.predict_proba(test_EB4_ns)[:,1]\nprint(roc_auc_score(y_val, val_preds_EB4_lr))\nprint(log_loss(y_val, val_preds_EB4_lr))","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:44:17.584615Z","iopub.execute_input":"2023-05-06T05:44:17.584933Z","iopub.status.idle":"2023-05-06T05:44:18.900009Z","shell.execute_reply.started":"2023-05-06T05:44:17.584903Z","shell.execute_reply":"2023-05-06T05:44:18.899092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_params = {\n    'eta': 0.05,\n    'max_depth': 4,\n    'subsample': 0.85,\n    'colsample_bytree': 0.6,\n    'alpha': 0.01,\n    'lambda': 1.00,\n    'gamma' : 0.02,\n    'max_bin': 256,\n    'objective': 'reg:logistic',\n    'eval_metric': 'auc',\n    'verbosity': 0,\n    'tree_method': 'gpu_hist', \n    'predictor': 'gpu_predictor'\n}\n\ndtrain = xgb.DMatrix(X_train, y_train)\ndval = xgb.DMatrix(X_val, y_val)\ndtest = xgb.DMatrix(test_EB4_ns)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:44:26.142975Z","iopub.execute_input":"2023-05-06T05:44:26.143319Z","iopub.status.idle":"2023-05-06T05:44:28.015323Z","shell.execute_reply.started":"2023-05-06T05:44:26.143263Z","shell.execute_reply":"2023-05-06T05:44:28.014534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nval_preds_xgb_2 = 0\ntest_preds_xgb_2 = 0\n\nn_seed = 10\n\nfor j in range(n_seed):\n    xgb_params['seed'] = 3*j**2+154\n    model = xgb.train(xgb_params, dtrain, num_boost_round=100)\n    val_preds_xgb_2 += model.predict(dval)/n_seed\n    test_preds_xgb_2 += model.predict(dtest)/n_seed","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:44:35.308739Z","iopub.execute_input":"2023-05-06T05:44:35.309061Z","iopub.status.idle":"2023-05-06T05:44:46.321881Z","shell.execute_reply.started":"2023-05-06T05:44:35.309030Z","shell.execute_reply":"2023-05-06T05:44:46.320798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ntrain_image_list = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')\ntest_image_list = np.load('../input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy')\nids = [int(x.split('.')[0]) for x in test_image_list]","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:46:20.421088Z","iopub.execute_input":"2023-05-06T05:46:20.421429Z","iopub.status.idle":"2023-05-06T05:46:20.454242Z","shell.execute_reply.started":"2023-05-06T05:46:20.421397Z","shell.execute_reply":"2023-05-06T05:46:20.453579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['id'] = ids\nsubs = np.clip(1.1*(0.5*test_preds_xgb_1+0.5*test_preds_EB7_lr)-0.1*(0.5*test_preds_xgb_2+0.5*test_preds_EB4_lr), 0, 1)\nsubs[subs > 0.9973] = 1\nsample_submission['label'] = subs\nsample_submission.to_csv('submission_EB7_EB4_3_c.csv', index=False)\nsample_submission.head()\n\nsample_submission['id'] = ids\nsubs = np.clip(1.1*(0.5*test_preds_xgb_1+0.5*test_preds_EB7_lr)-0.1*(0.5*test_preds_xgb_2+0.5*test_preds_EB4_lr), 0, 1)\nsubs[subs > 0.9973] = 1\nsubs[subs <  0.0018] = 0\nsample_submission['label'] = subs\nsample_submission.to_csv('submission_EB7_EB4_3_d.csv', index=False)\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T05:46:22.742032Z","iopub.execute_input":"2023-05-06T05:46:22.742376Z","iopub.status.idle":"2023-05-06T05:46:23.041001Z","shell.execute_reply.started":"2023-05-06T05:46:22.742342Z","shell.execute_reply":"2023-05-06T05:46:23.040250Z"},"trusted":true},"execution_count":null,"outputs":[]}]}