{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":"none","dataSources":[{"sourceId":70497,"databundleVersionId":7713244,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Reclass to only two class Fresh and Rotten  ","metadata":{}},{"cell_type":"code","source":"!mkdir -p /kaggle/working/train/fresh\n!mkdir -p /kaggle/working/train/rotten\n!mkdir -p /kaggle/working/test/fresh\n!mkdir -p /kaggle/working/test/rotten\n#/kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/freshapples\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/freshapples/* /kaggle/working/train/fresh\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/freshbanana/* /kaggle/working/train/fresh\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/freshoranges/* /kaggle/working/train/fresh\n#/kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/rottenapples\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/rottenapples/* /kaggle/working/train/rotten\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/rottenbanana/* /kaggle/working/train/rotten\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train/rottenoranges/* /kaggle/working/train/rotten\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:44:25.528717Z","iopub.execute_input":"2024-05-20T18:44:25.529287Z","iopub.status.idle":"2024-05-20T18:45:52.137532Z","shell.execute_reply.started":"2024-05-20T18:44:25.529242Z","shell.execute_reply":"2024-05-20T18:45:52.135743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/freshapples/* /kaggle/working/test/fresh\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/freshbanana/* /kaggle/working/test/fresh\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/freshoranges/* /kaggle/working/test/fresh\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/rottenapples/* /kaggle/working/test/rotten\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/rottenbanana/* /kaggle/working/test/rotten\n!cp -r /kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_test/test/rottenoranges/* /kaggle/working/test/rotten\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:49:19.823191Z","iopub.execute_input":"2024-05-20T18:49:19.823763Z","iopub.status.idle":"2024-05-20T18:49:30.855203Z","shell.execute_reply.started":"2024-05-20T18:49:19.823722Z","shell.execute_reply":"2024-05-20T18:49:30.853115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install opencv-python\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T17:19:48.439235Z","iopub.execute_input":"2024-05-20T17:19:48.439645Z","iopub.status.idle":"2024-05-20T17:20:05.849570Z","shell.execute_reply.started":"2024-05-20T17:19:48.439614Z","shell.execute_reply":"2024-05-20T17:20:05.848074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '/kaggle/working/train'\nTEST_PATH = '/kaggle/working/test'\n\nX_train_images, y_train = load_and_resize_images_from_folder(TRAIN_PATH)\nX_test_images, y_test = load_and_resize_images_from_folder(TEST_PATH)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:50:53.967314Z","iopub.execute_input":"2024-05-20T18:50:53.968090Z","iopub.status.idle":"2024-05-20T18:51:49.133503Z","shell.execute_reply.started":"2024-05-20T18:50:53.968035Z","shell.execute_reply":"2024-05-20T18:51:49.132200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2  # Make sure to import the cv2 library\nimport numpy as np\ndef load_and_resize_images_from_folder(folder, target_size=(224, 224)):\n    images = []\n    labels = []\n    for class_label in os.listdir(folder):\n        class_path = os.path.join(folder, class_label)\n        if os.path.isdir(class_path):\n            for filename in os.listdir(class_path):\n                img_path = os.path.join(class_path, filename)\n                img = cv2.imread(img_path,3)\n                img = cv2.resize(img, target_size)  # Resize the image\n                images.append(img)\n                labels.append(class_label)\n    return images, np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:52:45.728885Z","iopub.execute_input":"2024-05-20T18:52:45.729707Z","iopub.status.idle":"2024-05-20T18:52:45.739662Z","shell.execute_reply.started":"2024-05-20T18:52:45.729643Z","shell.execute_reply":"2024-05-20T18:52:45.738509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize the data set ","metadata":{"execution":{"iopub.status.busy":"2024-05-20T17:23:39.629683Z","iopub.execute_input":"2024-05-20T17:23:39.630709Z","iopub.status.idle":"2024-05-20T17:23:39.635980Z","shell.execute_reply.started":"2024-05-20T17:23:39.630660Z","shell.execute_reply":"2024-05-20T17:23:39.634739Z"}}},{"cell_type":"code","source":"import random\n#displaying the train data\nrandom_indices = random.sample(range(len(X_train_images)), 15)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:52:55.852457Z","iopub.execute_input":"2024-05-20T18:52:55.852922Z","iopub.status.idle":"2024-05-20T18:52:55.859396Z","shell.execute_reply.started":"2024-05-20T18:52:55.852889Z","shell.execute_reply":"2024-05-20T18:52:55.857874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the images\nplt.figure(figsize=(15, 8))\nfor i, index in enumerate(random_indices, 1):\n    plt.subplot(3,5, i)\n    plt.imshow(cv2.cvtColor(X_train_images[index], cv2.COLOR_BGR2RGB))\n    plt.title(f'class: {y_train[index]}')\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:00.251551Z","iopub.execute_input":"2024-05-20T18:53:00.251987Z","iopub.status.idle":"2024-05-20T18:53:02.436522Z","shell.execute_reply.started":"2024-05-20T18:53:00.251956Z","shell.execute_reply":"2024-05-20T18:53:02.435120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#displaying the train data\nrandom_indices_2 = random.sample(range(len(X_test_images)), 15)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:07.326861Z","iopub.execute_input":"2024-05-20T18:53:07.327298Z","iopub.status.idle":"2024-05-20T18:53:07.333660Z","shell.execute_reply.started":"2024-05-20T18:53:07.327265Z","shell.execute_reply":"2024-05-20T18:53:07.332057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 8))\nfor i, index in enumerate(random_indices_2, 1):\n    plt.subplot(3,5, i)\n    plt.imshow(cv2.cvtColor(X_test_images[index], cv2.COLOR_BGR2RGB))\n    plt.title(f'class: {y_test[index]}')\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:07.671241Z","iopub.execute_input":"2024-05-20T18:53:07.671642Z","iopub.status.idle":"2024-05-20T18:53:09.429120Z","shell.execute_reply.started":"2024-05-20T18:53:07.671610Z","shell.execute_reply":"2024-05-20T18:53:09.427295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert Data to Features(USE HISTOGRAM)\n","metadata":{}},{"cell_type":"code","source":"def compute_histogram_img(img_list, bins=(32,32,32)):\n    img_features = []\n    for img in img_list:\n        # Convert Color to BGR\n        bgr_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        # Calculate histogram color\n        hist = cv2.calcHist([bgr_img], [0, 1, 2], None, bins, [0, 256, 0, 256, 0, 256])\n        # Normalize Histogram\n        hist = cv2.normalize(hist, hist).flatten()\n        img_features.append(hist)\n    return np.array(img_features)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:36.678683Z","iopub.execute_input":"2024-05-20T18:53:36.679253Z","iopub.status.idle":"2024-05-20T18:53:36.688016Z","shell.execute_reply.started":"2024-05-20T18:53:36.679222Z","shell.execute_reply":"2024-05-20T18:53:36.686754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Computer Features\nX_train=compute_histogram_img(X_train_images)\nX_test=compute_histogram_img(X_test_images)\ny_train=np.where(y_train=='fresh',1,0)\ny_test=np.where(y_test=='fresh',1,0)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:38.176734Z","iopub.execute_input":"2024-05-20T18:53:38.177523Z","iopub.status.idle":"2024-05-20T18:53:49.810575Z","shell.execute_reply.started":"2024-05-20T18:53:38.177488Z","shell.execute_reply":"2024-05-20T18:53:49.809241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the training data into training and validation \nfrom sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:49.812873Z","iopub.execute_input":"2024-05-20T18:53:49.813606Z","iopub.status.idle":"2024-05-20T18:53:50.386835Z","shell.execute_reply.started":"2024-05-20T18:53:49.813565Z","shell.execute_reply":"2024-05-20T18:53:50.385397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape) \nprint(X_val.shape)\ny_train","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:53:50.388521Z","iopub.execute_input":"2024-05-20T18:53:50.388937Z","iopub.status.idle":"2024-05-20T18:53:50.398541Z","shell.execute_reply.started":"2024-05-20T18:53:50.388904Z","shell.execute_reply":"2024-05-20T18:53:50.397227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import classification_report\n#Build the model \nrandom_forest = RandomForestClassifier(n_estimators=100, random_state=0) \nrandom_forest.fit(X_train,y_train)\n\n#Make a prediction to prdict data \ny_pred = random_forest.predict(X_test)\n\n# Classification Report\nprint(classification_report(y_pred, y_test))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:00:40.990988Z","iopub.execute_input":"2024-05-20T19:00:40.991386Z","iopub.status.idle":"2024-05-20T19:01:08.399587Z","shell.execute_reply.started":"2024-05-20T19:00:40.991355Z","shell.execute_reply":"2024-05-20T19:01:08.397038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SHAP ","metadata":{"execution":{"iopub.status.busy":"2024-05-20T17:43:23.810996Z","iopub.execute_input":"2024-05-20T17:43:23.811434Z","iopub.status.idle":"2024-05-20T17:43:23.816283Z","shell.execute_reply.started":"2024-05-20T17:43:23.811403Z","shell.execute_reply":"2024-05-20T17:43:23.815355Z"}}},{"cell_type":"code","source":"!pip install shap","metadata":{"execution":{"iopub.status.busy":"2024-05-20T18:57:42.431595Z","iopub.execute_input":"2024-05-20T18:57:42.432134Z","iopub.status.idle":"2024-05-20T18:57:59.689031Z","shell.execute_reply.started":"2024-05-20T18:57:42.432101Z","shell.execute_reply":"2024-05-20T18:57:59.687388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shap\nimport matplotlib.pyplot as plt\n\n# load JS visualization code to notebook\nshap.initjs()\n\n# Create the explainer\nexplainer = shap.TreeExplainer(random_forest)\n\nshap_values = explainer.shap_values(X_test)\nprint(f'shap_values type: {type(shap_values)}')\nfor i, shap_val in enumerate(shap_values):\n    print(f'Shape of shap_values[{i}]: {shap_val.shape}')","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:25:20.776367Z","iopub.execute_input":"2024-05-20T19:25:20.776928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:05:33.611161Z","iopub.execute_input":"2024-05-20T19:05:33.611720Z","iopub.status.idle":"2024-05-20T19:05:33.620800Z","shell.execute_reply.started":"2024-05-20T19:05:33.611682Z","shell.execute_reply":"2024-05-20T19:05:33.619088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Variable Importance with Summary Plot","metadata":{}},{"cell_type":"code","source":"print(\"Grap with feature\")\nfigure = plt.figure()\nshap.summary_plot(shap_values, X_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:11:14.715149Z","iopub.execute_input":"2024-05-20T19:11:14.715633Z","iopub.status.idle":"2024-05-20T19:11:17.744915Z","shell.execute_reply.started":"2024-05-20T19:11:14.715601Z","shell.execute_reply":"2024-05-20T19:11:17.743387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Summary Plot on a Specific Label\n","metadata":{}},{"cell_type":"code","source":"print(\"Grap with feature\")\nfigure = plt.figure()\nshap.summary_plot(shap_values[1], X_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:13:01.620227Z","iopub.execute_input":"2024-05-20T19:13:01.621054Z","iopub.status.idle":"2024-05-20T19:13:04.433490Z","shell.execute_reply.started":"2024-05-20T19:13:01.621017Z","shell.execute_reply":"2024-05-20T19:13:04.432133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Grap with feature\")\nfigure = plt.figure()\nshap.summary_plot(shap_values, X_test)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length = len(X_test)\nprint(length)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:10:39.359845Z","iopub.execute_input":"2024-05-20T19:10:39.360855Z","iopub.status.idle":"2024-05-20T19:10:39.366223Z","shell.execute_reply.started":"2024-05-20T19:10:39.360820Z","shell.execute_reply":"2024-05-20T19:10:39.364929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Summary using LIME\ncan't do  ","metadata":{}},{"cell_type":"markdown","source":"# Model-specific methods\n","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier, plot_tree\ndt_clf = DecisionTreeClassifier(max_depth = 3, min_samples_leaf = 2)\ndt_clf.fit(X_train, y_train)\n\n# Predict on the test data and evaluate the model\ny_pred = dt_clf.predict(X_test)\n\nprint(classification_report(y_pred, y_test))","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:21:04.905939Z","iopub.execute_input":"2024-05-20T19:21:04.906910Z","iopub.status.idle":"2024-05-20T19:21:14.291963Z","shell.execute_reply.started":"2024-05-20T19:21:04.906838Z","shell.execute_reply":"2024-05-20T19:21:14.290289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# And the model decision-making process can be visualized from the code below:\n\n\n","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(25,20))\n_ = plot_tree(dt_clf,\n               \tfeature_names = shape_values,\n               \tclass_names = ['fresh','rotten'],\n               \tfilled=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-20T19:25:08.236100Z","iopub.execute_input":"2024-05-20T19:25:08.236546Z","iopub.status.idle":"2024-05-20T19:25:08.315866Z","shell.execute_reply.started":"2024-05-20T19:25:08.236512Z","shell.execute_reply":"2024-05-20T19:25:08.314124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}