{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CIFAR-10 Classification with CNN","metadata":{}},{"cell_type":"markdown","source":"## 1) Importing necessary libraries and data sets","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Input, Conv2D, MaxPooling2D, Dropout, BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.model_selection import train_test_split\n\nfrom warnings import filterwarnings\nfilterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:12:43.612228Z","iopub.execute_input":"2024-08-06T10:12:43.613153Z","iopub.status.idle":"2024-08-06T10:12:56.687930Z","shell.execute_reply.started":"2024-08-06T10:12:43.613114Z","shell.execute_reply":"2024-08-06T10:12:56.687132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:12:56.690136Z","iopub.execute_input":"2024-08-06T10:12:56.691123Z","iopub.status.idle":"2024-08-06T10:13:10.564168Z","shell.execute_reply.started":"2024-08-06T10:12:56.691084Z","shell.execute_reply":"2024-08-06T10:13:10.563356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2) Overview of data","metadata":{}},{"cell_type":"code","source":"print(\"Training Set Size: \", X_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:10.565419Z","iopub.execute_input":"2024-08-06T10:13:10.566041Z","iopub.status.idle":"2024-08-06T10:13:10.571118Z","shell.execute_reply.started":"2024-08-06T10:13:10.566006Z","shell.execute_reply":"2024-08-06T10:13:10.570190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Set Size: \", X_test.shape, y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:10.572293Z","iopub.execute_input":"2024-08-06T10:13:10.572566Z","iopub.status.idle":"2024-08-06T10:13:10.732939Z","shell.execute_reply.started":"2024-08-06T10:13:10.572542Z","shell.execute_reply":"2024-08-06T10:13:10.731964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_map = {0: \"Airplane\", \n              1: \"Automobile\", \n              2: \"Bird\", \n              3: \"Cat\", \n              4: \"Deer\", \n              5: \"Dog\", \n              6: \"Frog\", \n              7: \"Horse\", \n              8: \"Ship\", \n              9: \"Truck\"}","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:10.736156Z","iopub.execute_input":"2024-08-06T10:13:10.736566Z","iopub.status.idle":"2024-08-06T10:13:10.743081Z","shell.execute_reply.started":"2024-08-06T10:13:10.736529Z","shell.execute_reply":"2024-08-06T10:13:10.742185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First 10 images\nplt.figure(figsize=(10, 9))\nfor i in range(0, 10):\n    ax = plt.subplot(5, 5, i+1)\n    plt.title(f\"Label: {labels_map[y_train[i][0]]}\")\n    plt.imshow(X_train[i], cmap=\"gray\")\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:10.744312Z","iopub.execute_input":"2024-08-06T10:13:10.744668Z","iopub.status.idle":"2024-08-06T10:13:11.636058Z","shell.execute_reply.started":"2024-08-06T10:13:10.744628Z","shell.execute_reply":"2024-08-06T10:13:11.635222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Last 10 images\nplt.figure(figsize=(10, 9))\nfor i in range(0, 10):\n    ax = plt.subplot(5, 5, i+1)\n    plt.title(f\"Label: {labels_map[y_train[-i][0]]}\")\n    plt.imshow(X_train[-i], cmap=\"gray\")\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:11.637281Z","iopub.execute_input":"2024-08-06T10:13:11.637574Z","iopub.status.idle":"2024-08-06T10:13:12.208922Z","shell.execute_reply.started":"2024-08-06T10:13:11.637548Z","shell.execute_reply":"2024-08-06T10:13:12.208063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution of class numbers\ntrain_labels, train_counts = np.unique(y_train, return_counts=True)\ntest_labels, test_counts = np.unique(y_test, return_counts=True)\n\ntrain_class_number = pd.DataFrame({\"Labels\": train_labels, \"Counts\": train_counts})\ntest_class_number = pd.DataFrame({\"Labels\": test_labels, \"Counts\": test_counts})","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:12.210158Z","iopub.execute_input":"2024-08-06T10:13:12.210454Z","iopub.status.idle":"2024-08-06T10:13:12.219315Z","shell.execute_reply.started":"2024-08-06T10:13:12.210427Z","shell.execute_reply":"2024-08-06T10:13:12.218522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(13,4))\nsns.barplot(x=\"Labels\", y=\"Counts\", data=train_class_number, ax=ax[0])\nax[0].set_title(\"Distribution of the number of classes of the training dataset\")\nax[0].set_xlabel(\"Classes\")\nax[0].set_ylabel(\"Counts\")\n\nsns.barplot(x=\"Labels\", y=\"Counts\", data=test_class_number, ax=ax[1])\nax[1].set_title(\"Distribution of the number of classes of the test dataset\")\nax[1].set_xlabel(\"Classes\")\nax[1].set_ylabel(\"Counts\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T11:03:59.755042Z","iopub.execute_input":"2024-08-06T11:03:59.755795Z","iopub.status.idle":"2024-08-06T11:04:00.638866Z","shell.execute_reply.started":"2024-08-06T11:03:59.755762Z","shell.execute_reply":"2024-08-06T11:04:00.637893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3) Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"### 3.1) Encoding","metadata":{}},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)\ny_test = to_categorical(y_test, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:12.756630Z","iopub.execute_input":"2024-08-06T10:13:12.757006Z","iopub.status.idle":"2024-08-06T10:13:12.764495Z","shell.execute_reply.started":"2024-08-06T10:13:12.756973Z","shell.execute_reply":"2024-08-06T10:13:12.763443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0:5]","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:12.765729Z","iopub.execute_input":"2024-08-06T10:13:12.766095Z","iopub.status.idle":"2024-08-06T10:13:12.774842Z","shell.execute_reply.started":"2024-08-06T10:13:12.766066Z","shell.execute_reply":"2024-08-06T10:13:12.773769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.2) Changing data type","metadata":{}},{"cell_type":"code","source":"X_train = X_train.astype(\"float32\")\nX_test = X_test.astype(\"float32\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:12.776399Z","iopub.execute_input":"2024-08-06T10:13:12.776771Z","iopub.status.idle":"2024-08-06T10:13:12.979574Z","shell.execute_reply.started":"2024-08-06T10:13:12.776738Z","shell.execute_reply":"2024-08-06T10:13:12.978747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3.3) Standardization","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:12.980876Z","iopub.execute_input":"2024-08-06T10:13:12.981169Z","iopub.status.idle":"2024-08-06T10:13:13.207134Z","shell.execute_reply.started":"2024-08-06T10:13:12.981143Z","shell.execute_reply":"2024-08-06T10:13:13.206113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4) Data Augmentation","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(horizontal_flip=True,\n                             vertical_flip=True, \n                             fill_mode='nearest',\n                             zoom_range=0.1, # Görüntüyü yakınlaştırır.\n                             width_shift_range=0.1, # Yatay kaydırma\n                             height_shift_range=0.1) # Dikey kaydırma","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:13.210488Z","iopub.execute_input":"2024-08-06T10:13:13.210829Z","iopub.status.idle":"2024-08-06T10:13:13.215735Z","shell.execute_reply.started":"2024-08-06T10:13:13.210803Z","shell.execute_reply":"2024-08-06T10:13:13.214751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow(X_train, y_train, batch_size=256)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:13.217033Z","iopub.execute_input":"2024-08-06T10:13:13.217360Z","iopub.status.idle":"2024-08-06T10:13:13.226953Z","shell.execute_reply.started":"2024-08-06T10:13:13.217334Z","shell.execute_reply":"2024-08-06T10:13:13.225989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5) Modeling","metadata":{}},{"cell_type":"markdown","source":"### 5.1) Architectural Structure of the Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:13.228106Z","iopub.execute_input":"2024-08-06T10:13:13.228413Z","iopub.status.idle":"2024-08-06T10:13:13.241907Z","shell.execute_reply.started":"2024-08-06T10:13:13.228385Z","shell.execute_reply":"2024-08-06T10:13:13.240944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Input(shape=(32, 32, 3)))\n\nmodel.add(Conv2D(filters=32, kernel_size=(3,3), activation=\"relu\", padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(rate=0.25))\n\nmodel.add(Conv2D(filters=64, kernel_size=(3,3), activation=\"relu\", padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), activation=\"relu\", padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(rate=0.25))\n\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), activation=\"relu\", padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(units=256, activation=\"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(rate=0.5))\n\nmodel.add(Dense(units=128, activation=\"relu\"))\nmodel.add(BatchNormalization())\n\nmodel.add(Dense(units=10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:13.243499Z","iopub.execute_input":"2024-08-06T10:13:13.243821Z","iopub.status.idle":"2024-08-06T10:13:14.269015Z","shell.execute_reply.started":"2024-08-06T10:13:13.243796Z","shell.execute_reply":"2024-08-06T10:13:14.268213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:14.270130Z","iopub.execute_input":"2024-08-06T10:13:14.270420Z","iopub.status.idle":"2024-08-06T10:13:14.309599Z","shell.execute_reply.started":"2024-08-06T10:13:14.270394Z","shell.execute_reply":"2024-08-06T10:13:14.308743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",\n              optimizer=\"adam\",\n              metrics=[tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), \"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:14.310713Z","iopub.execute_input":"2024-08-06T10:13:14.310995Z","iopub.status.idle":"2024-08-06T10:13:14.332618Z","shell.execute_reply.started":"2024-08-06T10:13:14.310970Z","shell.execute_reply":"2024-08-06T10:13:14.331751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [EarlyStopping(monitor=\"val_loss\", \n                           patience=10, \n                           mode=\"min\", \n                           verbose=1, \n                           start_from_epoch=50), \n             ReduceLROnPlateau(monitor='val_loss', \n                               factor=0.5, \n                               patience=3, \n                               min_lr=1e-6), \n             ModelCheckpoint(filepath=\"best_model.keras\",\n                             monitor=\"val_loss\",\n                             mode=\"min\",\n                             save_best_only=True,\n                             save_weights_only=False,\n                             verbose=1)]","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:14.333657Z","iopub.execute_input":"2024-08-06T10:13:14.333976Z","iopub.status.idle":"2024-08-06T10:13:14.339630Z","shell.execute_reply.started":"2024-08-06T10:13:14.333951Z","shell.execute_reply":"2024-08-06T10:13:14.338746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.2) Model Fit","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_generator, \n                    epochs=100, \n                    validation_data=(X_test, y_test), \n                    callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:13:14.340759Z","iopub.execute_input":"2024-08-06T10:13:14.341035Z","iopub.status.idle":"2024-08-06T10:38:18.331634Z","shell.execute_reply.started":"2024-08-06T10:13:14.341011Z","shell.execute_reply":"2024-08-06T10:38:18.330819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6) Model Evaluation","metadata":{}},{"cell_type":"code","source":"# Accuracy ve Loss Grafikleri\n\n# Accuracy\nplt.figure(figsize=(20, 5))\nplt.subplot(1,2,1)\nplt.plot(history.history[\"accuracy\"], color=\"b\", label=\"Training Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], color=\"r\", label=\"Validation Accuracy\")\nplt.legend(loc=\"lower right\")\nplt.xlabel(\"Epoch\", fontsize=16)\nplt.ylabel(\"Accuracy\", fontsize=16)\nplt.ylim([min(plt.ylim()), 1])\nplt.title(\"Training and Test Accuracy Graph\", fontsize=16)\n\n# Loss\nplt.subplot(1,2,2)\nplt.plot(history.history[\"loss\"], color=\"b\", label=\"Training Loss\")\nplt.plot(history.history[\"val_loss\"], color=\"r\", label=\"Validation Loss\")\nplt.legend(loc=\"upper right\")\nplt.xlabel(\"Epoch\", fontsize=16)\nplt.ylabel(\"Loss\", fontsize=16)\nplt.ylim([0, max(plt.ylim())])\nplt.title(\"Training and Test Loss Graph\", fontsize=16)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:40:01.956732Z","iopub.execute_input":"2024-08-06T10:40:01.957463Z","iopub.status.idle":"2024-08-06T10:40:02.506624Z","shell.execute_reply.started":"2024-08-06T10:40:01.957428Z","shell.execute_reply":"2024-08-06T10:40:02.505720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, precision, recall, acc = model.evaluate(X_test, y_test, verbose=False)\n\nprint(f\"Test Accuracy: {round(acc * 100, 2)}\")\nprint(f\"Test Loss: {round(loss * 100, 2)}\")\nprint(f\"Test Precision: {round(precision * 100, 2)}\")\nprint(f\"Test Recall: {round(recall * 100, 2)}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:40:53.756655Z","iopub.execute_input":"2024-08-06T10:40:53.757487Z","iopub.status.idle":"2024-08-06T10:40:54.751582Z","shell.execute_reply.started":"2024-08-06T10:40:53.757441Z","shell.execute_reply":"2024-08-06T10:40:54.750554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.1) Loading Best Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n\nbest_model = load_model(\"best_model.keras\")","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:44:07.693513Z","iopub.execute_input":"2024-08-06T10:44:07.694177Z","iopub.status.idle":"2024-08-06T10:44:08.160794Z","shell.execute_reply.started":"2024-08-06T10:44:07.694138Z","shell.execute_reply":"2024-08-06T10:44:08.159943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.2) Prediction","metadata":{}},{"cell_type":"code","source":"y_pred = best_model.predict(X_test)\n\ny_pred_classes = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:44:52.214548Z","iopub.execute_input":"2024-08-06T10:44:52.214926Z","iopub.status.idle":"2024-08-06T10:44:54.384831Z","shell.execute_reply.started":"2024-08-06T10:44:52.214896Z","shell.execute_reply":"2024-08-06T10:44:54.383940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.3) Confusion Matrix","metadata":{}},{"cell_type":"code","source":"if len(y_test.shape) > 1 and y_test.shape[1] != 1:\n    y_test = np.argmax(y_test, axis=1)\n    \ny_test","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:45:44.458825Z","iopub.execute_input":"2024-08-06T10:45:44.459589Z","iopub.status.idle":"2024-08-06T10:45:44.467091Z","shell.execute_reply.started":"2024-08-06T10:45:44.459557Z","shell.execute_reply":"2024-08-06T10:45:44.466159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = np.vectorize(labels_map.get)(y_test)\n\ny_pred_classes = np.vectorize(labels_map.get)(y_pred_classes)","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:50:46.030503Z","iopub.execute_input":"2024-08-06T10:50:46.031352Z","iopub.status.idle":"2024-08-06T10:50:46.044229Z","shell.execute_reply.started":"2024-08-06T10:50:46.031315Z","shell.execute_reply":"2024-08-06T10:50:46.043312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:50:53.272762Z","iopub.execute_input":"2024-08-06T10:50:53.273144Z","iopub.status.idle":"2024-08-06T10:50:53.280196Z","shell.execute_reply.started":"2024-08-06T10:50:53.273113Z","shell.execute_reply":"2024-08-06T10:50:53.279172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_classes","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:51:03.073551Z","iopub.execute_input":"2024-08-06T10:51:03.073945Z","iopub.status.idle":"2024-08-06T10:51:03.080809Z","shell.execute_reply.started":"2024-08-06T10:51:03.073910Z","shell.execute_reply":"2024-08-06T10:51:03.079906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Benzersiz etiketleri bulma ve bir diziye dönüştürme\nlabels = np.unique(np.concatenate((y_test, y_pred_classes)))","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:54:56.170466Z","iopub.execute_input":"2024-08-06T10:54:56.170883Z","iopub.status.idle":"2024-08-06T10:54:56.178659Z","shell.execute_reply.started":"2024-08-06T10:54:56.170850Z","shell.execute_reply":"2024-08-06T10:54:56.177602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred_classes, labels=labels)\nplt.figure(figsize=(10,7))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=labels, yticklabels=labels)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:54:57.486989Z","iopub.execute_input":"2024-08-06T10:54:57.487355Z","iopub.status.idle":"2024-08-06T10:54:58.075150Z","shell.execute_reply.started":"2024-08-06T10:54:57.487326Z","shell.execute_reply":"2024-08-06T10:54:58.074157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.4) Classification Report","metadata":{}},{"cell_type":"code","source":"# Classification Report\n\nfrom sklearn.metrics import classification_report\n\nprint(classification_report(y_test, y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:55:36.124455Z","iopub.execute_input":"2024-08-06T10:55:36.124833Z","iopub.status.idle":"2024-08-06T10:55:36.258145Z","shell.execute_reply.started":"2024-08-06T10:55:36.124802Z","shell.execute_reply":"2024-08-06T10:55:36.257169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.5) Visualizing misclassified instances","metadata":{}},{"cell_type":"code","source":"# Visualize misclassified instances\nincorrect = np.where(y_pred_classes != y_test)[0]\nnum_incorrect = len(incorrect)\n\nplt.figure(figsize=(10, 8))\nfor i in range(min(num_incorrect, 36)):  # İlk 36 yanlış sınıflandırılan örneği göster\n    idx = incorrect[i]\n    plt.subplot(6, 6, i + 1)\n    plt.imshow(X_test[idx], cmap='gray')\n    plt.title(f\"T: {y_test[idx]}, P: {y_pred_classes[idx]}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T10:58:00.801102Z","iopub.execute_input":"2024-08-06T10:58:00.801472Z","iopub.status.idle":"2024-08-06T10:58:03.412004Z","shell.execute_reply.started":"2024-08-06T10:58:00.801442Z","shell.execute_reply":"2024-08-06T10:58:03.411117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6.6) Visualize correctly classified instances","metadata":{}},{"cell_type":"code","source":"correct = np.where(y_pred_classes == y_test)[0]\nnum_correct = len(incorrect)\n\nplt.figure(figsize=(10, 8))\nfor i in range(min(num_correct, 36)):  # İlk 36 yanlış sınıflandırılan örneği göster\n    idx = correct[i]\n    plt.subplot(6, 6, i + 1)\n    plt.imshow(X_test[idx], cmap='gray')\n    plt.title(f\"T: {y_test[idx]}, P: {y_pred_classes[idx]}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-06T11:02:19.284111Z","iopub.execute_input":"2024-08-06T11:02:19.285031Z","iopub.status.idle":"2024-08-06T11:02:21.435562Z","shell.execute_reply.started":"2024-08-06T11:02:19.284994Z","shell.execute_reply":"2024-08-06T11:02:21.434668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}