Script 989: Campaigns Anomaly

Purpose

Campaigns Anomaly

To Elaborate

The Python script solves the problem of identifying anomalies in campaign metrics data. It loads data from an ad publisher, calculates the rolling average for a specified metric, identifies anomalies based on the difference between the metric and rolling average, and generates a report with the identified anomalies.

Walking Through the Code

  1. The script defines two data processing functions: load_data_from_publisher and calculate_rolling_average.
    • load_data_from_publisher loads data from the ad publisher using the specified data source.
    • calculate_rolling_average calculates the rolling average for the specified metric in the data.
  2. The script defines an anomaly detection function: identify_anomalies.
    • identify_anomalies identifies anomalies based on the difference between the metric and rolling average.
  3. The script defines a report generation function: generate_report.
    • generate_report generates a report based on the data and identified anomalies.
  4. The main execution of the script:
    • Loads data from the data source using load_data_from_publisher and assigns it to the variable data.
    • Calculates the rolling average on a copy of the data using calculate_rolling_average and assigns it back to data.
    • Identifies anomalies using identify_anomalies with a specified threshold and assigns the result to anomalies.
    • Generates a report using generate_report with the data and identified anomalies.

Vitals

  • Script ID : 989
  • Client ID / Customer ID: 1306916981 / 60268647
  • Action Type: Email Report
  • Item Changed: None
  • Output Columns:
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Arayla Caldwell (acaldwell@marinsoftware.com)
  • Created by Arayla Caldwell on 2024-04-18 21:45
  • Last Updated by Arayla Caldwell on 2024-04-18 22:58
> See it in Action

Python Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##
## name: Campaigns Anomaly 
## description:
##  
## 
## author: 
## created: 2024-04-18
## 

# Data processing functions (replace with your actual logic)
def load_data_from_publisher(data_source):
  """
  Loads data from the ad publisher using the specified data source.

  Args:
      data_source (str): The data source to use (e.g., API endpoint, database connection string).

  Returns:
      pd.DataFrame: The loaded data as a pandas DataFrame.
  """
  # Replace with your implementation to fetch data from the ad publisher API/database
  # Example placeholder data
  data = {
      "date": ["2024-04-10", "2024-04-11", "2024-04-12", "2024-04-13", "2024-04-14"],
      "metric": [100, 120, 95, 150, 80]
  }
  return pd.DataFrame(data)

def calculate_rolling_average(data, window_size):
  """
  Calculates the rolling average for the specified metric in the data.

  Args:
      data (pd.DataFrame): The data containing the metric column.
      window_size (int): The window size for the rolling average calculation.

  Returns:
      pd.DataFrame: The data with an added column for rolling average.
  """
  data["rolling_average"] = data["metric"].rolling(window=window_size).mean()
  return data

# Anomaly detection function (replace with your desired logic)
def identify_anomalies(data, threshold):
  """
  Identifies anomalies based on the difference between the metric and rolling average.

  Args:
      data (pd.DataFrame): The data containing metric and rolling average columns.
      threshold (float): The threshold for anomaly detection.

  Returns:
      list: A list of anomaly indices in the data.
  """
  anomaly_indices = []
  for i in range(len(data)):
    if abs(data.loc[i, "metric"] - data.loc[i, "rolling_average"]) > threshold:
      anomaly_indices.append(i)
  return anomaly_indices

# Report generation function (replace with your actual logic)
def generate_report(data, anomalies):
  """
  Generates a report based on the data and identified anomalies.

  Args:
      data (pd.DataFrame): The data used for the report.
      anomalies (list): A list of anomaly indices in the data.
  """
  # Replace with your implementation to generate a report (e.g., CSV, email)
  # Example: Print a summary to the console
  print(f"Total records: {len(data)}")
  print(f"Anomaly count: {len(anomalies)}")
  for i in anomalies:
    print(f"Anomaly detected at index: {i}, value: {data.loc[i, 'metric']}")

# Main execution
data = load_data_from_publisher("your_data_source")  # Replace with actual data source
data = calculate_rolling_average(data.copy(), window_size=3)  # Calculate rolling average on a copy
anomalies = identify_anomalies(data, threshold=10)
generate_report(data, anomalies)

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus