Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RbeginneraiAI generated

Iris-style Dataset Clustering Explorer

Build an R script that finds natural groups in data without being told what they are, using k-means clustering, a classic unsupervised learning method. Working with a dataset like the Iris flowers (measurements of petals and sepals), you'll scale the measurements so none dominates, choose a sensible number of groups with the elbow and silhouette methods, run k-means, and plot the groups in two dimensions using PCA (principal component analysis), which squeezes many measurements into two while keeping as much information as possible. Finally you'll compare the groups found with the real species. You'll learn the basics of clustering, why scaling matters, choosing k, PCA for visualization, and ggplot2 with factoextra.

Estimate
~7h
Steps
6
Completed by
0
Proposed by
codeseed.app

stats · ggplot2 · factoextra

Project roadmap

  1. 01

    Load and scale the data

    ~1h

    Load the built-in iris dataset, keep the four numeric columns (setting the species aside for later), and look at summary() and a pairs plot. Scale the columns with scale() so each has mean 0 and standard deviation 1, and write in a comment why this matters for k-means (it uses distances, so large-valued columns would dominate). Done when: the scaled data has means of about 0 and standard deviations of 1.

  2. 02

    Choose the number of clusters

    ~1.5h

    Use factoextra's fviz_nbclust with the elbow method (total within-cluster sum of squares for k from 1 to 10) and the silhouette method, and save both charts. Explain in a comment which k you choose and why, noting that the methods can disagree. Set a random seed so results are repeatable. Done when: both charts are saved, and the choice of k is explained.

  3. 03

    Run k-means

    ~1h

    Run kmeans with the chosen k and nstart = 25 (trying 25 random starts and keeping the best, since results depend on the starting points). Add the cluster labels to the original data and print the size and average measurements of each cluster. Done when: each flower has a cluster, and the cluster averages are printed.

  4. 04

    Visualize with PCA

    ~1.5h

    Run PCA with prcomp on the scaled data, print how much variation the first two components explain, and plot the flowers on those two components colored by cluster, with fviz_cluster or ggplot2. Save the chart as a PNG. Done when: the chart shows clearly separated or overlapping groups, and the explained variation is printed.

  5. 05

    Compare with the real species

    ~1h

    Make a table of clusters against the actual species with table(), and describe which species the clustering separates well and which it mixes up, and why that happens (for example two species with similar measurements). Done when: the comparison table is printed with a short written interpretation.

  6. 06

    Wrap it in a report

    ~1h

    Put the whole analysis in an R Markdown or Quarto report with each step's chart and a short explanation, readable by someone who has never heard of k-means. Done when: rendering the report produces an HTML file with all charts and explanations.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7h · 6 steps

Tech stack

statsggplot2factoextra