π¬ 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
DataFrameComputes box plot statistics (
min,Q1,median,Q3,max,outliers) for selected featuresSupports 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 namemin: Minimum valueQ1: First quartile (25th percentile)median: Median (50th percentile)Q3: Third quartile (75th percentile)max: Maximum valueoutliers_count: Number of detected outliersoutliers: 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 isFalse.
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