Bird
Raised Fist0
ML Pythonml~10 mins

UMAP for dimensionality reduction in ML Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the UMAP class from the umap-learn library.

ML Python
from umap import [1]
Drag options to blanks, or click blank then click option'
AKMeans
BPCA
CTSNE
DUMAP
Attempts:
3 left
💡 Hint
Common Mistakes
Importing PCA or TSNE instead of UMAP.
Trying to import from sklearn instead of umap.
2fill in blank
medium

Complete the code to create a UMAP object with 2 output dimensions.

ML Python
reducer = UMAP(n_components=[1])
Drag options to blanks, or click blank then click option'
A2
B1
C10
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 or more components when 2 is needed for 2D plots.
Using 1 component which reduces to 1D, not 2D.
3fill in blank
hard

Fix the error in the code to fit and transform data using UMAP.

ML Python
embedding = reducer.[1](data)
Drag options to blanks, or click blank then click option'
Afit_transform
Bfit
Ctransform
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using fit alone which does not return transformed data.
Using predict which is not a method for UMAP.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.

ML Python
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('a')
Dword.isalpha()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.startswith('a') which filters words starting with 'a' instead of length.
Using word.isalpha() which checks if word has only letters.
5fill in blank
hard

Fill all three blanks to create a dictionary of embeddings where keys are uppercase words, values are the first component of embedding, and only include if the first component is positive.

ML Python
{ [1]: embedding[i, 0] for i, [2] in enumerate(words) if embedding[i, 0] [3] 0 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using 'i' instead of 'word' in the loop unpacking.

Practice

(1/5)
1. What is the main purpose of using UMAP in machine learning?
easy
A. To reduce the number of features while keeping data structure
B. To increase the number of features for better accuracy
C. To split data into training and testing sets
D. To normalize data values between 0 and 1

Solution

  1. Step 1: Understand UMAP's role

    UMAP is a tool to reduce many features into fewer dimensions.
  2. Step 2: Identify the goal of dimensionality reduction

    The goal is to keep similar data points close and preserve structure while reducing features.
  3. Final Answer:

    To reduce the number of features while keeping data structure -> Option A
  4. Quick Check:

    UMAP reduces features = B [OK]
Hint: UMAP shrinks features, keeps data shape [OK]
Common Mistakes:
  • Thinking UMAP increases features
  • Confusing UMAP with data splitting
  • Mixing UMAP with normalization
2. Which of the following is the correct way to import UMAP from the umap-learn library in Python?
easy
A. from umap import umap
B. from umap import UMAP
C. import UMAP from umap
D. import umap.UMAP

Solution

  1. Step 1: Recall correct Python import syntax

    Python imports classes or functions using 'from module import Class'.
  2. Step 2: Match with UMAP library usage

    The correct import is 'from umap import UMAP'. Options A and C look similar but A uses lowercase 'umap' which is incorrect.
  3. Final Answer:

    from umap import UMAP -> Option B
  4. Quick Check:

    Correct import syntax = D [OK]
Hint: Use 'from umap import UMAP' to import [OK]
Common Mistakes:
  • Using incorrect import syntax
  • Confusing module and class names
  • Using lowercase instead of uppercase for UMAP
3. What will be the shape of the output after applying UMAP with n_components=2 on a dataset with 100 samples and 50 features?
medium
A. (2, 50)
B. (50, 2)
C. (100, 2)
D. (100, 50)

Solution

  1. Step 1: Understand input data shape

    The dataset has 100 samples (rows) and 50 features (columns).
  2. Step 2: Apply UMAP dimensionality reduction

    UMAP reduces features from 50 to 2, so output shape is (samples, new_features) = (100, 2).
  3. Final Answer:

    (100, 2) -> Option C
  4. Quick Check:

    Output shape = (samples, n_components) = (100, 2) [OK]
Hint: Output rows = samples, columns = n_components [OK]
Common Mistakes:
  • Swapping samples and features in output shape
  • Confusing n_components with number of samples
  • Assuming output shape stays same as input
4. You run UMAP with n_neighbors=5 on a dataset but get an error. What is the most likely cause?
medium
A. UMAP requires n_neighbors to be exactly 10
B. The dataset has more than 5 features
C. n_neighbors must be larger than number of features
D. The dataset has fewer than 5 samples

Solution

  1. Step 1: Understand n_neighbors parameter

    n_neighbors defines how many nearest points UMAP uses to learn structure.
  2. Step 2: Check dataset size relation

    If dataset has fewer samples than n_neighbors, UMAP cannot find enough neighbors, causing error.
  3. Final Answer:

    The dataset has fewer than 5 samples -> Option D
  4. Quick Check:

    n_neighbors ≤ samples needed = A [OK]
Hint: n_neighbors must be ≤ number of samples [OK]
Common Mistakes:
  • Confusing features with samples for n_neighbors
  • Assuming fixed n_neighbors value required
  • Ignoring dataset size when setting n_neighbors
5. You want to visualize a dataset with 1000 samples and 100 features in 3D using UMAP. Which combination of parameters is best?
hard
A. n_components=3, n_neighbors=15 to balance detail and speed
B. n_components=2, n_neighbors=50 for maximum neighbor info
C. n_components=3, n_neighbors=1000 to use all samples as neighbors
D. n_components=10, n_neighbors=5 for detailed high dimensions

Solution

  1. Step 1: Choose n_components for 3D visualization

    Set n_components=3 to get 3D output suitable for plotting.
  2. Step 2: Select n_neighbors for balance

    n_neighbors=15 is a good default to capture local structure without slowing down too much.
  3. Step 3: Evaluate other options

    n_components=2, n_neighbors=50 for maximum neighbor info uses 2D, not 3D. n_components=3, n_neighbors=1000 to use all samples as neighbors uses too many neighbors, slowing computation. n_components=10, n_neighbors=5 for detailed high dimensions uses 10 components, not 3D.
  4. Final Answer:

    n_components=3, n_neighbors=15 to balance detail and speed -> Option A
  5. Quick Check:

    3D + balanced neighbors = C [OK]
Hint: Use n_components=3 for 3D, moderate n_neighbors for speed [OK]
Common Mistakes:
  • Choosing wrong n_components for visualization
  • Setting n_neighbors too high causing slow run
  • Confusing number of neighbors with number of components