Advertisement
Guest User

pattern matching code

a guest
Sep 13th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #!pip install stumpy
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import stumpy
  6. import numpy as np
  7.  
  8. data = pd.read_csv('https://docs.google.com/spreadsheets/d/1SQfhz7_FLaA7aADKdMrf2SL1p2TgQQfnH-earg1eOCY/export?gid=0&format=csv')
  9.  
  10. first_data = data["price"]
  11. second_data = data["Market Cap"]
  12.  
  13. m = 30
  14. Q = first_data
  15. T = second_data
  16.  
  17. distance_profile = stumpy.mass(Q, T, normalize=False)
  18. idx = np.argmin(distance_profile)
  19.  
  20.  
  21. time_series = first_data
  22.  
  23. # Number of latest points to highlight
  24. highlight_points = 30
  25.  
  26. # Range to mark
  27. start_range = idx
  28. end_range = idx + m
  29.  
  30. # Create the first plot with the gray rectangle for the latest 30 values
  31. plt.figure(figsize=(10, 6))
  32. plt.subplot(2, 1, 1)
  33. plt.plot(time_series, label='Time Series')
  34. latest_data = time_series[-highlight_points:]
  35. x_coordinates = np.arange(len(time_series) - highlight_points, len(time_series))
  36. y_min = np.min(time_series)
  37. y_max = np.max(time_series)
  38. rect = plt.Rectangle(
  39. (x_coordinates[0], y_min),
  40. highlight_points,
  41. y_max - y_min,
  42. fill=True,
  43. color='gray',
  44. alpha=0.5,
  45. )
  46. plt.gca().add_patch(rect)
  47. plt.xlabel('Time')
  48. plt.ylabel('Value')
  49. plt.title('Time Series with Gray Rectangle for Latest 30 Values (first_data)')
  50. plt.legend()
  51.  
  52. # Create the second plot with the gray rectangle for the specified range
  53. plt.subplot(2, 1, 2)
  54. time_series = second_data
  55. plt.plot(time_series, label='Time Series')
  56. plt.axvspan(start_range, end_range, color='gray', alpha=0.5)
  57. plt.xlabel('Time')
  58. plt.ylabel('Value')
  59. plt.title('The other graph in which I have to find the similar pattern (second_data)')
  60. plt.legend()
  61.  
  62. # Adjust layout
  63. plt.tight_layout()
  64.  
  65. # Show the plots
  66. plt.show()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement