Jump to content

File:Umap-iris.png: Difference between revisions

From 太極
Brb (talk | contribs)
<syntaxhighlight lang='r'> library(umap) # Load the built-in Iris dataset data(iris) # Separate the features (variables) from the species labels iris_features <- iris[, 1:4] iris_species <- iris[, 5] # Run UMAP, reducing the 4 dimensions down to 2 # n_neighbors: Controls how UMAP balances local vs. global structure. # min_dist: Controls how tightly points are packed together. iris_umap_results <- umap(iris_features, random_state = 42) # The result is a list. The actual 2D coordinates are...
 
(No difference)

Latest revision as of 17:16, 10 November 2025

Summary

library(umap)

# Load the built-in Iris dataset
data(iris)

# Separate the features (variables) from the species labels
iris_features <- iris[, 1:4]
iris_species <- iris[, 5]

# Run UMAP, reducing the 4 dimensions down to 2
# n_neighbors: Controls how UMAP balances local vs. global structure.
# min_dist: Controls how tightly points are packed together.
iris_umap_results <- umap(iris_features, random_state = 42)

# The result is a list. The actual 2D coordinates are stored in $layout
head(iris_umap_results$layout)

# Create a data frame for plotting
plot_data <- data.frame(
  UMAP1 = iris_umap_results$layout[, 1],
  UMAP2 = iris_umap_results$layout[, 2],
  Species = iris_species
)

# Plot the results, colored by the original Species (group)
plot(plot_data$UMAP1, plot_data$UMAP2,
     col = plot_data$Species,
     pch = 16, # Solid circles
     main = "Iris Dataset Projected with UMAP",
     xlab = "UMAP Dimension 1",
     ylab = "UMAP Dimension 2")

# Add a legend
legend("topleft", 
       legend = levels(plot_data$Species), 
       col = 1:3, 
       pch = 16)

library(ggplot2)
qplot(UMAP1, UMAP2, data = plot_data, color = Species)

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current17:16, 10 November 2025Thumbnail for version as of 17:16, 10 November 2025600 × 600 (25 KB)Brb (talk | contribs)<syntaxhighlight lang='r'> library(umap) # Load the built-in Iris dataset data(iris) # Separate the features (variables) from the species labels iris_features <- iris[, 1:4] iris_species <- iris[, 5] # Run UMAP, reducing the 4 dimensions down to 2 # n_neighbors: Controls how UMAP balances local vs. global structure. # min_dist: Controls how tightly points are packed together. iris_umap_results <- umap(iris_features, random_state = 42) # The result is a list. The actual 2D coordinates are...

The following page uses this file: