{"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-01-20T23:16:09.753323Z","iopub.execute_input":"2023-01-20T23:16:09.753863Z","iopub.status.idle":"2023-01-20T23:16:09.768517Z","shell.execute_reply.started":"2023-01-20T23:16:09.753818Z","shell.execute_reply":"2023-01-20T23:16:09.767013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Le CIFAR-10 est un jeu de données d'apprentissage automatique populaire qui \ncontient 60 000 images en niveaux de gris de dimensions 32x32 réparties en 10 \nclasses, avec 6000 images par classe**","metadata":{}},{"cell_type":"code","source":"# Load the CIFAR-10 dataset\nfrom keras.datasets import cifar10\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n\n# Plot the first 10 images from the training set\nimport matplotlib.pyplot as plt\nfig, axes = plt.subplots(2, 5, figsize=(10, 5))\naxes = axes.ravel()\nfor i in range(10):\n    axes[i].imshow(x_train[i])\n    axes[i].set_title(y_train[i])\n    axes[i].axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:16:09.772301Z","iopub.execute_input":"2023-01-20T23:16:09.772690Z","iopub.status.idle":"2023-01-20T23:16:11.329445Z","shell.execute_reply.started":"2023-01-20T23:16:09.772650Z","shell.execute_reply":"2023-01-20T23:16:11.328639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PCA (Analyse en Composantes Principales) est une méthode de réduction de \ndimensionnalité non supervisée qui permet de trouver les directions principales \ndans lesquelles les données varient le plus. Il est souvent utilisé pour réduire \nla dimensionnalité des données d'image en conservant les caractéristiques les \nplus informatives**","metadata":{}},{"cell_type":"markdown","source":"**STEPS:\n\n-Import the necessary libraries and load the CIFAR-10 dataset.\n-Normalize the pixel values of the images to be between 0 and 1.\n-Flatten the images from 32x32x3 to 3072x1.\n-Compute the covariance matrix of the data.\n-Compute the eigenvalues and eigenvectors of the covariance matrix.\n-Sort the eigenvectors by their corresponding eigenvalues in descending order.\n-Select the top k eigenvectors, where k is the number of dimensions you want to reduce the data to.\n-Project the data onto the new subspace defined by the top k eigenvectors.\nNote: This is the basic steps, you may want to check the shapes of the matrices, check if the data is imbalanced and deal with it, etc..**","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nimport numpy as np\n\n# Normalize pixel values\nx_train = x_train.astype('float32') / 255\n\n# Flatten images\nx_train_flattened = x_train.reshape(x_train.shape[0], -1)\n\n# Compute covariance matrix\ncov_matrix = np.cov(x_train_flattened, rowvar=False)\n\n# Compute eigenvalues and eigenvectors\neigenvalues, eigenvectors = np.linalg.eig(cov_matrix)\n\n# Sort eigenvectors by eigenvalues\nidx = eigenvalues.argsort()[::-1]\neigenvalues = eigenvalues[idx]\neigenvectors = eigenvectors[:,idx]\n\n# Select top k eigenvectors\nk = 50\ntop_k_eigenvectors = eigenvectors[:, :k]\n\n# Project data onto new subspace\nx_train_pca = x_train_flattened.dot(top_k_eigenvectors)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:16:11.331043Z","iopub.execute_input":"2023-01-20T23:16:11.331576Z","iopub.status.idle":"2023-01-20T23:17:20.081334Z","shell.execute_reply.started":"2023-01-20T23:16:11.331542Z","shell.execute_reply":"2023-01-20T23:17:20.078972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.scatter(x_train_pca[:, 0], x_train_pca[:, 1], c=y_train)\nplt.xlabel('LD1')\nplt.ylabel('LD2')\nplt.title('CIFAR-10 dataset in the LDA 2D space')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:17:20.084950Z","iopub.execute_input":"2023-01-20T23:17:20.088717Z","iopub.status.idle":"2023-01-20T23:17:21.267094Z","shell.execute_reply.started":"2023-01-20T23:17:20.088662Z","shell.execute_reply":"2023-01-20T23:17:21.265926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"**LDA (Linear Discriminant Analysis) is a technique for dimensionality reduction and classification. It is similar to PCA in that it tries to find a lower-dimensional representation of the data that captures the most important information. However, unlike PCA, LDA is a supervised technique that takes into account the class labels of the data. The goal of LDA is to find a lower-dimensional space in which the classes are as well separated as possible.**","metadata":{}},{"cell_type":"markdown","source":"**\nSTEPS :\n\nPrepare the data: LDA requires the data to be in a numerical format and the class labels should be known.\n\nCompute the mean vector for each class: LDA involves comparing the mean vectors of different classes.\n\nCompute the within-class scatter matrix and the between-class scatter matrix: The within-class scatter matrix is a measure of the spread of the data within each class, while the between-class scatter matrix is a measure of the spread of the data between the classes.\n\nCompute the eigenvectors and eigenvalues of the scatter matrices: LDA finds the eigenvectors of the scatter matrices that correspond to the largest eigenvalues. These eigenvectors are the linear discriminants.\n\nSelect the linear discriminants: Select the linear discriminants by selecting the eigenvectors corresponding to the highest eigenvalues.\n\nTransform the original data: Use the selected linear discriminants to transform the original data into a lower-dimensional space.\n\nVisualize the result: Visualize the transformed data using a scatter plot or other appropriate visualization technique.\n\nClassify the data: Once the data has been transformed into a lower-dimensional space, you can use a classifier such as k-NN or SVM to classify the data.**","metadata":{}},{"cell_type":"code","source":"from keras.datasets import cifar10\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n\n\n# reshape the data\nx_train = x_train.reshape(x_train.shape[0], 32*32*3)\nx_test = x_test.reshape(x_test.shape[0], 32*32*3)\n\n# normalize the data\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255\nx_test /= 255\n\n# create a LDA object\nlda = LinearDiscriminantAnalysis(n_components=3)\n\n# fit the LDA on the training set\nlda.fit(x_train, y_train)\n\n# transform the training and test sets\nx_train_lda = lda.transform(x_train)\nx_test_lda = lda.transform(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:17:21.270227Z","iopub.execute_input":"2023-01-20T23:17:21.270927Z","iopub.status.idle":"2023-01-20T23:20:48.561039Z","shell.execute_reply.started":"2023-01-20T23:17:21.270881Z","shell.execute_reply":"2023-01-20T23:20:48.559090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the training set\nplt.scatter(x_train_lda[:, 0], x_train_lda[:, 1], c=y_train)\nplt.xlabel('LD1')\nplt.ylabel('LD2')\nplt.title('CIFAR-10 dataset in the LDA 2D space')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:20:48.564716Z","iopub.execute_input":"2023-01-20T23:20:48.568313Z","iopub.status.idle":"2023-01-20T23:20:49.742395Z","shell.execute_reply.started":"2023-01-20T23:20:48.568261Z","shell.execute_reply":"2023-01-20T23:20:49.741562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**T-SNE**","metadata":{}},{"cell_type":"code","source":"from sklearn.manifold import TSNE\nfrom keras.datasets import cifar10\nimport matplotlib.pyplot as plt\n\n# Load the CIFAR-10 dataset\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\nX = x_train.reshape(x_train.shape[0], -1)\ny = y_train.reshape(-1)\n\n# Apply t-SNE\ntsne = TSNE(n_components=2, perplexity=30)\nX_tsne = tsne.fit_transform(X)\n\n# Visualize the t-SNE results\nplt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y, cmap='viridis')\nplt.colorbar()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:20:49.743626Z","iopub.execute_input":"2023-01-20T23:20:49.744151Z","iopub.status.idle":"2023-01-20T23:47:00.161202Z","shell.execute_reply.started":"2023-01-20T23:20:49.744095Z","shell.execute_reply":"2023-01-20T23:47:00.160372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**K-means**","metadata":{}},{"cell_type":"code","source":"# with TSNE\n\nfrom sklearn.cluster import KMeans\n\n# create a KMeans object\nkmeans = KMeans(n_clusters=10, random_state=0)\n\n# fit the KMeans on the t-SNE transformed data\nkmeans.fit(X_tsne)\n\n# predict the cluster labels\ny_predX_tsne = kmeans.predict(X_tsne)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:00.162535Z","iopub.execute_input":"2023-01-20T23:47:00.163023Z","iopub.status.idle":"2023-01-20T23:47:02.405127Z","shell.execute_reply.started":"2023-01-20T23:47:00.162984Z","shell.execute_reply":"2023-01-20T23:47:02.404295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y_predX_tsne)\nplt.xlabel('t-SNE component 1')\nplt.ylabel('t-SNE component 2')\nplt.title('CIFAR-10 dataset in the t-SNE 2D space with K-means clusters')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:02.406865Z","iopub.execute_input":"2023-01-20T23:47:02.407522Z","iopub.status.idle":"2023-01-20T23:47:03.561401Z","shell.execute_reply.started":"2023-01-20T23:47:02.407479Z","shell.execute_reply":"2023-01-20T23:47:03.560376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#LDA\nkmeans = KMeans(n_clusters=10, random_state=0)\nkmeans.fit(x_train_lda)\ny_predLDA = kmeans.predict(x_train_lda)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:03.563065Z","iopub.execute_input":"2023-01-20T23:47:03.563806Z","iopub.status.idle":"2023-01-20T23:47:09.731195Z","shell.execute_reply.started":"2023-01-20T23:47:03.563762Z","shell.execute_reply":"2023-01-20T23:47:09.729992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T00:21:28.229898Z","iopub.execute_input":"2023-01-21T00:21:28.230846Z","iopub.status.idle":"2023-01-21T00:22:01.999589Z","shell.execute_reply.started":"2023-01-21T00:21:28.230806Z","shell.execute_reply":"2023-01-21T00:22:01.998450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the training set\nplt.scatter(x_train_lda[:, 0], x_train_lda[:, 1], c=y_predLDA)\nplt.xlabel('LD1')\nplt.ylabel('LD2')\nplt.title('CIFAR-10 dataset in the LDA 2D space with K-means clusters')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:09.735436Z","iopub.execute_input":"2023-01-20T23:47:09.735755Z","iopub.status.idle":"2023-01-20T23:47:10.855126Z","shell.execute_reply.started":"2023-01-20T23:47:09.735727Z","shell.execute_reply":"2023-01-20T23:47:10.853985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA\nkmeans = KMeans(n_clusters=10, random_state=0)\nkmeans.fit(x_train_pca)\ny_predPCA = kmeans.predict(x_train_pca)","metadata":{"execution":{"iopub.status.busy":"2023-01-21T00:53:11.613186Z","iopub.execute_input":"2023-01-21T00:53:11.614032Z","iopub.status.idle":"2023-01-21T00:53:22.568199Z","shell.execute_reply.started":"2023-01-21T00:53:11.613995Z","shell.execute_reply":"2023-01-21T00:53:22.566922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(x_train_pca[:, 0], x_train_pca[:, 1], c=y_predPCA)\nplt.xlabel('PC1')\nplt.ylabel('PC2')\nplt.title('CIFAR-10 dataset in the PCA 2D space with K-means clusters')\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:21.865649Z","iopub.execute_input":"2023-01-20T23:47:21.865972Z","iopub.status.idle":"2023-01-20T23:47:23.011035Z","shell.execute_reply.started":"2023-01-20T23:47:21.865941Z","shell.execute_reply":"2023-01-20T23:47:23.009960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mpl_toolkits.mplot3d import Axes3D\n\n# Create a 3D figure\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\n\n# Create a scatter plot of the data, colored by cluster label\nax.scatter(x_train_pca[:, 0], x_train_pca[:, 1],x_train_pca[:,2], c=y_predPCA)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:23.012469Z","iopub.execute_input":"2023-01-20T23:47:23.013332Z","iopub.status.idle":"2023-01-20T23:47:24.209444Z","shell.execute_reply.started":"2023-01-20T23:47:23.013298Z","shell.execute_reply":"2023-01-20T23:47:24.208309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DBSCAN**","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN\n\n# Create a DBSCAN object with the desired number of clusters\ndbscan = DBSCAN()\n\n# Fit the DBSCAN object to the data\ndbscan.fit(X_tsne)\n\n# Get the cluster labels\nlabelsX_tsne = dbscan.labels_\n\n\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# Create a 3D figure\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\n\n# Create a scatter plot of the data, colored by cluster label\nax.scatter(X[:,0], X[:,1], X[:,2], c=labelsX_tsne)\n\n# Show the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:24.210778Z","iopub.execute_input":"2023-01-20T23:47:24.211139Z","iopub.status.idle":"2023-01-20T23:47:25.747623Z","shell.execute_reply.started":"2023-01-20T23:47:24.211082Z","shell.execute_reply":"2023-01-20T23:47:25.746386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN\n\n# Create a DBSCAN object with the desired number of clusters\ndbscan = DBSCAN()\n\n# Fit the DBSCAN object to the data\ndbscan.fit(x_train_lda)\n\n# Get the cluster labels\nlabelsLda = dbscan.labels_\n\n\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# Create a 3D figure\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\n\n# Create a scatter plot of the data, colored by cluster label\nax.scatter(X[:,0], X[:,1], X[:,2], c=labelsLda)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-20T23:47:25.749026Z","iopub.execute_input":"2023-01-20T23:47:25.749507Z","iopub.status.idle":"2023-01-20T23:47:29.342891Z","shell.execute_reply.started":"2023-01-20T23:47:25.749463Z","shell.execute_reply":"2023-01-20T23:47:29.341888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN\n\n# Create a DBSCAN object with the desired number of clusters\ndbscan = DBSCAN()\n\n# Fit the DBSCAN object to the data\ndbscan.fit(x_train_pca)\n\n# Get the cluster labels\nlabelsPca = dbscan.labels_\n\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# Create a 3D figure\nfig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\n\n# Create a scatter plot of the data, colored by cluster label\nax.scatter(X[:,0], X[:,1], X[:,2], c=labelsPca)\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-21T00:52:38.805193Z","iopub.execute_input":"2023-01-21T00:52:38.805610Z","iopub.status.idle":"2023-01-21T00:53:07.181801Z","shell.execute_reply.started":"2023-01-21T00:52:38.805575Z","shell.execute_reply":"2023-01-21T00:53:07.180562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Comparaison**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import silhouette_score\nfrom sklearn.metrics import davies_bouldin_score\n\n# silhouette_score(x_train_lda, y_predLDA)\n# ss_pca_kmeans = silhouette_score(x_train_lda, labelsPca)\nss_lda_kmeans = silhouette_score(x_train_lda, y_predLDA)\ndb_lda_kmeans = davies_bouldin_score(x_train_lda, y_predLDA)\nss_lda_dbscan = silhouette_score(x_train_lda, labelsLda)\ndb_lda_dbscan = davies_bouldin_score(x_train_lda, labelsLda)\n\nprint(ss_lda_kmeans)\nprint(db_lda_kmeans)\nprint(ss_lda_dbscan)\nprint(db_lda_dbscan)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T00:38:31.733489Z","iopub.execute_input":"2023-01-21T00:38:31.733911Z","iopub.status.idle":"2023-01-21T00:39:40.313586Z","shell.execute_reply.started":"2023-01-21T00:38:31.733874Z","shell.execute_reply":"2023-01-21T00:39:40.312162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_pca_kmeans = silhouette_score(x_train_pca, y_predPCA)\ndb_pca_kmeans = davies_bouldin_score(x_train_pca, y_predPCA)\n# ss_pca_dbscan = silhouette_score(x_train_pca, labelsPca)\n# db_pca_dbscan = davies_bouldin_score(x_train_pca, labelsPca)\nprint(ss_pca_kmeans)\nprint(db_pca_kmeans)\n# print(ss_pca_dbscan)\n# print(db_pca_dbscan)","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:05:34.448876Z","iopub.execute_input":"2023-01-21T01:05:34.449337Z","iopub.status.idle":"2023-01-21T01:06:13.202761Z","shell.execute_reply.started":"2023-01-21T01:05:34.449302Z","shell.execute_reply":"2023-01-21T01:06:13.201262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_tsne_kmeans = silhouette_score(X_tsne, y_predX_tsne)\ndb_tsne_kmeans = davies_bouldin_score(X_tsne, y_predX_tsne)\nss_tsne_dbscan = silhouette_score(X_tsne, labelsX_tsne)\ndb_tsne_dbscan = davies_bouldin_score(X_tsne, labelsX_tsne)\n\nprint(ss_tsne_kmeans)\nprint(db_tsne_kmeans)\nprint(ss_tsne_dbscan)\nprint(db_tsne_dbscan)","metadata":{"execution":{"iopub.status.busy":"2023-01-21T00:59:44.155093Z","iopub.execute_input":"2023-01-21T00:59:44.155515Z","iopub.status.idle":"2023-01-21T01:01:57.160687Z","shell.execute_reply.started":"2023-01-21T00:59:44.155480Z","shell.execute_reply":"2023-01-21T01:01:57.158416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Create a dataframe to store the results\nresults = pd.DataFrame(columns=['Model', 'Silhouette Score', 'Davies-Bouldin Index'])\n\n# Add the results for each model\nresults.loc[0] = ['LDA + KMeans', ss_lda_kmeans, db_lda_kmeans]\nresults.loc[1] = ['LDA + DBSCAN', ss_lda_dbscan, db_lda_dbscan]\nresults.loc[2] = ['PCA + KMeans', ss_pca_kmeans, db_pca_kmeans]\n# results.loc[3] = ['PCA + DBSCAN', ss_pca_dbscan, db_pca_dbscan]\nresults.loc[3] = ['TSNE + KMeans', ss_tsne_kmeans, db_tsne_kmeans]\nresults.loc[4] = ['TSNE + DBSCAN', ss_tsne_dbscan, db_tsne_dbscan]\n\n# Print the table\nprint(results)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:06:45.214480Z","iopub.execute_input":"2023-01-21T01:06:45.214907Z","iopub.status.idle":"2023-01-21T01:06:45.255420Z","shell.execute_reply.started":"2023-01-21T01:06:45.214871Z","shell.execute_reply":"2023-01-21T01:06:45.254142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nfig,ax=plt.subplots(nrows=1,ncols=2,figsize=(16,5))\n\nsns.barplot(data=results,x='Model',y='Silhouette Score',ax=ax[0])\nsns.barplot(data=results,x='Model',y='Davies-Bouldin Index',ax=ax[1])\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:07:17.235176Z","iopub.execute_input":"2023-01-21T01:07:17.235599Z","iopub.status.idle":"2023-01-21T01:07:17.839846Z","shell.execute_reply.started":"2023-01-21T01:07:17.235545Z","shell.execute_reply":"2023-01-21T01:07:17.839112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nn_images = 3 # Number of images to display per cluster\nfor i in range(10):\n    # get the indices of the data points in the current cluster\n    cluster_indices = np.where(y_predX_tsne == i)[0]\n    fig, axs = plt.subplots(1, n_images, figsize=(20, 5)) # Create a 1x3 grid of subplots\n    for j in range(n_images):\n        image = x_train[cluster_indices[j]]\n        axs[j].imshow(image)\n        axs[j].axis('off')\n    plt.suptitle(f'Cluster {i}')\n    plt.show()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:28:48.678232Z","iopub.execute_input":"2023-01-21T01:28:48.678770Z","iopub.status.idle":"2023-01-21T01:28:51.127595Z","shell.execute_reply.started":"2023-01-21T01:28:48.678732Z","shell.execute_reply":"2023-01-21T01:28:51.126485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nn_images = 3 # Number of images to display per cluster\nfor i in range(10):\n    # get the indices of the data points in the current cluster\n    cluster_indices = np.where(y_predPCA == i)[0]\n    fig, axs = plt.subplots(1, n_images, figsize=(20, 5)) # Create a 1x3 grid of subplots\n    for j in range(n_images):\n        image = x_train[cluster_indices[j]]\n        axs[j].imshow(image)\n        axs[j].axis('off')\n    plt.suptitle(f'Cluster {i}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:30:27.316404Z","iopub.execute_input":"2023-01-21T01:30:27.316806Z","iopub.status.idle":"2023-01-21T01:30:30.088184Z","shell.execute_reply.started":"2023-01-21T01:30:27.316774Z","shell.execute_reply":"2023-01-21T01:30:30.086945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_images = 3 # Number of images to display per cluster\nfor i in range(20):\n    # get the indices of the data points in the current cluster\n    cluster_indices = np.where(labelsX_tsne == i)[0]\n    fig, axs = plt.subplots(1, n_images, figsize=(20, 5)) # Create a 1x3 grid of subplots\n    for j in range(n_images):\n        image = x_train[cluster_indices[j]]\n        axs[j].imshow(image)\n        axs[j].axis('off')\n    plt.suptitle(f'Cluster {i}')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-21T01:31:28.813478Z","iopub.execute_input":"2023-01-21T01:31:28.813884Z","iopub.status.idle":"2023-01-21T01:31:34.024279Z","shell.execute_reply.started":"2023-01-21T01:31:28.813852Z","shell.execute_reply":"2023-01-21T01:31:34.023402Z"},"trusted":true},"execution_count":null,"outputs":[]}]}