Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- from scipy.spatial.distance import euclidean, cityblock, chebyshev, minkowski, hamming, cosine, jaccard
- # Define the two points for Euclidean, Manhattan, Chebyshev, and Minkowski
- point1 = (1, 2)
- point2 = (4, 6)
- # Define binary vectors for Hamming
- point1_binary = np.array([1, 0, 1, 0])
- point2_binary = np.array([1, 1, 0, 0])
- # Define vectors for Cosine
- point1_vector = np.array([1, 2, 3])
- point2_vector = np.array([4, 6, 8])
- # Define sets for Jaccard and convert them to binary vectors
- point1_set = set([1, 2, 3])
- point2_set = set([3, 4, 5])
- # Convert sets to binary vectors
- all_elements = point1_set.union(point2_set)
- point1_binary_vector = np.array([1 if x in point1_set else 0 for x in all_elements])
- point2_binary_vector = np.array([1 if x in point2_set else 0 for x in all_elements])
- # List of distance functions and their names
- distance_functions = [
- ("Euclidean", euclidean, point1, point2),
- ("Manhattan", cityblock, point1, point2),
- ("Chebyshev", chebyshev, point1, point2),
- ("Minkowski (p=3)", lambda p1, p2: minkowski(p1, p2, p=3), point1, point2),
- ("Hamming", hamming, point1_binary, point2_binary),
- ("Cosine", cosine, point1_vector, point2_vector),
- ("Jaccard", jaccard, point1_binary_vector, point2_binary_vector)
- ]
- # Calculate distances and store them
- distances = []
- for name, distance_func, p1, p2 in distance_functions:
- distance = distance_func(p1, p2)
- distances.append((name, distance))
- print(f"{name} distance:", distance)
- # Plot the distances using a bar chart
- fig, ax = plt.subplots(figsize=(10, 6))
- distance_names = [d[0] for d in distances]
- distance_values = [d[1] for d in distances]
- bars = ax.bar(distance_names, distance_values, color='skyblue')
- ax.set_xlabel('Distance Metrics')
- ax.set_ylabel('Distance Values')
- ax.set_title('Comparison of Different Distance Metrics')
- # Set the tick positions and labels explicitly
- ax.set_xticks(range(len(distance_names)))
- ax.set_xticklabels(distance_names, rotation=45, ha='right')
- # Add value labels on top of the bars
- for bar in bars:
- yval = bar.get_height()
- ax.text(bar.get_x() + bar.get_width()/2, yval + 0.05, round(yval, 2), ha='center', va='bottom')
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement