0
0
SciPydata~30 mins

Flat clustering (fcluster) in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Flat Clustering with fcluster in SciPy
📖 Scenario: You work in a small shop that sells fruits. You want to group similar fruits based on their sweetness and crunchiness scores to understand customer preferences better.
🎯 Goal: Build a program that uses hierarchical clustering and then applies fcluster to create flat clusters of fruits based on a distance threshold.
📋 What You'll Learn
Create a dictionary called fruits with fruit names as keys and tuples of (sweetness, crunchiness) as values.
Create a variable called threshold and set it to 1.5.
Use scipy.cluster.hierarchy.linkage to compute the linkage matrix from the fruit data.
Use scipy.cluster.hierarchy.fcluster with the linkage matrix and threshold to assign cluster labels.
Print the dictionary clusters that maps fruit names to their cluster labels.
💡 Why This Matters
🌍 Real World
Grouping similar items helps businesses understand customer preferences and organize products better.
💼 Career
Clustering is a common technique in data science for market segmentation, recommendation systems, and pattern discovery.
Progress0 / 4 steps
1
Create the fruit data dictionary
Create a dictionary called fruits with these exact entries: 'Apple': (7, 8), 'Banana': (10, 2), 'Cherry': (8, 7), 'Date': (9, 3), 'Elderberry': (6, 9).
SciPy
Need a hint?

Use curly braces {} to create a dictionary. Each fruit name is a key, and the value is a tuple with two numbers.

2
Set the clustering threshold
Create a variable called threshold and set it to 1.5.
SciPy
Need a hint?

Just write threshold = 1.5 on a new line.

3
Compute linkage and assign clusters
Import linkage and fcluster from scipy.cluster.hierarchy. Create a list called data_points containing the fruit values from fruits. Use linkage with data_points and method 'ward' to create Z. Then use fcluster with Z, threshold, and criterion 'distance' to create labels. Finally, create a dictionary called clusters that maps each fruit name to its cluster label.
SciPy
Need a hint?

Use list(fruits.values()) to get the data points. Use linkage with method 'ward'. Use fcluster with criterion 'distance'. Use a dictionary comprehension to map fruit names to labels.

4
Print the clusters dictionary
Write a print statement to display the clusters dictionary.
SciPy
Need a hint?

Use print(clusters) to show the result.