{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-27T03:41:16.621215Z","iopub.execute_input":"2022-07-27T03:41:16.621909Z","iopub.status.idle":"2022-07-27T03:41:16.630987Z","shell.execute_reply.started":"2022-07-27T03:41:16.621875Z","shell.execute_reply":"2022-07-27T03:41:16.629057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ライブラリの読み込み\nimport numpy as np\nimport sys\nimport tensorflow as tf\nimport os\nimport sys\nimport pandas as pd\nfrom keras import layers\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import RMSprop\n\n\n\n%matplotlib inline\nimport matplotlib.image as img\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix\nimport plotly.graph_objects as go\nimport itertools\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:16.643613Z","iopub.execute_input":"2022-07-27T03:41:16.644167Z","iopub.status.idle":"2022-07-27T03:41:16.662583Z","shell.execute_reply.started":"2022-07-27T03:41:16.644124Z","shell.execute_reply":"2022-07-27T03:41:16.661264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 乱数のシードを設定する\nseed = 0\nnp.random.seed(seed)\ntf.random.set_seed(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:16.665421Z","iopub.execute_input":"2022-07-27T03:41:16.665720Z","iopub.status.idle":"2022-07-27T03:41:16.678362Z","shell.execute_reply.started":"2022-07-27T03:41:16.665694Z","shell.execute_reply":"2022-07-27T03:41:16.677349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:16.681780Z","iopub.execute_input":"2022-07-27T03:41:16.682378Z","iopub.status.idle":"2022-07-27T03:41:33.116323Z","shell.execute_reply.started":"2022-07-27T03:41:16.682343Z","shell.execute_reply":"2022-07-27T03:41:33.115337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../working'))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.118254Z","iopub.execute_input":"2022-07-27T03:41:33.118890Z","iopub.status.idle":"2022-07-27T03:41:33.124589Z","shell.execute_reply.started":"2022-07-27T03:41:33.118846Z","shell.execute_reply":"2022-07-27T03:41:33.123537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_IMAGE_FOLDER_PATH = \"../working/train\"\nTEST_IMAGE_FOLDER_PATH = \"../working/test\"\n\nWIDTH = 150\nHEIGHT = 150","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.125969Z","iopub.execute_input":"2022-07-27T03:41:33.126861Z","iopub.status.idle":"2022-07-27T03:41:33.133185Z","shell.execute_reply.started":"2022-07-27T03:41:33.126823Z","shell.execute_reply":"2022-07-27T03:41:33.132157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILE_NAMES = os.listdir(TRAIN_IMAGE_FOLDER_PATH)\nFILE_NAMES[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.136502Z","iopub.execute_input":"2022-07-27T03:41:33.137109Z","iopub.status.idle":"2022-07-27T03:41:33.161252Z","shell.execute_reply.started":"2022-07-27T03:41:33.137068Z","shell.execute_reply":"2022-07-27T03:41:33.160326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\nfor i in os.listdir(TRAIN_IMAGE_FOLDER_PATH):\n    labels+=[i]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.162594Z","iopub.execute_input":"2022-07-27T03:41:33.163022Z","iopub.status.idle":"2022-07-27T03:41:33.187497Z","shell.execute_reply.started":"2022-07-27T03:41:33.162986Z","shell.execute_reply":"2022-07-27T03:41:33.186665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# empty list\ntargets = list()\nfull_paths = list()\ntrain_cats_dir = list()\ntrain_dogs_dir = list()\n\n# finding each file's target\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # target name\n    full_path = os.path.join(TRAIN_IMAGE_FOLDER_PATH, file_name)\n    \n    if(target == \"dog\"):\n        train_dogs_dir.append(full_path)\n    if(target == \"cat\"):\n        train_cats_dir.append(full_path)\n    \n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset = pd.DataFrame() # make dataframe\ndataset['image_path'] = full_paths # file path\ndataset['target'] = targets # file's target","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.188720Z","iopub.execute_input":"2022-07-27T03:41:33.189627Z","iopub.status.idle":"2022-07-27T03:41:33.270783Z","shell.execute_reply.started":"2022-07-27T03:41:33.189588Z","shell.execute_reply":"2022-07-27T03:41:33.269915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILE_NAMES = os.listdir(TEST_IMAGE_FOLDER_PATH)\nFILE_NAMES[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.273017Z","iopub.execute_input":"2022-07-27T03:41:33.273698Z","iopub.status.idle":"2022-07-27T03:41:33.288839Z","shell.execute_reply.started":"2022-07-27T03:41:33.273661Z","shell.execute_reply":"2022-07-27T03:41:33.287822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\nfor i in os.listdir(TEST_IMAGE_FOLDER_PATH):\n    labels+=[i]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.290512Z","iopub.execute_input":"2022-07-27T03:41:33.290854Z","iopub.status.idle":"2022-07-27T03:41:33.306083Z","shell.execute_reply.started":"2022-07-27T03:41:33.290820Z","shell.execute_reply":"2022-07-27T03:41:33.305254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# empty list\ntargets = list()\nfull_paths = list()\n\n# finding each file's target\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # target name\n    full_path = os.path.join(TEST_IMAGE_FOLDER_PATH, file_name)\n       \n    full_paths.append(full_path)\n    targets.append(target)\n\ntest_df = pd.DataFrame() # make dataframe\ntest_df['image_path'] = full_paths # file path\ntest_df['target'] = targets # file's target","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.307671Z","iopub.execute_input":"2022-07-27T03:41:33.308024Z","iopub.status.idle":"2022-07-27T03:41:33.350187Z","shell.execute_reply.started":"2022-07-27T03:41:33.307988Z","shell.execute_reply":"2022-07-27T03:41:33.349327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.355865Z","iopub.execute_input":"2022-07-27T03:41:33.356143Z","iopub.status.idle":"2022-07-27T03:41:33.365920Z","shell.execute_reply.started":"2022-07-27T03:41:33.356118Z","shell.execute_reply":"2022-07-27T03:41:33.365007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.367164Z","iopub.execute_input":"2022-07-27T03:41:33.367788Z","iopub.status.idle":"2022-07-27T03:41:33.382261Z","shell.execute_reply.started":"2022-07-27T03:41:33.367750Z","shell.execute_reply":"2022-07-27T03:41:33.380945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.383707Z","iopub.execute_input":"2022-07-27T03:41:33.384048Z","iopub.status.idle":"2022-07-27T03:41:33.390381Z","shell.execute_reply.started":"2022-07-27T03:41:33.384015Z","shell.execute_reply":"2022-07-27T03:41:33.389316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"total data counts:\", dataset['target'].count())\ncounts = dataset['target'].value_counts()\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.392143Z","iopub.execute_input":"2022-07-27T03:41:33.392983Z","iopub.status.idle":"2022-07-27T03:41:33.407959Z","shell.execute_reply.started":"2022-07-27T03:41:33.392944Z","shell.execute_reply":"2022-07-27T03:41:33.406852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_train, dataset_val = train_test_split(dataset, test_size=0.2, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.409790Z","iopub.execute_input":"2022-07-27T03:41:33.410352Z","iopub.status.idle":"2022-07-27T03:41:33.422035Z","shell.execute_reply.started":"2022-07-27T03:41:33.410315Z","shell.execute_reply":"2022-07-27T03:41:33.420873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_id_distributionTrain = dataset_train['target'].value_counts()\nclass_id_distributionTrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.424277Z","iopub.execute_input":"2022-07-27T03:41:33.424944Z","iopub.status.idle":"2022-07-27T03:41:33.438499Z","shell.execute_reply.started":"2022-07-27T03:41:33.424906Z","shell.execute_reply":"2022-07-27T03:41:33.437174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_id_distributionTest = dataset_val['target'].value_counts()\nclass_id_distributionTest.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.440573Z","iopub.execute_input":"2022-07-27T03:41:33.441338Z","iopub.status.idle":"2022-07-27T03:41:33.450556Z","shell.execute_reply.started":"2022-07-27T03:41:33.441277Z","shell.execute_reply":"2022-07-27T03:41:33.449411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(rescale=1./255,\n                                 rotation_range=45,\n                                 width_shift_range=0.2,\n                                 height_shift_range=0.2,\n                                 shear_range=0.25,\n                                 zoom_range=0.2,\n                                 channel_shift_range=100,\n                                 horizontal_flip=True,\n                                 fill_mode='nearest')\n\ntrain_datagenerator=train_datagen.flow_from_dataframe(dataframe=dataset_train,\n                                                     x_col=\"image_path\",\n                                                     y_col=\"target\",\n                                                     target_size=(WIDTH, HEIGHT),\n                                                     class_mode=\"binary\",\n                                                     batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.452065Z","iopub.execute_input":"2022-07-27T03:41:33.452591Z","iopub.status.idle":"2022-07-27T03:41:33.650161Z","shell.execute_reply.started":"2022-07-27T03:41:33.452526Z","shell.execute_reply":"2022-07-27T03:41:33.649128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator=validation_datagen.flow_from_dataframe(dataframe=dataset_val,\n                                                   x_col=\"image_path\",\n                                                   y_col=\"target\",\n                                                   target_size=(WIDTH, HEIGHT),\n                                                   class_mode=\"binary\",\n                                                   batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.651698Z","iopub.execute_input":"2022-07-27T03:41:33.652340Z","iopub.status.idle":"2022-07-27T03:41:33.714041Z","shell.execute_reply.started":"2022-07-27T03:41:33.652299Z","shell.execute_reply":"2022-07-27T03:41:33.712795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_da = Sequential()\nmodel_da.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)))\nmodel_da.add(layers.MaxPooling2D(2, 2))\nmodel_da.add(layers.Conv2D(64, (3,3), activation='relu'))\nmodel_da.add(layers.MaxPooling2D(2,2))\nmodel_da.add(layers.Conv2D(128, (3,3), activation='relu'))\nmodel_da.add(layers.MaxPooling2D(2,2))\n\nmodel_da.add(layers.Flatten())\nmodel_da.add(layers.Dropout(0.5))\nmodel_da.add(layers.Dense(512, activation='relu'))\nmodel_da.add(layers.Dense(1, activation='sigmoid'))\n\nmodel_da.compile(loss='binary_crossentropy',\n            optimizer=RMSprop(learning_rate=1e-4),\n            metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.715799Z","iopub.execute_input":"2022-07-27T03:41:33.716180Z","iopub.status.idle":"2022-07-27T03:41:33.788943Z","shell.execute_reply.started":"2022-07-27T03:41:33.716133Z","shell.execute_reply":"2022-07-27T03:41:33.787934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_da.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.790630Z","iopub.execute_input":"2022-07-27T03:41:33.791004Z","iopub.status.idle":"2022-07-27T03:41:33.797576Z","shell.execute_reply.started":"2022-07-27T03:41:33.790966Z","shell.execute_reply":"2022-07-27T03:41:33.796528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"History=model_da.fit(train_datagenerator,\n                  epochs=25,\n                  validation_data=validation_generator,\n                  validation_steps=dataset_val.shape[0]/150,\n                  steps_per_epoch=dataset_train.shape[0]/150)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:41:33.799247Z","iopub.execute_input":"2022-07-27T03:41:33.799921Z","iopub.status.idle":"2022-07-27T03:47:45.282016Z","shell.execute_reply.started":"2022-07-27T03:41:33.799883Z","shell.execute_reply":"2022-07-27T03:47:45.280925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = History.history['accuracy']\nval_acc = History.history['val_accuracy']\nloss = History.history['loss']\nval_loss = History.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, label='Training accuracy')\nplt.plot(epochs, val_acc, label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.show()\nfig = plt.figure()\nfig.savefig(\"acc.png\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:47:45.283819Z","iopub.execute_input":"2022-07-27T03:47:45.284173Z","iopub.status.idle":"2022-07-27T03:47:45.491326Z","shell.execute_reply.started":"2022-07-27T03:47:45.284128Z","shell.execute_reply":"2022-07-27T03:47:45.490537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(epochs, loss, label='Training Loss')\nplt.plot(epochs, val_loss, label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()\nfig = plt.figure()\nfig.savefig(\"loss.png\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:47:45.492683Z","iopub.execute_input":"2022-07-27T03:47:45.493009Z","iopub.status.idle":"2022-07-27T03:47:45.685086Z","shell.execute_reply.started":"2022-07-27T03:47:45.492975Z","shell.execute_reply":"2022-07-27T03:47:45.684205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model_da.evaluate(validation_generator, steps=len(validation_generator), verbose=1)\nprint('Loss: %.3f' % (val_loss * 100.0)) \nprint('Accuracy: %.3f' % (val_acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:47:45.687360Z","iopub.execute_input":"2022-07-27T03:47:45.688053Z","iopub.status.idle":"2022-07-27T03:48:00.605221Z","shell.execute_reply.started":"2022-07-27T03:47:45.688016Z","shell.execute_reply":"2022-07-27T03:48:00.604161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:48:00.606861Z","iopub.execute_input":"2022-07-27T03:48:00.607224Z","iopub.status.idle":"2022-07-27T03:48:00.612366Z","shell.execute_reply.started":"2022-07-27T03:48:00.607188Z","shell.execute_reply":"2022-07-27T03:48:00.611444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gen_image_label(directory):\n    ''' A generator that yields (label, id, jpg_filename) tuple.'''\n    for root, dirs, files in os.walk(directory):\n        for f in files:\n            _, ext = os.path.splitext(f)\n            if ext != '.jpg':\n                continue\n            basename = os.path.basename(f)\n            splits = basename.split('.')\n            if len(splits) == 3:\n                label, id_, ext = splits\n            else:\n                label = None\n                id_, ext = splits\n            fullname = os.path.join(root, f)\n            yield label, int(id_), fullname","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_dir = \"../working/test/\"\nlst = list(gen_image_label(test_data_dir))\ntest_df = pd.DataFrame(lst, columns=['label', 'id', 'filename'])\ntest_df = test_df.sort_values(by=['label', 'id'])\ntest_df['label_code'] = test_df.label.map({'cat':0, 'dog':1})\n\ntest_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:49:40.990803Z","iopub.execute_input":"2022-07-27T03:49:40.991155Z","iopub.status.idle":"2022-07-27T03:49:41.106786Z","shell.execute_reply.started":"2022-07-27T03:49:40.991118Z","shell.execute_reply":"2022-07-27T03:49:41.105704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test, dataset_test2 = train_test_split(dataset, test_size=0.5, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:56:27.744271Z","iopub.execute_input":"2022-07-27T03:56:27.745224Z","iopub.status.idle":"2022-07-27T03:56:27.755682Z","shell.execute_reply.started":"2022-07-27T03:56:27.745183Z","shell.execute_reply":"2022-07-27T03:56:27.754597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator=test_datagen.flow_from_dataframe(dataframe=dataset_test,\n                                                   x_col=\"image_path\",\n                                                   y_col=\"target\",\n                                                   target_size=(WIDTH, HEIGHT),\n                                                   class_mode=\"binary\",\n                                                   batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:56:30.398057Z","iopub.execute_input":"2022-07-27T03:56:30.398434Z","iopub.status.idle":"2022-07-27T03:56:30.523544Z","shell.execute_reply.started":"2022-07-27T03:56:30.398404Z","shell.execute_reply":"2022-07-27T03:56:30.522416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model_da.predict(x=test_generator, steps= len(test_generator), verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:56:46.052231Z","iopub.execute_input":"2022-07-27T03:56:46.052924Z","iopub.status.idle":"2022-07-27T03:57:22.943472Z","shell.execute_reply.started":"2022-07-27T03:56:46.052887Z","shell.execute_reply":"2022-07-27T03:57:22.942482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true=test_generator.classes, y_pred=np.argmax(predictions, axis=-1))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:57:27.902302Z","iopub.execute_input":"2022-07-27T03:57:27.902882Z","iopub.status.idle":"2022-07-27T03:57:27.916897Z","shell.execute_reply.started":"2022-07-27T03:57:27.902848Z","shell.execute_reply":"2022-07-27T03:57:27.915869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm, classes,normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):\n    \n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n        print(cm)\n    \n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n        horizontalalignment=\"center\",\n        color=\"white\" if cm[i, j] > thresh else \"black\")\n        plt.tight_layout()\n        plt.ylabel('True label')\n        plt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:57:27.956496Z","iopub.execute_input":"2022-07-27T03:57:27.956788Z","iopub.status.idle":"2022-07-27T03:57:27.965993Z","shell.execute_reply.started":"2022-07-27T03:57:27.956764Z","shell.execute_reply":"2022-07-27T03:57:27.964852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm_plot_labels = ['0_cat', '1_dog']\n\nplot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:57:27.993093Z","iopub.execute_input":"2022-07-27T03:57:27.993698Z","iopub.status.idle":"2022-07-27T03:57:28.257767Z","shell.execute_reply.started":"2022-07-27T03:57:27.993646Z","shell.execute_reply":"2022-07-27T03:57:28.256815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:57:28.269979Z","iopub.execute_input":"2022-07-27T03:57:28.270511Z","iopub.status.idle":"2022-07-27T03:57:28.281194Z","shell.execute_reply.started":"2022-07-27T03:57:28.270475Z","shell.execute_reply":"2022-07-27T03:57:28.280243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# csvの作成\nresults = pd.DataFrame({'id': pd.Series(test_df.id.values[:predictions.shape[0]]),\n                        'label': pd.Series(predictions.T[0])})\nresults.to_csv('submission.csv', index=False)\nresults.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T03:57:28.283436Z","iopub.execute_input":"2022-07-27T03:57:28.283868Z","iopub.status.idle":"2022-07-27T03:57:28.327197Z","shell.execute_reply.started":"2022-07-27T03:57:28.283831Z","shell.execute_reply":"2022-07-27T03:57:28.326342Z"},"trusted":true},"execution_count":null,"outputs":[]}]}