MarketBasketAnalysis 1.0.3

dotnet add package MarketBasketAnalysis --version 1.0.3
                    
NuGet\Install-Package MarketBasketAnalysis -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MarketBasketAnalysis" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MarketBasketAnalysis" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="MarketBasketAnalysis" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MarketBasketAnalysis --version 1.0.3
                    
#r "nuget: MarketBasketAnalysis, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MarketBasketAnalysis@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MarketBasketAnalysis&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=MarketBasketAnalysis&version=1.0.3
                    
Install as a Cake Tool

Market Basket Analysis

High-performance library for discovering and analyzing association rules from transactional data

Project Background

This library is a non-commercial reimplementation of the business logic from the original Market Basket Analysis system developed for the Komandor retail chain in 2019–2020 that is confirmed by an official letter from the company's Project & Analytics Office. The original system was designed for discovering and analyzing association rules from retail transaction data and included features such as rule quality metrics, generalized rule mining, graph analysis, and rule set comparison.

Features

  • Association Rule Mining: discover rules based on user-defined support and confidence thresholds
  • Rule Quality Metrics: support, confidence, lift, conviction, Yule's Q coefficient, φ (phi) correlation coefficient, χ² (chi-squared) test statistic
  • Pattern-Based Item Exclusion: dynamically exclude specific items from the mining process using item exlusion rules
  • Generalized Rule Mining: lift rules to higher abstraction levels using item conversion rules
  • Graph Analysis: detect maximal cliques of user-specified size in the association rule graph
  • Rule Set Operations: compute intersection and difference between two rule sets

Prerequisites

  • .NET Standard 2.0

Installation

Package Manager

Install-Package MarketBasketAnalysis -Version 1.0.3

.NET CLI

dotnet add package MarketBasketAnalysis --version 1.0.3

Quick Start

using MarketBasketAnalysis;
using MarketBasketAnalysis.AssociationRuleMining.Contracts;
using MarketBasketAnalysis.Models;
using Microsoft.Extensions.DependencyInjection;

// 1. Define items
var beef = new Item(1, "Beef");
var chicken = new Item(2, "Chicken");
var milk = new Item(3, "Milk");
var cheese = new Item(4, "Cheese");
var boots = new Item(5, "Boots");
var clothes = new Item(6, "Clothes");

// 2. Define transactions
IEnumerable<Item[]> transactions =
[
    [beef, chicken, milk],
    [beef, cheese],
    [cheese, boots],
    [boots, chicken, cheese],
    [beef, chicken, clothes, cheese, milk],
    [clothes, chicken, milk],
    [chicken, milk, clothes],
];

// 3. Configure DI container
var services = new ServiceCollection();
services.AddMarketBasketAnalysis();
await using var serviceProvider = services.BuildServiceProvider();

// 4. Create miner instance
var minerFactory = serviceProvider.GetRequiredService<IMinerFactory>();
var miner = minerFactory.Create();

// 5. Configure mining parameters
var miningParameters = new MiningParameters(minSupport: 0.4, minConfidence: 0.5);

// 6. Mine association rules
var associationRules = miner.Mine(transactions, miningParameters);

// 7. Output discovered association rules
foreach (var associationRule in associationRules)
{
    Console.WriteLine($"{associationRule}: support {associationRule.Support:f2}, confidence {associationRule.Confidence:f2}");
}

// Output (order may vary):
// Clothes -> Chicken: support 0.43, confidence 1.00
// Chicken -> Clothes: support 0.43, confidence 0.60
// Clothes -> Milk: support 0.43, confidence 1.00
// Milk -> Clothes: support 0.43, confidence 0.75
// Milk -> Chicken: support 0.57, confidence 1.00
// Chicken -> Milk: support 0.57, confidence 0.80

Examples

Additional usage examples demonstrating different library features are available in the examples directory.

Benchmarks

Association rule mining performance was measured on the Instacart Market Basket Analysis dataset containing over 3 million transactions and ~50,000 distinct items.

Environment: Windows 11, Intel Core i5-12500H (12 physical / 16 logical cores), .NET 10, MinConfidence = 0.01, 1 warmup + 3 measured iterations.

The table shows mining time and allocated memory for different combinations of MinSupport and MaxDegreeOfParallelism:

MinSupport AssociationRules DOP = 1 DOP = 4 DOP = 8 DOP = 16
0.01 28 15.1 s / 2.30 GB 6.2 s / 2.31 GB 5.2 s / 2.31 GB 5.6 s / 2.31 GB
0.001 3634 44.0 s / 2.53 GB 15.0 s / 2.53 GB 10.8 s / 2.47 GB 9.3 s / 2.54 GB
0.0001 79572 144.2 s / 4.67 GB 55.4 s / 4.67 GB 39.3 s / 4.95 GB 30.0 s / 4.67 GB
0.00001 805303 154.3 s / 6.99 GB 69.6 s / 6.99 GB 60.0 s / 7.56 GB 47.4 s / 6.99 GB

Key takeaways:

  • Mining scales well with parallelism: up to ~4.8x faster at DOP = 16 compared to DOP = 1
  • Allocated memory depends mainly on MinSupport: as it decreases from 0.01 to 0.00001, the number of discovered association rules grows from 28 to ~805K and allocated memory increases from ~2.3 GB to ~7 GB

Benchmark sources are available in the benchmarks directory.

Documentation

Detailed class documentation are available in the API Reference

License

MIT License - See LICENSE

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Fixed bug in generalized association rule mining