1. import numpy as np
  2. def flag_anomalies(data, threshold=3, retries=3):
  3. """
  4. Flags anomalies in a collection of numerical data with retry logic.
  5. Args:
  6. data (list or numpy array): The collection of numerical data.
  7. threshold (float): The number of standard deviations from the mean to consider an anomaly.
  8. retries (int): Number of retry attempts.
  9. Returns:
  10. list: A list of indices where anomalies are detected. Returns an empty list if no anomalies are found after retries.
  11. """
  12. data = np.array(data) # Convert to numpy array for easier calculations
  13. anomalies = []
  14. for attempt in range(retries):
  15. try:
  16. mean = np.mean(data)
  17. std = np.std(data)
  18. #Identify anomalies
  19. anomalies = np.where(np.abs(data - mean) > threshold * std)[0].tolist()
  20. if anomalies:
  21. break # Exit retry loop if anomalies are found
  22. except Exception as e:
  23. print(f"Attempt {attempt + 1} failed: {e}")
  24. if attempt < retries - 1:
  25. # Retry the process
  26. pass # continue to the next attempt
  27. else:
  28. #If all retries failed, return empty list
  29. return []
  30. return anomalies
  31. if __name__ == '__main__':
  32. # Example Usage
  33. data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
  34. anomalies = flag_anomalies(data)
  35. print(f"Anomalies found at indices: {anomalies}")
  36. data2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  37. anomalies2 = flag_anomalies(data2)
  38. print(f"Anomalies found at indices: {anomalies2}")
  39. data3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000]
  40. anomalies3 = flag_anomalies(data3, threshold=2, retries=5)
  41. print(f"Anomalies found at indices: {anomalies3}")

Add your comment