### Install Documentation Requirements Source: https://github.com/lmcinnes/umap/blob/master/CONTRIBUTING.md Installs the necessary Python packages for building the documentation locally. Ensure you are in the project's root directory. ```bash pip install -r docs_requirements.txt ``` -------------------------------- ### Install UMAP from Source Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install the UMAP package in editable mode from the source directory using pip. ```bash python -m pip install -e . ``` -------------------------------- ### Install UMAP from Source (if needed) Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Document embedding using UMAP.ipynb Installs the umap-learn library. If a recent version is not available via pip/conda, it suggests installing from the master branch of the GitHub repository. ```python #!pip install umap-learn ``` -------------------------------- ### Install UMAP with TBB Optimizations Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install UMAP with TBB for additional CPU optimizations on x86 processors. ```bash pip install umap-learn[tbb] ``` -------------------------------- ### Install UMAP with Parametric UMAP (CPU) Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install UMAP with CPU-only Tensorflow dependencies for parametric UMAP functionality. ```bash pip install umap-learn[parametric_umap] ``` -------------------------------- ### Download and Prepare UMAP Source Source: https://github.com/lmcinnes/umap/blob/master/README.rst Download the UMAP source code using wget, unzip it, and navigate into the master directory for manual installation. ```bash wget https://github.com/lmcinnes/umap/archive/master.zip unzip master.zip rm master.zip cd umap-master ``` -------------------------------- ### Install Required Libraries Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Document embedding using UMAP.ipynb Installs essential Python libraries for data manipulation, machine learning, and visualization, including numpy, pandas, scikit-learn, and UMAP. ```python !pip install numpy scipy pandas scikit-learn datashader holoviews numba ``` -------------------------------- ### Install UMAP and Core Libraries Source: https://github.com/lmcinnes/umap/blob/master/notebooks/AnimatingUMAP.ipynb Installs the UMAP library along with essential data science packages: numpy for numerical operations, matplotlib for plotting, and seaborn for enhanced visualizations. This command ensures all necessary tools are available for UMAP implementation. ```bash !pip install numpy matplotlib seaborn umap-learn ``` -------------------------------- ### Install UMAP via Pip Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install the UMAP package using pip. Ensure that numba, sklearn, numpy, and scipy are already installed. This command installs the core UMAP library. ```bash pip install umap-learn ``` -------------------------------- ### ParametricUMAP Model Output and Training Log Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/04.0-parametric-umap-mnist-embedding-convnet-with-autoencoder-loss.ipynb Example output showing the initialized ParametricUMAP model configuration and the training progress log, including loss values per epoch. ```text Output: ParametricUMAP(autoencoder_loss=True, n_training_epochs=5, optimizer=, parametric_reconstruction=True, reconstruction_validation=array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])) Construct fuzzy simplicial set Sun Aug 16 18:25:21 2020 Finding Nearest Neighbors Sun Aug 16 18:25:21 2020 Building RP forest with 17 trees Sun Aug 16 18:25:25 2020 parallel NN descent for 16 iterations 0 / 16 1 / 16 2 / 16 3 / 16 4 / 16 Sun Aug 16 18:25:37 2020 Finished Nearest Neighbor Search Sun Aug 16 18:25:40 2020 Construct embedding Epoch 1/50 724/724 [==============================] - 14s 19ms/step - loss: 0.4622 - reconstruction_loss: 0.2297 - umap_loss: 0.2325 - val_loss: 0.2012 - val_reconstruction_loss: 0.2012 - val_umap_loss: 0.0000e+00 Epoch 2/50 724/724 [==============================] - 14s 19ms/step - loss: 0.3612 - reconstruction_loss: 0.1873 - umap_loss: 0.1739 - val_loss: 0.1897 - val_reconstruction_loss: 0.1897 - val_umap_loss: 0.0000e+00 Epoch 3/50 724/724 [==============================] - 13s 18ms/step - loss: 0.3373 - reconstruction_loss: 0.1791 - umap_loss: 0.1581 - val_loss: 0.1834 - val_reconstruction_loss: 0.1834 - val_umap_loss: 0.0000e+00 Epoch 4/50 724/724 [==============================] - 13s 18ms/step - loss: 0.3224 - reconstruction_loss: 0.1739 - umap_loss: 0.1484 - val_loss: 0.1802 - val_reconstruction_loss: 0.1802 - val_umap_loss: 0.0000e+00 Epoch 5/50 724/724 [==============================] - 14s 20ms/step - loss: 0.3112 - reconstruction_loss: 0.1701 - umap_loss: 0.1411 - val_loss: 0.1777 - val_reconstruction_loss: 0.1777 - val_umap_loss: 0.0000e+00 Epoch 6/50 724/724 [==============================] - 13s 18ms/step - loss: 0.3052 - reconstruction_loss: 0.1679 - umap_loss: 0.1373 - val_loss: 0.1759 - val_reconstruction_loss: 0.1759 - val_umap_loss: 0.0000e+00 Epoch 7/50 724/724 [==============================] - 13s 18ms/step - loss: 0.2997 - reconstruction_loss: 0.1661 - umap_loss: 0.1336 - val_loss: 0.1752 - val_reconstruction_loss: 0.1752 - val_umap_loss: 0.0000e+00 Epoch 8/50 724/724 [==============================] - 14s 20ms/step - loss: 0.2966 - reconstruction_loss: 0.1648 - umap_loss: 0.1318 - val_loss: 0.1735 - val_reconstruction_loss: 0.1735 - val_umap_loss: 0.0000e+00 Epoch 9/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2938 - reconstruction_loss: 0.1638 - umap_loss: 0.1300 - val_loss: 0.1735 - val_reconstruction_loss: 0.1735 - val_umap_loss: 0.0000e+00 Epoch 10/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2911 - reconstruction_loss: 0.1630 - umap_loss: 0.1281 - val_loss: 0.1724 - val_reconstruction_loss: 0.1724 - val_umap_loss: 0.0000e+00 Epoch 11/50 724/724 [==============================] - 13s 18ms/step - loss: 0.2888 - reconstruction_loss: 0.1623 - umap_loss: 0.1265 - val_loss: 0.1724 - val_reconstruction_loss: 0.1724 - val_umap_loss: 0.0000e+00 Epoch 12/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2879 - reconstruction_loss: 0.1618 - umap_loss: 0.1260 - val_loss: 0.1715 - val_reconstruction_loss: 0.1715 - val_umap_loss: 0.0000e+00 Epoch 13/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2864 - reconstruction_loss: 0.1613 - umap_loss: 0.1250 - val_loss: 0.1710 - val_reconstruction_loss: 0.1710 - val_umap_loss: 0.0000e+00 Epoch 14/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2846 - reconstruction_loss: 0.1607 - umap_loss: 0.1239 - val_loss: 0.1706 - val_reconstruction_loss: 0.1706 - val_umap_loss: 0.0000e+00 Epoch 15/50 724/724 [==============================] - 15s 21ms/step - loss: 0.2843 - reconstruction_loss: 0.1606 - umap_loss: 0.1237 - val_loss: 0.1707 - val_reconstruction_loss: 0.1707 - val_umap_loss: 0.0000e+00 Epoch 16/50 724/724 [==============================] - 14s 19ms/step - loss: 0.2833 - reconstruction_loss: 0.1602 - umap_loss: 0.1231 - val_loss: 0.1706 - val_reconstruction_loss: 0.1706 - val_umap_loss: 0.0000e+00 Epoch 17/50 724/724 [==============================] - 14s 20ms/step - loss: 0.2833 - reconstruction_loss: 0.1601 - umap_loss: 0.1232 - val_loss: 0.1703 - val_reconstruction_loss: 0.1703 - val_umap_loss: 0.0000e+00 Epoch 18/50 ``` -------------------------------- ### Manual Installation with Conda Dependencies Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install UMAP dependencies using Conda and then install UMAP from pip. This is a fallback if pip has dependency issues. ```bash conda install numpy scipy conda install scikit-learn conda install numba pip install umap-learn ``` -------------------------------- ### Install ffmpeg using Conda Source: https://github.com/lmcinnes/umap/blob/master/notebooks/AnimatingUMAP.ipynb Installs the ffmpeg package, which is required for creating animations, using conda. The '-y' flag automatically confirms any prompts during installation. ```bash !conda install -c conda-forge ffmpeg -y ``` -------------------------------- ### UMAP with densMAP and Output Densities Source: https://github.com/lmcinnes/umap/blob/master/README.rst Example demonstrating the use of densMAP with UMAP, including setting density preservation parameters and outputting local radii. Requires scikit-learn for data fetching and resampling. ```python import umap from sklearn.datasets import fetch_openml from sklearn.utils import resample digits = fetch_openml(name='mnist_784') subsample, subsample_labels = resample(digits.data, digits.target, n_samples=7000, stratify=digits.target, random_state=1) embedding, r_orig, r_emb = umap.UMAP(densmap=True, dens_lambda=2.0, n_neighbors=30, output_dens=True).fit_transform(subsample) ``` -------------------------------- ### Import Libraries and Setup Plotting Source: https://github.com/lmcinnes/umap/blob/master/notebooks/UMAP usage and parameters.ipynb Imports essential libraries for data manipulation (numpy), visualization (matplotlib, seaborn), and UMAP. Sets up default plotting styles. ```python import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import seaborn as sns import umap %matplotlib inline sns.set(style='white', rc={'figure.figsize':(12,8)}) ``` -------------------------------- ### Setup Plotting Grid and Axes Source: https://github.com/lmcinnes/umap/blob/master/doc/inverse_transform.md Sets up a matplotlib figure with a GridSpec to create a layout for plotting the UMAP embedding and generated images. ```python # Set up the grid fig = plt.figure(figsize=(12,6)) gs = GridSpec(10, 20, fig) scatter_ax = fig.add_subplot(gs[:, :10]) digit_axes = np.zeros((10, 10), dtype=object) for i in range(10): for j in range(10): digit_axes[i, j] = fig.add_subplot(gs[i, 10 + j]) ``` -------------------------------- ### Load and Print Digits Dataset Description Source: https://github.com/lmcinnes/umap/blob/master/doc/basic_usage.md Loads the digits dataset from scikit-learn and prints its description. Ensure scikit-learn is installed. ```python3 digits = load_digits() print(digits.DESCR) ``` -------------------------------- ### Check Conda Version Source: https://github.com/lmcinnes/umap/blob/master/notebooks/AnimatingUMAP.ipynb Verifies if conda is installed and its current version. This is a preliminary step before installing other packages using conda. ```bash !conda --version ``` -------------------------------- ### Load MNIST and Fashion-MNIST datasets Source: https://github.com/lmcinnes/umap/blob/master/doc/densmap_demo.md Load the MNIST and Fashion-MNIST datasets using scikit-learn. These datasets are used as examples for demonstrating UMAP and DensMAP. ```python3 mnist = sklearn.datasets.fetch_openml("mnist_784") fmnist = sklearn.datasets.fetch_openml("Fashion-MNIST") ``` -------------------------------- ### List Available GPUs Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/03.0-parametric-umap-mnist-embedding-convnet-with-reconstruction.ipynb Lists the physical GPU devices available to TensorFlow. This helps in verifying GPU setup for accelerated training. ```python tf.config.list_physical_devices('GPU') ``` -------------------------------- ### Display Python Version Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Document embedding using UMAP.ipynb Checks the installed Python version. This is useful for ensuring compatibility with libraries. ```python !python --version ``` -------------------------------- ### Install UMAP via Conda Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install the UMAP package and its dependencies using conda from the conda-forge channel. This is the recommended method for Linux, OS X, and Windows 64-bit systems. ```bash conda install -c conda-forge umap-learn ``` -------------------------------- ### GPU-Accelerated UMAP with torchdr Source: https://github.com/lmcinnes/umap/blob/master/README.rst Demonstrates how to use torchdr for GPU-accelerated UMAP computations. Ensure CUDA is available and torchdr is installed. ```python from torchdr import UMAP as torchdrUMAP umap_gpu = torchdrUMAP( n_neighbors=15, min_dist=0.1, n_components=2, device='cuda' ) embedding = umap_gpu.fit_transform(data-maps) ``` -------------------------------- ### Load MNIST data and initialize variables Source: https://github.com/lmcinnes/umap/blob/master/doc/precomputed_k-nn.md Loads the MNIST dataset and initializes numpy arrays for storing embeddings. No specific setup is required beyond standard library imports. ```python3 from sklearn.datasets import fetch_openml import numpy as np import umap import umap.plot from umap.umap_ import nearest_neighbors data, labels = fetch_openml('mnist_784', version=1, return_X_y=True) labels = np.asarray(labels, dtype=np.int32) n_neighbors = [5, 50, 100, 250] min_dists = [0, 0.2, 0.5, 0.9] normal_embeddings = np.zeros((4, 4, 70000, 2)) precomputed_knn_embeddings = np.zeros((4, 4, 70000, 2)) ``` -------------------------------- ### Install UMAP with Plotting Dependencies via Pip Source: https://github.com/lmcinnes/umap/blob/master/README.rst Install UMAP along with all necessary dependencies for plotting functionality using pip. This command includes libraries like matplotlib and datashader. ```bash pip install umap-learn[plot] ``` -------------------------------- ### Initialize Plotly Environment Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_plotly_plot.html Ensures the Plotly environment object is initialized. This is a common setup step before using Plotly functionalities. ```javascript window.PLOTLYENV=window.PLOTLYENV || {}; ``` -------------------------------- ### Import Dimension Reduction Implementations Source: https://github.com/lmcinnes/umap/blob/master/doc/benchmarking.md Imports various dimension reduction algorithms from scikit-learn, MulticoreTSNE, and UMAP. This setup is required to compare their performance. ```ipython3 from sklearn.manifold import TSNE, LocallyLinearEmbedding, Isomap, MDS, SpectralEmbedding from sklearn.decomposition import PCA from MulticoreTSNE import MulticoreTSNE from umap import UMAP ``` -------------------------------- ### UMAP API Guide Source: https://github.com/lmcinnes/umap/blob/master/doc/index.md This section details the UMAP API, including its core UMAP and ParametricUMAP classes, as well as useful functions. ```APIDOC ## UMAP API Guide This document outlines the API for the UMAP library, focusing on its core functionalities and helper functions. ### UMAP Details about the main UMAP class and its usage. ### ParametricUMAP Information on the ParametricUMAP class for parametric embeddings. ### Useful Functions Description of various utility functions available within the UMAP library. ``` -------------------------------- ### Initialize ParametricUMAP Model Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/02.0-parametric-umap-mnist-embedding-convnet.ipynb Initialize the ParametricUMAP model with specified encoder, dimensions, number of components, and training epochs. Ensure tensorflow_probability is installed to avoid warnings. ```python embedder = ParametricUMAP(encoder=encoder, dims=dims, n_components=n_components, n_training_epochs=1, verbose=True) ``` -------------------------------- ### Import Libraries and Setup Plotting Source: https://github.com/lmcinnes/umap/blob/master/notebooks/AnimatingUMAP.ipynb Imports necessary libraries for numerical operations, plotting, and visualization. Sets up the plotting style and figure size using Seaborn. Ensures Matplotlib plots are displayed inline and animations are rendered as HTML5. ```python %matplotlib inline import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import animation from IPython.display import HTML import seaborn as sns import itertools sns.set(style='white', rc={'figure.figsize':(14, 12), 'animation.html': 'html5'}) ``` -------------------------------- ### Import Libraries and Setup Plotting Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Document embedding using UMAP.ipynb Imports necessary libraries for data loading, UMAP embedding, and visualization. It also configures Bokeh for inline plotting in a Jupyter Notebook environment. ```python import pandas as pd import umap import umap.plot # Used to get the data from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer # Some plotting libraries import matplotlib.pyplot as plt %matplotlib notebook from bokeh.plotting import show, save, output_notebook, output_file from bokeh.resources import INLINE output_notebook(resources=INLINE) ``` -------------------------------- ### Get TensorFlow Probability Version Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/07.0-parametric-umap-global-loss.ipynb Display the installed version of TensorFlow Probability. ```python tfp.__version__ ``` -------------------------------- ### Get TensorFlow Version Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/03.0-parametric-umap-mnist-embedding-convnet-with-reconstruction.ipynb Retrieves the installed TensorFlow version. Useful for checking compatibility and ensuring the correct environment. ```python tf.__version__ ``` -------------------------------- ### Build Documentation Locally Source: https://github.com/lmcinnes/umap/blob/master/CONTRIBUTING.md Builds the project documentation in HTML format using Sphinx. The output will be located in the 'doc/_build' directory. ```bash sphinx-build -b html doc doc/_build ``` -------------------------------- ### Initializing and Running Fast Algorithms Source: https://github.com/lmcinnes/umap/blob/master/doc/performance.md Sets up a list of fast dimension reduction algorithms (PCA, UMAP, MulticoreTSNE, OpenTSNE) and measures their performance across specified dataset sizes. It prints the completion time for each algorithm. ```python very_fast_algorithms = [ PCA(), UMAP(), MulticoreTSNE(), OpenTSNE(), ] vfast_performance_data = {} for algorithm in very_fast_algorithms: if 'openTSNE' in str(algorithm.__class__): alg_name = "OpenTSNE" elif 'MulticoreTSNE' in str(algorithm.__class__): alg_name = "MulticoreTSNE" else: alg_name = str(algorithm).split('(')[0] vfast_performance_data[alg_name] = data_size_scaling(algorithm, mnist_data, sizes=[3200, 6400, 12800, 25600, 51200, 70000], n_runs=2) print(f"[{time.asctime(time.localtime())}] Completed {alg_name}") ``` -------------------------------- ### Install UMAP Requirements with Conda Source: https://github.com/lmcinnes/umap/blob/master/README.rst Optionally install UMAP requirements using Conda before proceeding with the package installation. ```bash conda install scikit-learn numba ``` -------------------------------- ### Initialize and Populate LIL Sparse Matrix Source: https://github.com/lmcinnes/umap/blob/master/doc/sparse.md Initializes an empty LIL sparse matrix with the correct dimensions and then populates its 'rows' and 'data' attributes using the pre-computed lists. Requires scipy.sparse and numpy. ```python factor_matrix = scipy.sparse.lil_matrix((len(lil_matrix_rows), len(primes)), dtype=np.float32) factor_matrix.rows = np.array(lil_matrix_rows) factor_matrix.data = np.array(lil_matrix_data) ``` -------------------------------- ### Running densMAP Source: https://github.com/lmcinnes/umap/blob/master/README.rst Enable densMAP by setting the 'densmap' flag to True during UMAP initialization. This augments UMAP to preserve local density information in addition to topological structure. The 'dens_frac' parameter controls the fraction of epochs for density optimization. ```python embedding = umap.UMAP(densmap=True).fit_transform(data) ``` -------------------------------- ### Import Basic Tools for Benchmarking Source: https://github.com/lmcinnes/umap/blob/master/doc/benchmarking.md Imports necessary libraries for data manipulation, fetching data, resampling, and timing operations. These are foundational for performance analysis. ```ipython3 import numpy as np import pandas as pd from sklearn.datasets import fetch_openml from sklearn.utils import resample import time ``` -------------------------------- ### Display Sample Documents Source: https://github.com/lmcinnes/umap/blob/master/doc/document_embedding.md Iterates through the first three documents of the dataset, printing their category and the first 500 characters. ```python3 for idx, document in enumerate(dataset.data[:3]): category = dataset.target_names[dataset.target[idx]] print(f'Category: {category}') print('---------------------------') # Print the first 500 characters of the post print(document[:500]) print('---------------------------') ``` -------------------------------- ### Initialize Bokeh for Notebook Output Source: https://github.com/lmcinnes/umap/blob/master/doc/basic_usage.md Sets up Bokeh to output plots directly within a Jupyter Notebook environment. Requires bokeh.plotting and bokeh.palettes. ```python from bokeh.plotting import figure, show, output_notebook from bokeh.models import HoverTool, ColumnDataSource, CategoricalColorMapper from bokeh.palettes import Spectral10 output_notebook() ``` -------------------------------- ### Display Sample Documents Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Document embedding using UMAP.ipynb Iterates through the first three documents of the dataset, printing their category and the first 500 characters of their content. This gives a glimpse into the nature of the documents. ```python for idx, document in enumerate(dataset.data[:3]): category = dataset.target_names[dataset.target[idx]] print(f'Category: {category}') print('---------------------------') # Print the first 500 characters of the post print(document[:500]) print('---------------------------') ``` -------------------------------- ### Get Training Image Shape Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/03.0-parametric-umap-mnist-embedding-convnet-with-reconstruction.ipynb Display the shape of the training images dataset. ```python train_images.shape ``` -------------------------------- ### Configure and Fit AlignedUMAP with Parameter Lists Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_basic_usage.md Instantiate and fit AlignedUMAP with lists of parameters for n_neighbors and min_dist, along with alignment parameters. The number of datasets provided to fit must match the length of the parameter lists. ```python3 neighbors_mapper = umap.AlignedUMAP( n_neighbors=[3,4,5,7,11,16,22,29,37,45,54], min_dist=[0.01,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45], alignment_window_size=2, alignment_regularisation=1e-3, ).fit( [digits.data for i in range(10)], relations=constant_relations ) ``` -------------------------------- ### Get Data Range Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/03.0-parametric-umap-mnist-embedding-convnet-with-reconstruction.ipynb Determine the minimum and maximum values in the test images dataset. ```python np.min(test_images), np.max(test_images) ``` -------------------------------- ### Clustering Result Example Source: https://github.com/lmcinnes/umap/blob/master/doc/clustering.md Represents a numerical result from a clustering process, indicating a proportion or percentage. ```default 0.17081428571428572 ``` -------------------------------- ### Initialize ParametricUMAP Embedder Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/07.0-parametric-umap-global-loss.ipynb Create an instance of the ParametricUMAP embedder with specified parameters for global correlation loss weight and number of epochs. Verbose output is enabled. ```python embedder = ParametricUMAP( global_correlation_loss_weight = 0.1, n_epochs=25, verbose=True ) ``` -------------------------------- ### Transform Data with Embedder Source: https://github.com/lmcinnes/umap/blob/master/notebooks/MNIST_Landmarks.ipynb Use the transform method of a trained embedder to get the new embeddings for input data. ```python p_emb2_x1 = p_embedder.transform(x1) ``` -------------------------------- ### Initialize ParametricUMAP Model Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/01.0-parametric-umap-mnist-embedding-basic.ipynb Initializes the ParametricUMAP model with specified parameters. Set n_epochs for training duration and verbose for training progress output. ```python embedder = ParametricUMAP(n_epochs = 50, verbose=True) ``` -------------------------------- ### Get Embedding Shape Source: https://github.com/lmcinnes/umap/blob/master/doc/document_embedding.md Check the shape of the resulting UMAP embedding, confirming the number of documents and reduced dimensions. ```python embedding.embedding_.shape ``` -------------------------------- ### Run UMAP with Multi-threading (First Run) Source: https://github.com/lmcinnes/umap/blob/master/doc/reproducibility.md Fits the UMAP model to the loaded data. This first run utilizes multi-threading for faster performance, as indicated by the significantly smaller Wall time compared to CPU time. Numba may still be JIT compiling. ```python3 %%time mapper1 = umap.UMAP().fit(data) ``` -------------------------------- ### Initialize ParametricUMAP model Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/07.0-parametric-umap-global-loss.ipynb Creates an instance of the ParametricUMAP model with specified parameters, including a weight for the global correlation loss, number of epochs, and verbosity. The optimizer is set to Adam by default. ```python embedder = ParametricUMAP( global_correlation_loss_weight = 1.0, n_epochs=25, verbose=True ) ``` -------------------------------- ### Import Libraries for UMAP Source: https://github.com/lmcinnes/umap/blob/master/doc/inverse_transform.md Imports necessary libraries for data manipulation, plotting, and UMAP functionality. Ensure all these libraries are installed. ```python3 import numpy as np import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec import seaborn as sns import sklearn.datasets import umap import umap.plot ``` -------------------------------- ### Prepare Hover Data with Pandas Source: https://github.com/lmcinnes/umap/blob/master/doc/plotting.md Construct a pandas DataFrame to hold data for interactive hover tooltips. Each row corresponds to a data point, and columns contain information like index, label, and item name. ```python hover_data = pd.DataFrame({'index':np.arange(30000), 'label':fmnist.target[:30000]}) hover_data['item'] = hover_data.label.map( { '0':'T-shirt/top', '1':'Trouser', '2':'Pullover', '3':'Dress', '4':'Coat', '5':'Sandal', '6':'Shirt', '7':'Sneaker', '8':'Bag', '9':'Ankle Boot', } ) ``` -------------------------------- ### Import Plotting Tools and Load Data Source: https://github.com/lmcinnes/umap/blob/master/doc/benchmarking.md Imports plotting libraries and fetches the Fashion-MNIST dataset for use in performance comparisons. Ensures plots are displayed inline. ```ipython3 import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline ``` ```ipython3 mnist = fetch_openml('Fashion-MNIST', version=1) ``` -------------------------------- ### Create and Populate Sparse Matrix Source: https://github.com/lmcinnes/umap/blob/master/doc/sparse.md Creates a lil_matrix and populates it with the prepared row and data arrays. This matrix will be used for transforming new data. ```python3 new_data = scipy.sparse.lil_matrix((len(lil_matrix_rows), len(primes)), dtype=np.float32) new_data.rows = np.array(lil_matrix_rows) new_data.data = np.array(lil_matrix_data) new_data ``` -------------------------------- ### Load Digits Dataset Source: https://github.com/lmcinnes/umap/blob/master/notebooks/AnimatingUMAP.ipynb Loads the digits dataset from scikit-learn. This dataset is used as an example for UMAP embedding due to its manageable size. ```python from sklearn.datasets import load_digits ``` -------------------------------- ### Basic UMAP Embedding Source: https://github.com/lmcinnes/umap/blob/master/README.rst Perform a basic UMAP dimension reduction on scikit-learn compatible data. This example demonstrates the default usage. ```python import umap from sklearn.datasets import load_digits digits = load_digits() embedding = umap.UMAP().fit_transform(digits.data) ``` -------------------------------- ### Initialize ParametricUMAP Model Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/04.0-parametric-umap-mnist-embedding-convnet-with-autoencoder-loss.ipynb Instantiate a ParametricUMAP model with specified encoder, decoder, dimensions, and training configurations. Ensure reconstruction_validation is provided if autoencoder_loss is True. ```python embedder = ParametricUMAP( encoder=encoder, decoder=decoder, dims=dims, n_training_epochs=1, n_components=n_components, parametric_reconstruction= True, autoencoder_loss = True, reconstruction_validation=test_images, verbose=True, ) ``` -------------------------------- ### Import Notebook Display Tools and Animation Module Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_basic_usage.md Imports necessary libraries for displaying images and HTML in a notebook environment, along with Matplotlib's animation module for creating animations. This is a prerequisite for generating animated GIFs. ```python from IPython.display import display, Image, HTML from matplotlib import animation ``` -------------------------------- ### Basic Parametric UMAP Usage Source: https://github.com/lmcinnes/umap/blob/master/doc/parametric_umap.md Replace `umap.UMAP` with `parametric_umap.ParametricUMAP` for basic usage. This initializes and transforms data using the default network architecture. ```python3 from umap.parametric_umap import ParametricUMAP embedder = ParametricUMAP() embedding = embedder.fit_transform(my_data) ``` -------------------------------- ### Download and Time 20 Newsgroups Dataset Source: https://github.com/lmcinnes/umap/blob/master/doc/document_embedding.md Downloads the 20 newsgroups dataset and measures the time taken for the operation. ```python3 %%time dataset = fetch_20newsgroups(subset='all', shuffle=True, random_state=42) ``` -------------------------------- ### Prepare Sparse Matrix for New Data Source: https://github.com/lmcinnes/umap/blob/master/doc/sparse.md Prepares a sparse matrix to hold new data for transformation. This involves iterating through integers and their prime factors. ```python3 %%time lil_matrix_rows = [] lil_matrix_data = [] for n in range(100000, 110000): prime_factors = sympy.primefactors(n) lil_matrix_rows.append([prime_to_column[p] for p in prime_factors]) lil_matrix_data.append([1] * len(prime_factors)) ``` -------------------------------- ### Fit UMAP on Full MNIST Dataset Source: https://github.com/lmcinnes/umap/blob/master/doc/composing_models.md Applies UMAP to the entire MNIST dataset to generate a 2D embedding. Ensure UMAP is installed and imported. ```python3 mapper = umap.UMAP(random_state=42).fit(mnist.data) ``` -------------------------------- ### Initialize and Train ParametricUMAP with Reconstruction Source: https://github.com/lmcinnes/umap/blob/master/doc/parametric_umap.md Initialize ParametricUMAP with a provided encoder and decoder, enabling parametric reconstruction. Pass validation data to monitor reconstruction loss on unseen samples during training. ```python embedder = ParametricUMAP( encoder=encoder, decoder=decoder, dims=dims, parametric_reconstruction= True, reconstruction_validation=validation_images, verbose=True, ) embedding = embedder.fit_transform(train_images) ``` -------------------------------- ### Apply Supervised UMAP for Visualization Source: https://github.com/lmcinnes/umap/blob/master/doc/supervised.md Applies supervised UMAP to the training data, using labels to guide the embedding. Visualizes the result and saves it to 'galaxy10_2D_umap_supervised.svg'. ```python reducer = umap.UMAP( n_components=2, n_neighbors=15, random_state=42, transform_seed=42, verbose=False ) reducer.fit(X_train, y_train) galaxy10_umap_supervised = reducer.transform(X_train) fig = plt.figure(1, figsize=(4, 4)) plt.clf() plt.scatter( galaxy10_umap_supervised[:, 0], galaxy10_umap_supervised[:, 1], c=y_train, cmap=plt.cm.nipy_spectral, edgecolor="k", label=y_train, ) plt.colorbar(boundaries=np.arange(11) - 0.5).set_ticks(np.arange(10)) plt.savefig("galaxy10_2D_umap_supervised.svg") ``` -------------------------------- ### Import necessary libraries Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/07.0-parametric-umap-global-loss.ipynb Imports NumPy, SciPy stats, and Scikit-learn for numerical operations, statistical analysis, and metrics. ```python import numpy as np import scipy.stats import sklearn ``` -------------------------------- ### Initialize ParametricUMAP Model Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/05.0-parametric-umap-with-callback.ipynb Instantiate the ParametricUMAP model with specified training epochs and Keras fit arguments. Set verbose to True for detailed training output. ```python embedder = ParametricUMAP( verbose=True, keras_fit_kwargs = keras_fit_kwargs, n_training_epochs=5 ) ``` -------------------------------- ### Supervised UMAP DensMAP Transformation Source: https://github.com/lmcinnes/umap/blob/master/doc/densmap_demo.md Applies UMAP with DensMAP in a supervised manner, using training labels to guide the dimensionality reduction. Requires UMAP and matplotlib. ```python reducer = umap.UMAP( densmap=True, n_components=2, random_state=42, verbose=False ) reducer.fit(X_train, y_train) galaxy10_densmap_supervised = reducer.transform(X_train) fig = plt.figure(1, figsize=(4, 4)) plt.clf() plt.scatter( galaxy10_densmap_supervised[:, 0], galaxy10_densmap_supervised[:, 1], c=y_train, cmap=plt.cm.nipy_spectral, edgecolor="k", label=y_train, ) plt.colorbar(boundaries=np.arange(11) - 0.5).set_ticks(np.arange(10)) plt.savefig("galaxy10_2D_densmap_supervised.svg") ``` -------------------------------- ### Run UMAP with Multi-threading (Second Run) Source: https://github.com/lmcinnes/umap/blob/master/doc/reproducibility.md Fits the UMAP model to the data again. This second run is faster than the first because Numba's JIT compilation is already complete. Multi-threading is still active. ```python3 %%time mapper2 = umap.UMAP().fit(data) ``` -------------------------------- ### Save Animation as GIF Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_basic_usage.md Saves the created animation as a GIF file using the 'pillow' writer. Ensure the 'pillow' library is installed. The animation figure is closed after saving. ```python anim.save("aligned_umap_pendigits_anim.gif", writer="pillow") plt.close(anim._fig) ``` -------------------------------- ### Get Unique Bin Edges and Count Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/07.0-parametric-umap-global-loss.ipynb Determines the unique bin edges used for histogramming and returns their count. This is often used after digitizing data for plotting or analysis. ```python np.unique(xbins), len(np.unique(xbins)) ``` -------------------------------- ### Initialize Bokeh Logging Source: https://github.com/lmcinnes/umap/blob/master/examples/iris/iris.html Sets the logging level for Bokeh to 'info'. This is useful for debugging and monitoring Bokeh's internal processes. ```javascript Bokeh.set_log_level("info"); ``` -------------------------------- ### Define a Custom Neural Network for Parametric UMAP Source: https://github.com/lmcinnes/umap/blob/master/doc/parametric_umap.md Define a custom neural network architecture using `tf.keras.Sequential` and pass it to `ParametricUMAP`. This example uses convolutional layers for MNIST data. ```python # define the network import tensorflow as tf dims = (28, 28, 1) n_components = 2 encoder = tf.keras.Sequential([ tf.keras.layers.InputLayer(input_shape=dims), tf.keras.layers.Conv2D( filters=32, kernel_size=3, strides=(2, 2), activation="relu", padding="same" ), tf.keras.layers.Conv2D( filters=64, kernel_size=3, strides=(2, 2), activation="relu", padding="same" ), tf.keras.layers.Flatten(), tf.keras.layers.Dense(units=256, activation="relu"), tf.keras.layers.Dense(units=256, activation="relu"), tf.keras.layers.Dense(units=n_components), ]) encoder.summary() ``` -------------------------------- ### Initialize and Fit UMAP Reducer Source: https://github.com/lmcinnes/umap/blob/master/doc/basic_usage.md Initializes a UMAP reducer with a specified random state and fits it to the digits dataset. This prepares the model for dimensionality reduction. ```python3 reducer = umap.UMAP(random_state=42) reducer.fit(digits.data) ``` -------------------------------- ### Configure Keras Fit Arguments for Early Stopping Source: https://github.com/lmcinnes/umap/blob/master/doc/parametric_umap.md Define a dictionary `keras_fit_kwargs` to pass Keras callbacks to the `fit` method of ParametricUMAP. This example includes `EarlyStopping` to halt training when the loss plateaus. ```python keras_fit_kwargs = {"callbacks": [ tf.keras.callbacks.EarlyStopping( monitor='loss', min_delta=10**-2, patience=10, verbose=1, ) ]} ``` -------------------------------- ### Load and prepare dataset Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_basic_usage.md Loads the digits dataset from sklearn and sorts it based on the sum of ink in each digit. ```python3 digits = sklearn.datasets.load_digits() ``` ```python3 ordered_digits = digits.data[np.argsort(digits.data.sum(axis=1))] ordered_target = digits.target[np.argsort(digits.data.sum(axis=1))] plt.matshow(ordered_digits[-1].reshape((8,8))) ``` -------------------------------- ### Import Necessary Libraries for UMAP Source: https://github.com/lmcinnes/umap/blob/master/doc/basic_usage.md Import essential libraries including numpy, scikit-learn modules for data handling and preprocessing, and plotting tools like matplotlib and seaborn. This setup is required before using UMAP. ```python3 import numpy as np from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt import seaborn as sns import pandas as pd %matplotlib inline ``` -------------------------------- ### Create 3D Scatter Plot Traces Source: https://github.com/lmcinnes/umap/blob/master/doc/aligned_umap_basic_usage.md Generates traces for a 3D scatter plot using Plotly. Each trace represents a curve with a specific color based on the digit it represents. Ensure Plotly and SciPy are installed. ```python palette = px.colors.diverging.Spectral interpolated_traces = [fx(z), fy(z)] traces = [ go.Scatter3d( x=interpolated_traces[0][i], y=interpolated_traces[1][i], z=z*3.0, mode="lines", line=dict( color=palette[digits.target[i]], width=3.0 ), opacity=1.0, ) for i in range(digits.data.shape[0]) ] fig = go.Figure(data=traces) fig.update_layout( width=800, height=700, autosize=False, showlegend=False, ) fig.show() ``` -------------------------------- ### Load Libraries for UMAP and Data Handling Source: https://github.com/lmcinnes/umap/blob/master/doc/document_embedding.md Imports necessary libraries for UMAP, data manipulation, and dataset loading. Requires UMAP version >= 0.4.0. Configures Bokeh for notebook output. ```python3 import pandas as pd import umap import umap.plot # Used to get the data from sklearn.datasets import fetch_20newsgroups from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer # Some plotting libraries import matplotlib.pyplot as plt %matplotlib notebook from bokeh.plotting import show, save, output_notebook, output_file from bokeh.resources import INLINE output_notebook(resources=INLINE) ``` -------------------------------- ### Setup Plotting Grid and Scatter Plot Source: https://github.com/lmcinnes/umap/blob/master/doc/inverse_transform.md Configures a matplotlib figure with a `GridSpec` to display both the UMAP embedding scatter plot and a grid for generated digits. It also plots the original data points and the test points used for inverse transformation. ```python3 # Set up the grid fig = plt.figure(figsize=(12,6)) gs = GridSpec(10, 20, fig) scatter_ax = fig.add_subplot(gs[:, :10]) digit_axes = np.zeros((10, 10), dtype=object) for i in range(10): for j in range(10): digit_axes[i, j] = fig.add_subplot(gs[i, 10 + j]) # Use umap.plot to plot to the major axis # umap.plot.points(mapper, labels=labels, ax=scatter_ax) scatter_ax.scatter(mapper.embedding_[:, 0], mapper.embedding_[:, 1], c=labels.astype(np.int32), cmap='Spectral', s=0.1) scatter_ax.set(xticks=[], yticks=[]) # Plot the locations of the text points scatter_ax.scatter(test_pts[:, 0], test_pts[:, 1], marker='x', c='k', s=15) ``` -------------------------------- ### Select and prepare landmarks for Parametric UMAP training Source: https://github.com/lmcinnes/umap/blob/master/doc/transform_landmarked_pumap.md Selects landmark indices from the original dataset (x1), concatenates them with the new dataset (x2) to create augmented training data (x2_lmk, y2_lmk), and constructs a landmarks vector. The landmarks vector contains NaN for points without landmark information and the transformed positions for the selected landmarks. ```python # Select landmarks indexes from x1. # landmark_idx = list(np.random.choice(range(x1.shape[0]), int(x1.shape[0]/100), replace=False)) # Add the landmark points to x2 for training. # x2_lmk = np.concatenate((x2, x1[landmark_idx])) y2_lmk = np.concatenate((y2, y1[landmark_idx])) # Make our landmarks vector, which is nan where we have no landmark information. # landmarks = np.stack( [np.array([np.nan, np.nan])]*x2.shape[0] + list( p_embedder.transform( x1[landmark_idx] ) ) ) # Set landmark loss weight and continue training our Parametric UMAP model. # p_embedder.landmark_loss_weight = 0.01 p_embedder.fit(x2_lmk, landmark_positions=landmarks) p_emb2_x2 = p_embedder.transform(x2) # Check how x1 looks when embedded in the space retrained on x2 and landmarks. ``` -------------------------------- ### Visualize Original and Reconstructed Images Source: https://github.com/lmcinnes/umap/blob/master/notebooks/Parametric_UMAP/03.0-parametric-umap-mnist-embedding-convnet-with-reconstruction.ipynb Generate a plot comparing original test images with their reconstructions from the ParametricUMAP model. Images are displayed in grayscale. ```python nex = 10 fig, axs = plt.subplots(ncols=10, nrows=2, figsize=(nex, 2)) for i in range(nex): axs[0, i].matshow(np.squeeze(test_images[i].reshape(28, 28, 1)), cmap=plt.cm.Greys) axs[1, i].matshow( np.squeeze(test_images_recon[i].reshape(28, 28, 1)), cmap=plt.cm.Greys, vmin = 0, vmax = 1 ) for ax in axs.flatten(): ax.axis("off") ```