{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from utils import *\n", "warnings.filterwarnings('ignore')\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "fd_collection = getCollection(\"team_5_mwdb_phase_2\", \"fd_collection\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "#same function as 0a\n", "def pca_inherent_dimensionality(data, threshold):\n", " \n", " # Calculate the mean of the data\n", " mean = np.mean(data, axis=0)\n", " # Center the data by subtracting the mean\n", " centered_data = data - mean\n", " # Normalize the data\n", " normalized_data = centered_data / np.std(centered_data, axis=0)\n", "\n", " # Reshape the centered data to ensure compatible dimensions\n", " if(len(normalized_data.shape)==3):\n", " reshaped_normalized_data = normalized_data.reshape(normalized_data.shape[0], normalized_data.shape[2])\n", " else:\n", " reshaped_normalized_data=normalized_data\n", "\n", " # Calculate the covariance matrix\n", " covariance_matrix = np.dot(reshaped_normalized_data.T, reshaped_normalized_data) / (reshaped_normalized_data.shape[0] - 1)\n", "\n", " # Compute the eigenvalues and eigenvectors of the covariance matrix\n", " eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix)\n", "\n", " \n", " # Determine the number of eigenvalues greater than the threshold\n", " significant_eigenvalues = eigenvalues[eigenvalues > threshold]\n", " inherent_dimensionality = len(significant_eigenvalues)\n", "\n", " return inherent_dimensionality" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inherent dimensionality associated with label 0 : 140\n", "Inherent dimensionality associated with label 1 : 134\n", "Inherent dimensionality associated with label 2 : 75\n", "Inherent dimensionality associated with label 3 : 182\n", "Inherent dimensionality associated with label 4 : 27\n", "Inherent dimensionality associated with label 5 : 186\n", "Inherent dimensionality associated with label 6 : 20\n", "Inherent dimensionality associated with label 7 : 20\n", "Inherent dimensionality associated with label 8 : 22\n", "Inherent dimensionality associated with label 9 : 26\n", "Inherent dimensionality associated with label 10 : 22\n", "Inherent dimensionality associated with label 11 : 16\n", "Inherent dimensionality associated with label 12 : 63\n", "Inherent dimensionality associated with label 13 : 48\n", "Inherent dimensionality associated with label 14 : 20\n", "Inherent dimensionality associated with label 15 : 42\n", "Inherent dimensionality associated with label 16 : 44\n", "Inherent dimensionality associated with label 17 : 24\n", "Inherent dimensionality associated with label 18 : 21\n", "Inherent dimensionality associated with label 19 : 60\n", "Inherent dimensionality associated with label 20 : 23\n", "Inherent dimensionality associated with label 21 : 28\n", "Inherent dimensionality associated with label 22 : 30\n", "Inherent dimensionality associated with label 23 : 53\n", "Inherent dimensionality associated with label 24 : 22\n", "Inherent dimensionality associated with label 25 : 34\n", "Inherent dimensionality associated with label 26 : 35\n", "Inherent dimensionality associated with label 27 : 34\n", "Inherent dimensionality associated with label 28 : 24\n", "Inherent dimensionality associated with label 29 : 25\n", "Inherent dimensionality associated with label 30 : 27\n", "Inherent dimensionality associated with label 31 : 33\n", "Inherent dimensionality associated with label 32 : 25\n", "Inherent dimensionality associated with label 33 : 31\n", "Inherent dimensionality associated with label 34 : 33\n", "Inherent dimensionality associated with label 35 : 37\n", "Inherent dimensionality associated with label 36 : 31\n", "Inherent dimensionality associated with label 37 : 25\n", "Inherent dimensionality associated with label 38 : 31\n", "Inherent dimensionality associated with label 39 : 42\n", "Inherent dimensionality associated with label 40 : 32\n", "Inherent dimensionality associated with label 41 : 33\n", "Inherent dimensionality associated with label 42 : 21\n", "Inherent dimensionality associated with label 43 : 16\n", "Inherent dimensionality associated with label 44 : 16\n", "Inherent dimensionality associated with label 45 : 25\n", "Inherent dimensionality associated with label 46 : 48\n", "Inherent dimensionality associated with label 47 : 49\n", "Inherent dimensionality associated with label 48 : 20\n", "Inherent dimensionality associated with label 49 : 26\n", "Inherent dimensionality associated with label 50 : 43\n", "Inherent dimensionality associated with label 51 : 39\n", "Inherent dimensionality associated with label 52 : 15\n", "Inherent dimensionality associated with label 53 : 31\n", "Inherent dimensionality associated with label 54 : 42\n", "Inherent dimensionality associated with label 55 : 56\n", "Inherent dimensionality associated with label 56 : 29\n", "Inherent dimensionality associated with label 57 : 40\n", "Inherent dimensionality associated with label 58 : 38\n", "Inherent dimensionality associated with label 59 : 19\n", "Inherent dimensionality associated with label 60 : 32\n", "Inherent dimensionality associated with label 61 : 21\n", "Inherent dimensionality associated with label 62 : 19\n", "Inherent dimensionality associated with label 63 : 41\n", "Inherent dimensionality associated with label 64 : 15\n", "Inherent dimensionality associated with label 65 : 37\n", "Inherent dimensionality associated with label 66 : 27\n", "Inherent dimensionality associated with label 67 : 16\n", "Inherent dimensionality associated with label 68 : 19\n", "Inherent dimensionality associated with label 69 : 22\n", "Inherent dimensionality associated with label 70 : 18\n", "Inherent dimensionality associated with label 71 : 22\n", "Inherent dimensionality associated with label 72 : 25\n", "Inherent dimensionality associated with label 73 : 16\n", "Inherent dimensionality associated with label 74 : 28\n", "Inherent dimensionality associated with label 75 : 39\n", "Inherent dimensionality associated with label 76 : 28\n", "Inherent dimensionality associated with label 77 : 24\n", "Inherent dimensionality associated with label 78 : 19\n", "Inherent dimensionality associated with label 79 : 30\n", "Inherent dimensionality associated with label 80 : 19\n", "Inherent dimensionality associated with label 81 : 41\n", "Inherent dimensionality associated with label 82 : 27\n", "Inherent dimensionality associated with label 83 : 17\n", "Inherent dimensionality associated with label 84 : 31\n", "Inherent dimensionality associated with label 85 : 21\n", "Inherent dimensionality associated with label 86 : 42\n", "Inherent dimensionality associated with label 87 : 29\n", "Inherent dimensionality associated with label 88 : 31\n", "Inherent dimensionality associated with label 89 : 16\n", "Inherent dimensionality associated with label 90 : 42\n", "Inherent dimensionality associated with label 91 : 23\n", "Inherent dimensionality associated with label 92 : 42\n", "Inherent dimensionality associated with label 93 : 37\n", "Inherent dimensionality associated with label 94 : 111\n", "Inherent dimensionality associated with label 95 : 18\n", "Inherent dimensionality associated with label 96 : 28\n", "Inherent dimensionality associated with label 97 : 16\n", "Inherent dimensionality associated with label 98 : 27\n", "Inherent dimensionality associated with label 99 : 19\n", "Inherent dimensionality associated with label 100 : 29\n" ] } ], "source": [ "unique_labels=[]\n", "feature_space=[]\n", "unique_labels=fd_collection.distinct(\"true_label\")\n", "for i in range (len(unique_labels)):\n", " label_fds = [\n", " np.array(img_fds['fc_fd']).flatten() # get the specific feature model's feature vector\n", " for img_fds in fd_collection.find({\"true_label\": unique_labels[i]}) # repeat for all images\n", " ]\n", " threshold=0.5\n", " print(\"Inherent dimensionality associated with label\", unique_labels[i], \":\", pca_inherent_dimensionality(label_fds, threshold))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" } }, "nbformat": 4, "nbformat_minor": 2 }