{"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":"2023-07-27T07:30:52.897881Z","iopub.execute_input":"2023-07-27T07:30:52.898343Z","iopub.status.idle":"2023-07-27T07:30:52.911844Z","shell.execute_reply.started":"2023-07-27T07:30:52.898281Z","shell.execute_reply":"2023-07-27T07:30:52.910459Z"},"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":"2023-07-27T07:30:52.914760Z","iopub.execute_input":"2023-07-27T07:30:52.915524Z","iopub.status.idle":"2023-07-27T07:31:00.807848Z","shell.execute_reply.started":"2023-07-27T07:30:52.915483Z","shell.execute_reply":"2023-07-27T07:31:00.806748Z"},"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":"2023-07-27T07:31:00.809521Z","iopub.execute_input":"2023-07-27T07:31:00.809916Z","iopub.status.idle":"2023-07-27T07:31:00.816642Z","shell.execute_reply.started":"2023-07-27T07:31:00.809876Z","shell.execute_reply":"2023-07-27T07:31:00.815604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\n# 圧縮ファイルの解凍\nzipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/train.zip\", \"r\").extractall(\".\")\nzipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/test.zip\", \"r\").extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:00.820408Z","iopub.execute_input":"2023-07-27T07:31:00.821126Z","iopub.status.idle":"2023-07-27T07:31:18.165100Z","shell.execute_reply.started":"2023-07-27T07:31:00.821077Z","shell.execute_reply":"2023-07-27T07:31:18.164012Z"},"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":"2023-07-27T07:31:18.166585Z","iopub.execute_input":"2023-07-27T07:31:18.167001Z","iopub.status.idle":"2023-07-27T07:31:18.178962Z","shell.execute_reply.started":"2023-07-27T07:31:18.166950Z","shell.execute_reply":"2023-07-27T07:31:18.177508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# トレーニングデータの最初を表示\nFILE_NAMES = os.listdir(TRAIN_IMAGE_FOLDER_PATH)\nFILE_NAMES[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.182184Z","iopub.execute_input":"2023-07-27T07:31:18.182488Z","iopub.status.idle":"2023-07-27T07:31:18.208613Z","shell.execute_reply.started":"2023-07-27T07:31:18.182461Z","shell.execute_reply":"2023-07-27T07:31:18.207722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\n# トレーニングデータに番号をつける\nfor i in os.listdir(TRAIN_IMAGE_FOLDER_PATH):\n    labels+=[i]","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.209761Z","iopub.execute_input":"2023-07-27T07:31:18.210046Z","iopub.status.idle":"2023-07-27T07:31:18.237111Z","shell.execute_reply.started":"2023-07-27T07:31:18.210020Z","shell.execute_reply":"2023-07-27T07:31:18.236252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 変数初期化\ntargets = list()\nfull_paths = list()\ntrain_cats_dir = list()\ntrain_dogs_dir = list()\n\n# ターゲット内のファイルを処理\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # ターゲットネーム\n    full_path = os.path.join(TRAIN_IMAGE_FOLDER_PATH, file_name)\n    \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() # データフレームの作成\ndataset['image_path'] = full_paths # ファイルパス\ndataset['target'] = targets # 犬か猫か","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.238444Z","iopub.execute_input":"2023-07-27T07:31:18.238891Z","iopub.status.idle":"2023-07-27T07:31:18.349019Z","shell.execute_reply.started":"2023-07-27T07:31:18.238852Z","shell.execute_reply":"2023-07-27T07:31:18.348039Z"},"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":"2023-07-27T07:31:18.350627Z","iopub.execute_input":"2023-07-27T07:31:18.350999Z","iopub.status.idle":"2023-07-27T07:31:18.367663Z","shell.execute_reply.started":"2023-07-27T07:31:18.350962Z","shell.execute_reply":"2023-07-27T07:31:18.366559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\n# テストデータに番号をつける\nfor i in os.listdir(TEST_IMAGE_FOLDER_PATH):\n    labels+=[i]","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.372699Z","iopub.execute_input":"2023-07-27T07:31:18.372991Z","iopub.status.idle":"2023-07-27T07:31:18.389976Z","shell.execute_reply.started":"2023-07-27T07:31:18.372966Z","shell.execute_reply":"2023-07-27T07:31:18.389046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 変数初期化\ntargets = list()\nfull_paths = list()\n\n# 見つかったタ―ゲットに対して処理\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] # ターゲット名\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() # データフレーム\ntest_df['image_path'] = full_paths # ファイルパス\ntest_df['target'] = targets # ターゲットファイル","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.391396Z","iopub.execute_input":"2023-07-27T07:31:18.392438Z","iopub.status.idle":"2023-07-27T07:31:18.442822Z","shell.execute_reply.started":"2023-07-27T07:31:18.392402Z","shell.execute_reply":"2023-07-27T07:31:18.441801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の最初を表示\nprint(dataset.head(10))\n# 結果の最初を表示\ntest_df.head(10)\n# テストデータ数\nprint(len(test_df))\nprint(\"データ数:\", dataset['target'].count())\ncounts = dataset['target'].value_counts()\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.444492Z","iopub.execute_input":"2023-07-27T07:31:18.444874Z","iopub.status.idle":"2023-07-27T07:31:18.466356Z","shell.execute_reply.started":"2023-07-27T07:31:18.444837Z","shell.execute_reply":"2023-07-27T07:31:18.465360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 配列を２分割\ndataset_train, dataset_val = train_test_split(dataset, test_size=0.2, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.468109Z","iopub.execute_input":"2023-07-27T07:31:18.468518Z","iopub.status.idle":"2023-07-27T07:31:18.480654Z","shell.execute_reply.started":"2023-07-27T07:31:18.468479Z","shell.execute_reply":"2023-07-27T07:31:18.479268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ターゲットの犬猫の数把握\nclass_id_distributionTrain = dataset_train['target'].value_counts()\nclass_id_distributionTrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.482493Z","iopub.execute_input":"2023-07-27T07:31:18.482891Z","iopub.status.idle":"2023-07-27T07:31:18.496245Z","shell.execute_reply.started":"2023-07-27T07:31:18.482853Z","shell.execute_reply":"2023-07-27T07:31:18.495079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストの犬猫の数把握\nclass_id_distributionTest = dataset_val['target'].value_counts()\nclass_id_distributionTest.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:18.498311Z","iopub.execute_input":"2023-07-27T07:31:18.499136Z","iopub.status.idle":"2023-07-27T07:31:18.509673Z","shell.execute_reply.started":"2023-07-27T07:31:18.499095Z","shell.execute_reply":"2023-07-27T07:31:18.508580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像にノイズを入れてデータを増やす\ntrain_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":"2023-07-27T07:31:18.511398Z","iopub.execute_input":"2023-07-27T07:31:18.512185Z","iopub.status.idle":"2023-07-27T07:31:18.753720Z","shell.execute_reply.started":"2023-07-27T07:31:18.512138Z","shell.execute_reply":"2023-07-27T07:31:18.752459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像にノイズを入れてデータを増やす\nvalidation_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":"2023-07-27T07:31:18.755515Z","iopub.execute_input":"2023-07-27T07:31:18.756229Z","iopub.status.idle":"2023-07-27T07:31:18.828783Z","shell.execute_reply.started":"2023-07-27T07:31:18.756184Z","shell.execute_reply":"2023-07-27T07:31:18.827658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# レイヤー層を追加\nmodel_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":"2023-07-27T07:31:18.830670Z","iopub.execute_input":"2023-07-27T07:31:18.831105Z","iopub.status.idle":"2023-07-27T07:31:22.819772Z","shell.execute_reply.started":"2023-07-27T07:31:18.831054Z","shell.execute_reply":"2023-07-27T07:31:22.818675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_da.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-27T07:31:22.822252Z","iopub.execute_input":"2023-07-27T07:31:22.822911Z","iopub.status.idle":"2023-07-27T07:31:22.831501Z","shell.execute_reply.started":"2023-07-27T07:31:22.822870Z","shell.execute_reply":"2023-07-27T07:31:22.830397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 機械学習実行\nHistory=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":"2023-07-27T07:31:22.833028Z","iopub.execute_input":"2023-07-27T07:31:22.834096Z","iopub.status.idle":"2023-07-27T08:48:07.984898Z","shell.execute_reply.started":"2023-07-27T07:31:22.834055Z","shell.execute_reply":"2023-07-27T08:48:07.983862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の保存と表示\nacc = 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":"2023-07-27T08:48:07.986809Z","iopub.execute_input":"2023-07-27T08:48:07.987132Z","iopub.status.idle":"2023-07-27T08:48:08.241378Z","shell.execute_reply.started":"2023-07-27T08:48:07.987097Z","shell.execute_reply":"2023-07-27T08:48:08.240383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の保存と表示\nplt.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":"2023-07-27T08:48:08.242818Z","iopub.execute_input":"2023-07-27T08:48:08.243240Z","iopub.status.idle":"2023-07-27T08:48:08.471422Z","shell.execute_reply.started":"2023-07-27T08:48:08.243207Z","shell.execute_reply":"2023-07-27T08:48:08.470196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の表示\nval_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":"2023-07-27T08:48:08.473445Z","iopub.execute_input":"2023-07-27T08:48:08.474121Z","iopub.status.idle":"2023-07-27T08:48:24.611986Z","shell.execute_reply.started":"2023-07-27T08:48:08.474068Z","shell.execute_reply":"2023-07-27T08:48:24.610775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2023-07-27T08:48:24.615396Z","iopub.execute_input":"2023-07-27T08:48:24.615716Z","iopub.status.idle":"2023-07-27T08:48:24.620682Z","shell.execute_reply.started":"2023-07-27T08:48:24.615686Z","shell.execute_reply":"2023-07-27T08:48:24.619531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学習結果をテストデータにかけて実行\ndef gen_image_label(directory):\n    ''' 犬猫判別.'''\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":{"iopub.status.busy":"2023-07-27T09:28:05.893356Z","iopub.execute_input":"2023-07-27T09:28:05.894370Z","iopub.status.idle":"2023-07-27T09:28:05.903956Z","shell.execute_reply.started":"2023-07-27T09:28:05.894299Z","shell.execute_reply":"2023-07-27T09:28:05.902869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学習結果をテストデータにかけて実行\ntest_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'])\n# test_df['label_code'] = test_df.label.map({'cat':0, 'dog':1})\ntest_df['label_code'] = dataset.target.map({'cat':0, 'dog':1})\n\ntest_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T09:33:23.045512Z","iopub.execute_input":"2023-07-27T09:33:23.045917Z","iopub.status.idle":"2023-07-27T09:33:23.186603Z","shell.execute_reply.started":"2023-07-27T09:33:23.045883Z","shell.execute_reply":"2023-07-27T09:33:23.185375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 配列を２分割\ndataset_test, dataset_test2 = train_test_split(dataset, test_size=0.5, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T09:33:27.937748Z","iopub.execute_input":"2023-07-27T09:33:27.938165Z","iopub.status.idle":"2023-07-27T09:33:27.951676Z","shell.execute_reply.started":"2023-07-27T09:33:27.938130Z","shell.execute_reply":"2023-07-27T09:33:27.950657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータにノイズを入れてテスト数を増やす\ntest_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":"2023-07-27T09:33:29.487416Z","iopub.execute_input":"2023-07-27T09:33:29.487845Z","iopub.status.idle":"2023-07-27T09:33:29.638571Z","shell.execute_reply.started":"2023-07-27T09:33:29.487809Z","shell.execute_reply":"2023-07-27T09:33:29.637530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータに対して検証実行\npredictions = model_da.predict(x=test_generator, steps= len(test_generator), verbose=0)\ncm = confusion_matrix(y_true=test_generator.classes, y_pred=np.argmax(predictions, axis=-1))","metadata":{"execution":{"iopub.status.busy":"2023-07-27T09:33:31.183411Z","iopub.execute_input":"2023-07-27T09:33:31.183791Z","iopub.status.idle":"2023-07-27T09:34:10.141128Z","shell.execute_reply.started":"2023-07-27T09:33:31.183759Z","shell.execute_reply":"2023-07-27T09:34:10.140110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 手sとデータの結果のグラフ\ndef 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":"2023-07-27T09:34:59.515529Z","iopub.execute_input":"2023-07-27T09:34:59.516486Z","iopub.status.idle":"2023-07-27T09:34:59.527844Z","shell.execute_reply.started":"2023-07-27T09:34:59.516448Z","shell.execute_reply":"2023-07-27T09:34:59.526679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果の混同行列をプロット\ncm_plot_labels = ['0_cat', '1_dog']\n\nplot_confusion_matrix(cm=cm, classes=cm_plot_labels, title='Confusion Matrix')","metadata":{"execution":{"iopub.status.busy":"2023-07-27T09:35:02.908215Z","iopub.execute_input":"2023-07-27T09:35:02.908978Z","iopub.status.idle":"2023-07-27T09:35:03.241462Z","shell.execute_reply.started":"2023-07-27T09:35:02.908938Z","shell.execute_reply":"2023-07-27T09:35:03.240485Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-27T08:49:05.659614Z","iopub.execute_input":"2023-07-27T08:49:05.659989Z","iopub.status.idle":"2023-07-27T08:49:05.668280Z","shell.execute_reply.started":"2023-07-27T08:49:05.659949Z","shell.execute_reply":"2023-07-27T08:49:05.666913Z"},"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)\nprint(results)","metadata":{"execution":{"iopub.status.busy":"2023-07-27T09:18:26.447687Z","iopub.execute_input":"2023-07-27T09:18:26.448107Z","iopub.status.idle":"2023-07-27T09:18:26.496441Z","shell.execute_reply.started":"2023-07-27T09:18:26.448068Z","shell.execute_reply":"2023-07-27T09:18:26.495260Z"},"trusted":true},"execution_count":null,"outputs":[]}]}