Advertisement
brandblox

Lab_ML(03/02/25)

Feb 3rd, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from scipy.spatial.distance import euclidean, cityblock, chebyshev, minkowski, hamming, cosine, jaccard
  4.  
  5. # Define the two points for Euclidean, Manhattan, Chebyshev, and Minkowski
  6. point1 = (1, 2)
  7. point2 = (4, 6)
  8.  
  9. # Define binary vectors for Hamming
  10. point1_binary = np.array([1, 0, 1, 0])
  11. point2_binary = np.array([1, 1, 0, 0])
  12.  
  13. # Define vectors for Cosine
  14. point1_vector = np.array([1, 2, 3])
  15. point2_vector = np.array([4, 6, 8])
  16.  
  17. # Define sets for Jaccard and convert them to binary vectors
  18. point1_set = set([1, 2, 3])
  19. point2_set = set([3, 4, 5])
  20. # Convert sets to binary vectors
  21. all_elements = point1_set.union(point2_set)
  22. point1_binary_vector = np.array([1 if x in point1_set else 0 for x in all_elements])
  23. point2_binary_vector = np.array([1 if x in point2_set else 0 for x in all_elements])
  24.  
  25. # List of distance functions and their names
  26. distance_functions = [
  27.     ("Euclidean", euclidean, point1, point2),
  28.     ("Manhattan", cityblock, point1, point2),
  29.     ("Chebyshev", chebyshev, point1, point2),
  30.     ("Minkowski (p=3)", lambda p1, p2: minkowski(p1, p2, p=3), point1, point2),
  31.     ("Hamming", hamming, point1_binary, point2_binary),
  32.     ("Cosine", cosine, point1_vector, point2_vector),
  33.     ("Jaccard", jaccard, point1_binary_vector, point2_binary_vector)
  34. ]
  35.  
  36. # Calculate distances and store them
  37. distances = []
  38. for name, distance_func, p1, p2 in distance_functions:
  39.     distance = distance_func(p1, p2)
  40.     distances.append((name, distance))
  41.     print(f"{name} distance:", distance)
  42.  
  43. # Plot the distances using a bar chart
  44. fig, ax = plt.subplots(figsize=(10, 6))
  45. distance_names = [d[0] for d in distances]
  46. distance_values = [d[1] for d in distances]
  47.  
  48. bars = ax.bar(distance_names, distance_values, color='skyblue')
  49. ax.set_xlabel('Distance Metrics')
  50. ax.set_ylabel('Distance Values')
  51. ax.set_title('Comparison of Different Distance Metrics')
  52.  
  53. # Set the tick positions and labels explicitly
  54. ax.set_xticks(range(len(distance_names)))
  55. ax.set_xticklabels(distance_names, rotation=45, ha='right')
  56.  
  57. # Add value labels on top of the bars
  58. for bar in bars:
  59.     yval = bar.get_height()
  60.     ax.text(bar.get_x() + bar.get_width()/2, yval + 0.05, round(yval, 2), ha='center', va='bottom')
  61.  
  62. plt.tight_layout()
  63. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement