{"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"},{"sourceId":7967935,"sourceType":"datasetVersion","datasetId":4688165}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-04-02T03:52:20.103469Z","iopub.execute_input":"2024-04-02T03:52:20.103996Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.cluster import KMeans\nimport plotly\nimport plotly.graph_objects as go\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:12:40.772897Z","iopub.execute_input":"2024-04-02T04:12:40.773386Z","iopub.status.idle":"2024-04-02T04:12:40.782834Z","shell.execute_reply.started":"2024-04-02T04:12:40.773350Z","shell.execute_reply":"2024-04-02T04:12:40.780581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn_extra.cluster import KMedoids","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:12:48.420200Z","iopub.execute_input":"2024-04-02T04:12:48.420650Z","iopub.status.idle":"2024-04-02T04:12:48.470891Z","shell.execute_reply.started":"2024-04-02T04:12:48.420615Z","shell.execute_reply":"2024-04-02T04:12:48.469451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-learn-extra","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:11:44.024196Z","iopub.execute_input":"2024-04-02T04:11:44.024701Z","iopub.status.idle":"2024-04-02T04:12:03.445237Z","shell.execute_reply.started":"2024-04-02T04:11:44.024663Z","shell.execute_reply":"2024-04-02T04:12:03.443918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\ndef create_data_dict():\n    data_dict = {}\n    cnt = 0\n\n    for filename in os.listdir(\"/kaggle/input/fruits-pixels-256-256-3\"):\n        filepath = os.path.join(\"/kaggle/input/fruits-pixels-256-256-3\", filename)\n        data = np.load(filepath)\n        for i in range(data.shape[0]):\n            label = filename.split(\"_\")[0]\n            pattern = r\"(rotten|fresh)([a-z]+)\"\n            match = re.match(pattern, label, flags=re.IGNORECASE)\n\n            if match:\n                freshness, fruit = match.groups()\n            else:\n                freshness = \"\" \n                fruit = label\n\n            folder_path = os.path.join(\"/kaggle/input/csc532-fruits-categorical-classification-2024/csc532_2024_train/train\", label)\n            image_path = os.path.join(folder_path, os.listdir(folder_path)[0])\n            data_dict[cnt] = {\"freshness\": freshness, \"fruit\": fruit, \"image_path\": image_path} \n            cnt += 1\n            \n    return data_dict","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_dict = create_data_dict()\n# df = pd.DataFrame(data_dict)\n# df = df.T","metadata":{"execution":{"iopub.status.idle":"2024-04-02T03:52:24.004122Z","shell.execute_reply.started":"2024-04-02T03:52:23.992692Z","shell.execute_reply":"2024-04-02T03:52:24.002937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef visualize_image(image, title=None):\n  \"\"\"Visualizes an image using matplotlib.\n\n  Args:\n      image: A NumPy array representing the image data.\n      title: Optional title for the plot (default: None).\n  \"\"\"\n\n  plt.figure()\n  if len(image.shape) == 3:  # Check for RGB image\n    plt.imshow(image)\n  else:\n    plt.imshow(image, cmap='gray')  # Grayscale for non-RGB images\n\n  plt.axis('off')  # Hide axes for cleaner visualization\n  if title:\n    plt.title(title)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:52:24.006279Z","iopub.execute_input":"2024-04-02T03:52:24.006694Z","iopub.status.idle":"2024-04-02T03:52:24.020155Z","shell.execute_reply.started":"2024-04-02T03:52:24.006652Z","shell.execute_reply":"2024-04-02T03:52:24.019008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Pipeline:\n    def __init__(self, folder_path, n_components=2, n_clusters=2, clusterMethod=\"kmeans\", \n                 shuffle=True, sampleRatio=0.25, random_state=42,) -> None:\n        self.folder_path = folder_path\n        self.n_components = n_components\n        self.n_clusters = n_clusters\n        self.clusterMethod = clusterMethod\n        self.shuffle = shuffle\n        self.sampleRatio = sampleRatio\n        self.random_state = random_state\n        self.data = None\n        self.reduced_data = None\n        self.shuffled_data = None\n        self.centroids = None\n        self.labels = None\n            \n    def visualize_image(self, image_array) -> None:\n      plt.imshow(image_array)\n      plt.axis('off')  # Hide axes for cleaner visualization (optional)\n      plt.show()\n    \n    def load_and_merge_data(self) -> None:\n      merged_data = None\n      with tqdm(total=len(os.listdir(self.folder_path)), desc=\"Loading Data Files\") as pbar:  # Use tqdm for progress bar\n        for filename in os.listdir(self.folder_path):\n          if filename.endswith(\".npy\"):\n            try:\n              file_path = os.path.join(self.folder_path, filename)\n              data = np.load(file_path)\n              if merged_data is None:\n                merged_data = data\n              else:\n                merged_data = np.concatenate((merged_data, data), axis=0)\n              pbar.update(1)  # Update progress bar for each loaded file\n              print(f\"Loaded data from '{filename}'.\")\n            except FileNotFoundError:\n              print(f\"Error: File '{filename}' not found.\")\n        self.data = merged_data\n        if self.shuffle:\n            self.shuffle_and_pick()\n        print(\"Data loaded successfully!\")\n        # return merged_data\n\n    def shuffle_and_pick(self):\n      shuffled_data = shuffle(self.data)  # Shuffle the data\n      subset_size = int(len(self.data) * self.sampleRatio)  # Calculate subset size based on percentage\n      self.shuffled_data = shuffled_data[:subset_size]\n      # return shuffled_data[:subset_size]  # Pick the first 'subset_size' elements\n\n\n    def reduce_dim_and_scale(self):\n      data = self.shuffled_data\n      scaler = StandardScaler()  # Initialize scaler for standardization\n      scaled_data = scaler.fit_transform(data.reshape(-1, data.shape[1] * data.shape[2] * data.shape[3]))  # Reshape and scale\n      pca = PCA(n_components=self.n_components)\n      reduced_data = pca.fit_transform(scaled_data)\n      self.reduced_data = reduced_data\n      # return reduced_data\n\n\n    def cluster_data(self):\n      data = self.reduced_data\n      if self.clusterMethod.lower() == \"kmeans\":\n        clusterer = KMeans(n_clusters=self.n_clusters, random_state=42)\n      elif self.clusterMethod.lower() == \"kmedoids\":\n        clusterer = KMedoids(n_clusters=self.n_clusters, random_state=42)\n      else:\n        raise ValueError(f\"Invalid clustering algorithm: {self.clusterMethod}\")\n\n      clusterer.fit(data)\n      labels = clusterer.labels_\n\n      if hasattr(clusterer, 'cluster_centers_'):\n        centroids = clusterer.cluster_centers_\n      else:\n        centroids = clusterer.medoids_\n\n      self.labels = labels\n      self.centroids = centroids\n      # return labels, centroids\n\n\n    def visualize_clusters(self) -> go.Figure:\n      data = self.reduced_data\n      unique_labels = list(set(self.labels))\n\n      traces = []\n      default_colors = plotly.colors.DEFAULT_PLOTLY_COLORS  # Access default colors\n\n      for i, label in enumerate(unique_labels):\n        cluster_data = data[self.labels == label]  # Filter data for each cluster\n        trace = go.Scatter3d(\n            x=cluster_data[:, 0],\n            y=cluster_data[:, 1],\n            z=cluster_data[:, 2],\n            mode='markers',\n            name=f\"Cluster {label}\",\n            marker=dict(\n                size=8,\n                color=default_colors[i % len(default_colors)],  # Use default colors cyclically\n            ),\n        )\n        traces.append(trace)\n\n      # Create the layout with title\n      layout = go.Layout(\n          title=\"K-Means Clustering Visualization (3D)\",\n          scene=dict(\n              xaxis_title=\"Component 1\",\n              yaxis_title=\"Component 2\",\n              zaxis_title=\"Component 3\",\n          ),\n      )\n      fig = go.Figure(data=traces, layout=layout)\n      fig.show()\n    \n    \n    def visualize_distribution(self) -> go.Figure:\n      unique_labels = list(set(self.labels))\n      label_counts = []\n        \n      for label in unique_labels:\n        label_counts.append(np.sum(self.labels == label))  # Count occurrences of each label\n\n      trace = go.Pie(\n          labels=unique_labels,\n          values=label_counts,\n          textinfo=\"percent+label\",  # Show both percentage and label on hover\n          hole=0.4,  # Adjust hole size for better readability (optional)\n      )\n      layout = go.Layout(\n          title=\"Distribution of Data Points in Clusters\",\n      )\n      fig = go.Figure(data=[trace], layout=layout)\n      fig.show()  \n        \n        \n    def __str__(self) -> str:\n        return f\"\"\"This clustering pipeline is configured with the following parameters:\n        - Number of PCA components: {self.n_components}\n        - Number of clusters: {self.n_clusters}\n        - Clustering algorithm: {self.clusterMethod}\n        - Shuffle: {self.shuffle}\n        - Sample ratio: {self.sampleRatio}\n        - Random state: {self.random_state} (for reproducibility)\"\"\"\n","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:12:54.082433Z","iopub.execute_input":"2024-04-02T04:12:54.085213Z","iopub.status.idle":"2024-04-02T04:12:54.117973Z","shell.execute_reply.started":"2024-04-02T04:12:54.085169Z","shell.execute_reply":"2024-04-02T04:12:54.116730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Function to get sample indeces from each cluster**","metadata":{}},{"cell_type":"code","source":"def get_samples_indeces(data):\n  value_indices = {}\n  for i, item in enumerate(data):\n    if item not in value_indices:\n      value_indices[item] = []\n    value_indices[item].append(i)\n\n  for value, indices in value_indices.items():\n    sampled_indices = random.sample(indices, min(10, len(indices)))\n    value_indices[value] = sampled_indices  # Update with sampled indices\n  return value_indices","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:52:46.262277Z","iopub.execute_input":"2024-04-02T03:52:46.263253Z","iopub.status.idle":"2024-04-02T03:52:46.269867Z","shell.execute_reply.started":"2024-04-02T03:52:46.263215Z","shell.execute_reply":"2024-04-02T03:52:46.269033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Function to visualize the samples clusters**","metadata":{}},{"cell_type":"code","source":"def visualize_samples_n_2(images_class_0, images_class_1):\n    total_images_per_class = min(len(images_class_0), len(images_class_1))\n    rows = total_images_per_class \n    cols = 2 \n\n    fig, axes = plt.subplots(rows, cols, figsize=(10, 15))\n    class_image_index = {0: 0, 1: 0}\n\n    for i in range(rows):\n      for j in range(cols):\n        if class_image_index[j] >= total_images_per_class:\n          continue\n\n        image = None\n        class_label = None\n        if j == 0:\n          image = images_class_0[class_image_index[j]]\n          class_label = \"Class 0\"\n        else:\n          image = images_class_1[class_image_index[j]]\n          class_label = \"Class 1\"\n\n        axes[i, j].imshow(image)\n        axes[i, j].set_xticks([])\n        axes[i, j].set_yticks([])\n        axes[i, j].set_title(f\"{class_label} - Image {class_image_index[j] + 1}\")\n        class_image_index[j] += 1 \n\n    fig.suptitle(f\"Visualization of Images by Class (Grid: {rows}x{cols})\")\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:54:08.765725Z","iopub.execute_input":"2024-04-02T04:54:08.766293Z","iopub.status.idle":"2024-04-02T04:54:08.779241Z","shell.execute_reply.started":"2024-04-02T04:54:08.766254Z","shell.execute_reply":"2024-04-02T04:54:08.777742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_samples_n_3(images_class_0, images_class_1, images_class_2):\n    total_images_per_class = min(len(images_class_0), len(images_class_1), len(images_class_2))  \n    rows = total_images_per_class  \n    cols = 3  \n\n    fig, axes = plt.subplots(rows, cols, figsize=(12, 15))  \n    class_image_index = {0: 0, 1: 0, 2: 0}\n\n    for i in range(rows):\n      for j in range(cols):\n        if class_image_index[j] >= total_images_per_class:\n          continue\n\n        image = None\n        class_label = None\n        if j == 0:\n          image = images_class_0[class_image_index[j]]\n          class_label = \"Class 0\"\n        elif j == 1:\n          image = images_class_1[class_image_index[j]]\n          class_label = \"Class 1\"\n        else:\n          image = images_class_2[class_image_index[j]]\n          class_label = \"Class 2\"\n\n        axes[i, j].imshow(image)\n        axes[i, j].set_xticks([])\n        axes[i, j].set_yticks([])\n        axes[i, j].set_title(f\"{class_label} - Image {class_image_index[j] + 1}\")\n        class_image_index[j] += 1  \n\n    fig.suptitle(f\"Visualization of Images by Class (Grid: {rows}x{cols})\")\n    plt.tight_layout()\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **N Clusters = 2**","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/fruits-pixels-256-256-3\"\np1 = Pipeline(folder_path=path, n_components=3, n_clusters=2, clusterMethod=\"kmeans\", shuffle=True, sampleRatio=0.2)\nprint(p1)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:52:47.645278Z","iopub.execute_input":"2024-04-02T03:52:47.645679Z","iopub.status.idle":"2024-04-02T03:52:47.652110Z","shell.execute_reply.started":"2024-04-02T03:52:47.645648Z","shell.execute_reply":"2024-04-02T03:52:47.650637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.load_and_merge_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:52:48.778296Z","iopub.execute_input":"2024-04-02T03:52:48.778686Z","iopub.status.idle":"2024-04-02T03:53:15.644920Z","shell.execute_reply.started":"2024-04-02T03:52:48.778658Z","shell.execute_reply":"2024-04-02T03:53:15.643153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.reduce_dim_and_scale()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:53:15.647407Z","iopub.execute_input":"2024-04-02T03:53:15.647933Z","iopub.status.idle":"2024-04-02T03:53:39.598507Z","shell.execute_reply.started":"2024-04-02T03:53:15.647891Z","shell.execute_reply":"2024-04-02T03:53:39.597382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.cluster_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:53:39.599872Z","iopub.execute_input":"2024-04-02T03:53:39.600255Z","iopub.status.idle":"2024-04-02T03:53:39.709839Z","shell.execute_reply.started":"2024-04-02T03:53:39.600225Z","shell.execute_reply":"2024-04-02T03:53:39.708683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.visualize_clusters()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:53:39.716136Z","iopub.execute_input":"2024-04-02T03:53:39.719167Z","iopub.status.idle":"2024-04-02T03:53:40.241530Z","shell.execute_reply.started":"2024-04-02T03:53:39.719120Z","shell.execute_reply":"2024-04-02T03:53:40.240253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1.visualize_distribution()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:53:40.242800Z","iopub.execute_input":"2024-04-02T03:53:40.243155Z","iopub.status.idle":"2024-04-02T03:53:40.278526Z","shell.execute_reply.started":"2024-04-02T03:53:40.243126Z","shell.execute_reply":"2024-04-02T03:53:40.277307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nsample_indexes_p1 = get_samples_indeces(p1.labels)\nsample_indexes_p1","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:56:19.200731Z","iopub.execute_input":"2024-04-02T03:56:19.201195Z","iopub.status.idle":"2024-04-02T03:56:19.212376Z","shell.execute_reply.started":"2024-04-02T03:56:19.201165Z","shell.execute_reply":"2024-04-02T03:56:19.211142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p1_cluster_0 = p1.shuffled_data[sample_indexes_p1[0]]\np1_cluster_1 = p1.shuffled_data[sample_indexes_p1[1]]","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:56:20.413029Z","iopub.execute_input":"2024-04-02T03:56:20.413489Z","iopub.status.idle":"2024-04-02T03:56:20.421447Z","shell.execute_reply.started":"2024-04-02T03:56:20.413457Z","shell.execute_reply":"2024-04-02T03:56:20.419770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualize the samples from each cluster**","metadata":{}},{"cell_type":"code","source":"visualize_samples_n_2(p1_cluster_0, p1_cluster_1)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:59:45.814825Z","iopub.execute_input":"2024-04-02T04:59:45.815317Z","iopub.status.idle":"2024-04-02T04:59:47.416719Z","shell.execute_reply.started":"2024-04-02T04:59:45.815287Z","shell.execute_reply":"2024-04-02T04:59:47.415300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **N Clusters = 3**","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/fruits-pixels-256-256-3\"\np2 = Pipeline(folder_path=path, n_components=3, n_clusters=3, clusterMethod=\"kmeans\", shuffle=True, sampleRatio=0.5)\nprint(p2)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:56:31.314611Z","iopub.execute_input":"2024-04-02T03:56:31.315085Z","iopub.status.idle":"2024-04-02T03:56:31.322699Z","shell.execute_reply.started":"2024-04-02T03:56:31.315033Z","shell.execute_reply":"2024-04-02T03:56:31.321422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2.load_and_merge_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:56:31.774985Z","iopub.execute_input":"2024-04-02T03:56:31.775444Z","iopub.status.idle":"2024-04-02T03:56:36.243020Z","shell.execute_reply.started":"2024-04-02T03:56:31.775412Z","shell.execute_reply":"2024-04-02T03:56:36.241866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2.reduce_dim_and_scale()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:56:36.244782Z","iopub.execute_input":"2024-04-02T03:56:36.245109Z","iopub.status.idle":"2024-04-02T03:57:40.387959Z","shell.execute_reply.started":"2024-04-02T03:56:36.245083Z","shell.execute_reply":"2024-04-02T03:57:40.386881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2.cluster_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:40.389131Z","iopub.execute_input":"2024-04-02T03:57:40.389484Z","iopub.status.idle":"2024-04-02T03:57:41.870728Z","shell.execute_reply.started":"2024-04-02T03:57:40.389457Z","shell.execute_reply":"2024-04-02T03:57:41.869751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2.visualize_clusters()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:41.876423Z","iopub.execute_input":"2024-04-02T03:57:41.879541Z","iopub.status.idle":"2024-04-02T03:57:41.906578Z","shell.execute_reply.started":"2024-04-02T03:57:41.879494Z","shell.execute_reply":"2024-04-02T03:57:41.905359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2.visualize_distribution()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:41.908129Z","iopub.execute_input":"2024-04-02T03:57:41.908535Z","iopub.status.idle":"2024-04-02T03:57:41.926278Z","shell.execute_reply.started":"2024-04-02T03:57:41.908501Z","shell.execute_reply":"2024-04-02T03:57:41.924905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_indexes_p2 = get_samples_indeces(p2.labels)\nsample_indexes_p2","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:41.928141Z","iopub.execute_input":"2024-04-02T03:57:41.928726Z","iopub.status.idle":"2024-04-02T03:57:41.941381Z","shell.execute_reply.started":"2024-04-02T03:57:41.928694Z","shell.execute_reply":"2024-04-02T03:57:41.939994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p2_cluster_0 = p2.shuffled_data[sample_indexes_p2[0]]\np2_cluster_1 = p2.shuffled_data[sample_indexes_p2[1]]\np2_cluster_2 = p2.shuffled_data[sample_indexes_p2[2]]","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:41.942671Z","iopub.execute_input":"2024-04-02T03:57:41.943279Z","iopub.status.idle":"2024-04-02T03:57:41.950764Z","shell.execute_reply.started":"2024-04-02T03:57:41.943248Z","shell.execute_reply":"2024-04-02T03:57:41.949620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Visualize the samples from each cluster**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimages_class_0 = p2_cluster_0  \nimages_class_1 = p2_cluster_1  \nimages_class_2 = p2_cluster_2  \n\ntotal_images_per_class = min(len(images_class_0), len(images_class_1), len(images_class_2))  \nrows = total_images_per_class  \ncols = 3  \n\nfig, axes = plt.subplots(rows, cols, figsize=(12, 15))  \nclass_image_index = {0: 0, 1: 0, 2: 0}\n\nfor i in range(rows):\n  for j in range(cols):\n    if class_image_index[j] >= total_images_per_class:\n      continue\n\n    image = None\n    class_label = None\n    if j == 0:\n      image = images_class_0[class_image_index[j]]\n      class_label = \"Class 0\"\n    elif j == 1:\n      image = images_class_1[class_image_index[j]]\n      class_label = \"Class 1\"\n    else:\n      image = images_class_2[class_image_index[j]]\n      class_label = \"Class 2\"\n\n    axes[i, j].imshow(image)\n    axes[i, j].set_xticks([])\n    axes[i, j].set_yticks([])\n    axes[i, j].set_title(f\"{class_label} - Image {class_image_index[j] + 1}\")\n    class_image_index[j] += 1  \n\nfig.suptitle(f\"Visualization of Images by Class (Grid: {rows}x{cols})\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:57:41.952203Z","iopub.execute_input":"2024-04-02T03:57:41.952511Z","iopub.status.idle":"2024-04-02T03:57:44.309527Z","shell.execute_reply.started":"2024-04-02T03:57:41.952486Z","shell.execute_reply":"2024-04-02T03:57:44.308446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **N Clusters = 6**","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/fruits-pixels-256-256-3\"\np3 = Pipeline(folder_path=path, n_components=3, n_clusters=6, clusterMethod=\"kmeans\", shuffle=True, sampleRatio=0.1)\nprint(p3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.load_and_merge_data()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.shuffle_and_pick()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.reduce_dim_and_scale()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.cluster_data()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.visualize_clusters()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.reduced_data.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p3.visualize_distribution()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Kmedoids**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\ntf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T03:59:06.988301Z","iopub.execute_input":"2024-04-02T03:59:06.988714Z","iopub.status.idle":"2024-04-02T03:59:22.521022Z","shell.execute_reply.started":"2024-04-02T03:59:06.988685Z","shell.execute_reply":"2024-04-02T03:59:22.519794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/fruits-pixels-256-256-3\"\nk2 = Pipeline(folder_path=path, n_components=3, n_clusters=2, clusterMethod=\"kmedoids\", shuffle=True, sampleRatio=0.5)\nprint(k2)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:02:04.905100Z","iopub.execute_input":"2024-04-02T04:02:04.905994Z","iopub.status.idle":"2024-04-02T04:02:04.913544Z","shell.execute_reply.started":"2024-04-02T04:02:04.905963Z","shell.execute_reply":"2024-04-02T04:02:04.912254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2.load_and_merge_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:13:09.442887Z","iopub.execute_input":"2024-04-02T04:13:09.443781Z","iopub.status.idle":"2024-04-02T04:13:31.418292Z","shell.execute_reply.started":"2024-04-02T04:13:09.443728Z","shell.execute_reply":"2024-04-02T04:13:31.416781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2.reduce_dim_and_scale()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:13:31.420141Z","iopub.execute_input":"2024-04-02T04:13:31.420485Z","iopub.status.idle":"2024-04-02T04:14:35.926588Z","shell.execute_reply.started":"2024-04-02T04:13:31.420457Z","shell.execute_reply":"2024-04-02T04:14:35.924439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2.cluster_data()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:14:35.929372Z","iopub.execute_input":"2024-04-02T04:14:35.929820Z","iopub.status.idle":"2024-04-02T04:14:38.296095Z","shell.execute_reply.started":"2024-04-02T04:14:35.929783Z","shell.execute_reply":"2024-04-02T04:14:38.294463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2.visualize_clusters()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:14:38.299215Z","iopub.execute_input":"2024-04-02T04:14:38.299622Z","iopub.status.idle":"2024-04-02T04:14:38.333625Z","shell.execute_reply.started":"2024-04-02T04:14:38.299588Z","shell.execute_reply":"2024-04-02T04:14:38.331178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2.visualize_distribution()","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:14:38.335530Z","iopub.execute_input":"2024-04-02T04:14:38.336773Z","iopub.status.idle":"2024-04-02T04:14:38.354333Z","shell.execute_reply.started":"2024-04-02T04:14:38.336730Z","shell.execute_reply":"2024-04-02T04:14:38.352540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_indexes_k2 = get_samples_indeces(k2.labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:14:38.357177Z","iopub.execute_input":"2024-04-02T04:14:38.358013Z","iopub.status.idle":"2024-04-02T04:14:38.370856Z","shell.execute_reply.started":"2024-04-02T04:14:38.357961Z","shell.execute_reply":"2024-04-02T04:14:38.369101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k2_cluster_0 = k2.shuffled_data[sample_indexes_p1[0]]\nk2_cluster_1 = k2.shuffled_data[sample_indexes_p1[1]]","metadata":{"execution":{"iopub.status.busy":"2024-04-02T04:43:09.332890Z","iopub.execute_input":"2024-04-02T04:43:09.334125Z","iopub.status.idle":"2024-04-02T04:43:09.341501Z","shell.execute_reply.started":"2024-04-02T04:43:09.334068Z","shell.execute_reply":"2024-04-02T04:43:09.339951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_samples_n_2(k2_cluster_0, k2_cluster_1)","metadata":{"execution":{"iopub.status.busy":"2024-04-02T05:07:02.428181Z","iopub.execute_input":"2024-04-02T05:07:02.428844Z","iopub.status.idle":"2024-04-02T05:07:04.639524Z","shell.execute_reply.started":"2024-04-02T05:07:02.428800Z","shell.execute_reply":"2024-04-02T05:07:04.638322Z"},"trusted":true},"execution_count":null,"outputs":[]}]}