You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多目标关联规则挖掘需求:求Python/MATLAB版完整NSGA-II实现

Hey there! Let me break down what you're looking for into actionable parts for both Python and MATLAB, plus some robust off-the-shelf implementations you can leverage for your multi-objective association rule mining work with NSGA-II.

Python Implementation for NSGA-II (Multi-Objective Association Rule Mining)

Python has great libraries that make implementing NSGA-II straightforward, and they’re built to handle standard datasets (like market basket data from UCI or Kaggle). The DEAP library is a go-to for evolutionary algorithms, and it has native support for NSGA-II.

Step-by-Step Example

First, install DEAP and dependencies:

pip install deap pandas numpy

Here’s a complete skeleton that takes a standard transactional dataset as input, defines multi-objective fitness (e.g., maximize support/confidence, minimize rule length), and runs NSGA-II:

import random
import pandas as pd
import numpy as np
from deap import base, creator, tools, algorithms

# Load your standard dataset (CSV of transactions, each row is a list of items)
def load_dataset(file_path):
    df = pd.read_csv(file_path)
    transactions = df.apply(lambda x: x.dropna().tolist(), axis=1).tolist()
    return transactions

transactions = load_dataset("your_dataset.csv")
all_items = list(set(item for trans in transactions for item in trans))

# Define multi-objective fitness: maximize support/confidence, minimize rule length
creator.create("FitnessMulti", base.Fitness, weights=(1.0, 1.0, -1.0))
creator.create("Individual", list, fitness=creator.FitnessMulti)

toolbox = base.Toolbox()
# Attribute generator: random 0/1 to represent item presence in antecedent/consequent
toolbox.register("attr_item", random.randint, 0, 1)
# Individual creator: length = 2*total items (split into antecedent + consequent)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, n=2*len(all_items))
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

def evaluate(individual):
    split_idx = len(all_items)
    ant_mask = individual[:split_idx]
    cons_mask = individual[split_idx:]
    
    antecedent = [all_items[i] for i, val in enumerate(ant_mask) if val == 1]
    consequent = [all_items[i] for i, val in enumerate(cons_mask) if val == 1]
    
    if not antecedent or not consequent:
        return (0.0, 0.0, float('inf'))  # Invalid rule
    
    # Calculate support: percentage of transactions containing both sets
    total_trans = len(transactions)
    support_count = sum(1 for trans in transactions if set(antecedent).issubset(trans) and set(consequent).issubset(trans))
    support = support_count / total_trans
    
    # Calculate confidence: support / support of antecedent
    ant_support_count = sum(1 for trans in transactions if set(antecedent).issubset(trans))
    confidence = support_count / ant_support_count if ant_support_count > 0 else 0.0
    
    # Rule length: total items in antecedent + consequent
    rule_length = len(antecedent) + len(consequent)
    
    return (support, confidence, rule_length)

# Register genetic operators
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selNSGA2)
toolbox.register("evaluate", evaluate)

def main():
    random.seed(42)
    pop = toolbox.population(n=50)
    NGEN = 20
    CXPB = 0.7
    MUTPB = 0.2
    
    # Evaluate initial population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    for gen in range(NGEN):
        print(f"Generation {gen+1}/{NGEN}")
        # Select next generation
        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))
        
        # Apply crossover and mutation
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
        
        # Evaluate invalid individuals
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        
        # Replace population with offspring
        pop[:] = offspring
    
    # Extract and display top Pareto-front rules
    pareto_front = tools.selNSGA2(pop, len(pop))
    for ind in pareto_front[:5]:
        ant = [all_items[i] for i, val in enumerate(ind[:len(all_items)]) if val ==1]
        cons = [all_items[i] for i, val in enumerate(ind[len(all_items):]) if val ==1]
        print(f"Rule: {ant} -> {cons} | Support: {ind.fitness.values[0]:.4f} | Confidence: {ind.fitness.values[1]:.4f} | Length: {ind.fitness.values[2]}")

if __name__ == "__main__":
    main()

This code works with any standard transactional CSV—just adjust the dataset loading logic if your file uses a different format. You can easily tweak the fitness objectives (e.g., add lift or conviction) to match your research goals.

MATLAB Implementation for NSGA-II

For MATLAB, you have two reliable paths:

  • Global Optimization Toolbox: The gamultiobj function follows NSGA-II principles and is a battle-tested option. You can wrap your association rule evaluation logic into a fitness function and pass it directly.
  • Open-Source Community Implementations: MATLAB File Exchange has full NSGA-II implementations designed for custom optimization tasks. Look for versions that support dataset input and let you customize fitness functions.

Quick MATLAB Skeleton

% Load standard transactional dataset (as cell array)
transactions = readtable('your_dataset.csv');
transactions = table2cell(transactions);
allItems = unique([transactions{:}]);
allItems = allItems(~cellfun(@isempty, allItems));

% Fitness function: minimize negative support/confidence, minimize rule length
function fitness = ruleFitness(individual, transactions, allItems)
    splitIdx = length(allItems);
    antMask = individual(1:splitIdx) > 0.5;
    consMask = individual(splitIdx+1:end) > 0.5;
    
    antecedent = allItems(antMask);
    consequent = allItems(consMask);
    
    if isempty(antecedent) || isempty(consequent)
        fitness = [0, 0, inf];
        return;
    end
    
    % Calculate support
    totalTrans = length(transactions);
    supportCount = sum(cellfun(@(x) all(ismember(antecedent, x)) && all(ismember(consequent, x)), transactions));
    support = supportCount / totalTrans;
    
    % Calculate confidence
    antSupportCount = sum(cellfun(@(x) all(ismember(antecedent, x)), transactions));
    confidence = supportCount / antSupportCount;
    
    % Rule length
    ruleLength = length(antecedent) + length(consequent);
    
    % gamultiobj minimizes, so invert maximization objectives
    fitness = [-support, -confidence, ruleLength];
end

% Set up NSGA-II-style optimization
options = optimoptions('gamultiobj', ...
    'PopulationSize', 50, ...
    'MaxGenerations', 20, ...
    'SelectionFcn', @selectiontournament, ...
    'CrossoverFcn', @crossover两点, ...
    'MutationFcn', @mutationgaussian);

numVars = 2 * length(allItems);
paretoFront = gamultiobj(@(x) ruleFitness(x, transactions, allItems), numVars, options);

% Display top rules from Pareto front
for i = 1:min(5, size(paretoFront, 1))
    antMask = paretoFront(i, 1:length(allItems)) > 0.5;
    consMask = paretoFront(i, length(allItems)+1:end) > 0.5;
    ant = allItems(antMask);
    cons = allItems(consMask);
    fprintf('Rule: %s -> %s | Support: %.4f | Confidence: %.4f | Length: %.0f\n', ...
        strjoin(ant, ', '), strjoin(cons, ', '), ...
        -ruleFitness(paretoFront(i,:), transactions, allItems)(1), ...
        -ruleFitness(paretoFront(i,:), transactions, allItems)(2), ...
        ruleFitness(paretoFront(i,:), transactions, allItems)(3));
end
Feature-Complete NSGA-II Implementations

If you don’t want to build from scratch, these are your best bets:

  • Python:
    • pymoo: A highly optimized multi-objective library with a production-ready NSGA-II implementation. It supports parallel computing, advanced termination criteria, and easy integration with custom association rule fitness functions.
    • DEAP: As shown above, it’s flexible and lets you tweak every part of the NSGA-II pipeline—perfect for research where you need to experiment with evolutionary operators.
  • MATLAB:
    • gamultiobj (Global Optimization Toolbox): Has built-in Pareto front visualization and parameter tuning tools, making it easy to iterate on your mining logic.
    • Community NSGA-II implementations (File Exchange): Look for packages that include elite preservation, adaptive crossover/mutation rates, and dataset input handling—many are tailored for academic multi-objective mining research.

内容的提问来源于stack exchange,提问作者Akla Meghla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:37:32