πŸ”¬ analysis.box_plotΒΆ

Box plot analysis is a robust method for visualizing the distribution and identifying outliers in numerical data. This module provides tools to compute box plot statistics for selected columns in a pandas DataFrame, supporting both IQR and Z-score methods for outlier detection.

The BoxPlotAnalyzer class offers a flexible interface for generating box plot statistics, with configurable methods, thresholds, and precision.

OverviewΒΆ

The module exposes:

  • A parameter dataclass for flexible configuration

  • An analyzer class that:

    • Accepts a pandas DataFrame

    • Computes box plot statistics (min, Q1, median, Q3, max, outliers) for selected features

    • Supports both IQR and Z-score outlier detection methods

    • Allows configurable precision and outlier thresholds

    • Returns structured output for downstream analysis or visualization


Class ReferenceΒΆ

class owlmix.analysis.box_plot.BoxPlotParams(columns=None, method='iqr', threshold=None, precision=2)ΒΆ

Dataclass for specifying box plot analysis parameters.

Parameters:
  • columns (Optional[List[str]]) – List of column names to include in the analysis. If None, all numeric columns are used.

  • method (str) – Method to identify outliers. Options are 'iqr' (Interquartile Range) and 'zscore' (Z-score method). Default is 'iqr'.

  • threshold (Optional[float]) – Threshold for identifying outliers. For 'iqr', it’s the multiplier for the IQR (default 1.5). For 'zscore', it’s the Z-score threshold (default 3.0).

  • precision (int) – Number of decimal places to round statistics. Default is 2.

Raises:

ValueError – If an unsupported method is provided or precision is negative.


class owlmix.analysis.box_plot.BoxPlotAnalyzer(df, params)ΒΆ

Analyzer for creating box plot data from a DataFrame.

Parameters:
  • df (pandas.DataFrame) – Input DataFrame containing the data.

  • params (BoxPlotParams) – Configuration parameters for box plot analysis.

compute()ΒΆ

Computes box plot statistics for each selected column.

Returns:

A list of dictionaries, each containing:
  • column: Column name

  • min: Minimum value

  • Q1: First quartile (25th percentile)

  • median: Median (50th percentile)

  • Q3: Third quartile (75th percentile)

  • max: Maximum value

  • outliers_count: Number of detected outliers

  • outliers: List of outlier values

Return type:

list[dict]

print_results_json(results=None, indent=2)ΒΆ

Prints the results in JSON format.

Parameters:
  • results (list[dict], optional) – The results to print. If None, uses the computed results.

  • indent (int) – Indentation level for pretty-printing the JSON.

print_results(results=None, include_outliers=False)ΒΆ

Prints the results in a human-readable tabular format.

Parameters:
  • results (list[dict], optional) – The results to print. If None, uses the computed results.

  • include_outliers (bool) – Whether to include the list of outlier values in the table. Default is False.


Usage ExampleΒΆ

Below is a simple example of how to use the analyzer:

import pandas as pd
from owlmix.utils.sample_data_generator import create_sample_data
from owlmix.analysis.box_plot import BoxPlotAnalyzer, BoxPlotParams

df = create_sample_data(n=100)
params = BoxPlotParams(
    method="zscore",  # or "iqr"
    precision=2
)
analyzer = BoxPlotAnalyzer(df, params)
result = analyzer.compute()
analyzer.print_results_json(result)
analyzer.print_results(result, include_outliers=False)

Result Example

Result Output - analyzer.print_results_json(result)

[
    {
        "column": "tv_spend",
        "min": 102,
        "Q1": 228,
        "median": 299,
        "Q3": 408,
        "max": 498,
        "outliers_count": 0,
        "outliers": []
    },
    {
        "column": "digital_spend",
        "min": 50,
        "Q1": 115.75,
        "median": 166,
        "Q3": 242.25,
        "max": 294,
        "outliers_count": 0,
        "outliers": []
    },
    ...
]

Result Output - analyzer.print_results(result, include_outliers=False)

Column            Min      Q1    Median      Q3    Max    Outliers Count
-------------  ------  ------  --------  ------  -----  ----------------
tv_spend          102     228       299     408    498                 0
digital_spend      50  115.75       166  242.25    294                 0
radio_spend        20      53        83  108.75    149                 0
tv_grp             10   28.75      45.5      79     96                 0
radio_grp          21    53.5      91.5  120.25    149                 0
digital_imp        11   31.75      54.5   75.25     98                 0
radio_imp          20      54      92.5  119.25    149                 0
inflation          10      32        50   72.25     97                 0
sales          124.34  194.83       235   281.7  364.8                 0

NotesΒΆ

  • Only numeric columns are processed; non-numeric columns are ignored.

  • Outlier detection can be performed using either the IQR or Z-score method.

  • If no columns are specified, all numeric columns in the DataFrame are analyzed.

  • The threshold parameter controls the sensitivity of outlier detection.

  • Results can be printed in both JSON and tabular formats.

DependenciesΒΆ

  • pandas

  • numpy

  • tabulate

ReferencesΒΆ

See AlsoΒΆ

Back to Home