0
0
SciPydata~30 mins

Distance metrics (euclidean, cosine, manhattan) in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Distance metrics (euclidean, cosine, manhattan)
📖 Scenario: You work as a data analyst for a company that wants to compare customer preferences. You have two customers' ratings for different products. You want to measure how similar or different these customers are using three common distance metrics: Euclidean, Cosine, and Manhattan distances.
🎯 Goal: Build a small program that calculates the Euclidean, Cosine, and Manhattan distances between two given customer rating vectors using scipy. This will help you understand how these distance metrics work and how to use them in Python.
📋 What You'll Learn
Create two lists called customer1 and customer2 with exact rating values.
Create a variable called metrics that holds the names of the distance metrics as strings.
Use scipy.spatial.distance functions to calculate Euclidean, Cosine, and Manhattan distances between customer1 and customer2.
Print the results clearly labeled.
💡 Why This Matters
🌍 Real World
Distance metrics help compare customers, products, or documents by measuring how similar or different they are. This is useful in recommendation systems, clustering, and search engines.
💼 Career
Data scientists and analysts use distance metrics to analyze patterns, group data, and build machine learning models that rely on similarity measures.
Progress0 / 4 steps
1
Create customer rating lists
Create two lists called customer1 and customer2 with these exact values: customer1 = [4, 3, 5, 1, 2] and customer2 = [5, 1, 4, 2, 3].
SciPy
Need a hint?

Use square brackets to create lists and separate numbers with commas.

2
Create a list of metric names
Create a list called metrics that contains these exact strings: 'euclidean', 'cosine', and 'cityblock'.
SciPy
Need a hint?

Use a list with the exact strings inside single quotes.

3
Calculate distances using scipy
Import scipy.spatial.distance as dist. Then create a dictionary called results where keys are metric names from metrics and values are the distances calculated by calling dist.(customer1, customer2) for each metric in metrics.
SciPy
Need a hint?

Use getattr(dist, metric) to call the function by name inside a dictionary comprehension.

4
Print the distance results
Use a for loop with variables metric and value to iterate over results.items(). Inside the loop, print the metric name and its distance value in this exact format: "Euclidean distance: 2.828" (rounded to 3 decimal places). Use metric.capitalize() to capitalize the metric name and print the value rounded with round(value, 3).
SciPy
Need a hint?

Use a for loop and f-string to format the output exactly as shown.