Setting up Environment


setup(data, transaction_id, item_id, ignore_items = None, session_id = None)


This function initializes the environment in pycaret. setup() must called before executing any other function in pycaret. It takes three mandatory parameters: (i) dataframe {array-like, sparse matrix}, (ii) transaction_id param identifying basket and (iii) item_id param used to create rules. These three params are normally found in any transactional dataset. pycaret will internally convert the dataframe into a sparse matrix which is required for association rules mining.

#import the dataset from pycaret repository
from pycaret.datasets import get_data
france = get_data('france')

#import association rule module
from pycaret.arules import *

#intialize the setup
exp_arules = setup(france, transaction_id = 'InvoiceNo', item_id = 'Description')



anomaly‘ is a pandas Dataframe.


data : dataframe
{array-like, sparse matrix}, shape (n_samples, n_features) where n_samples is the number of samples and n_features is the number of features.

transaction_id: string
Name of column representing transaction id. This will be used to pivot the matrix.

item_id: string
Name of column used for creation of rules. Normally, this will be the variable of interest.

ignore_items: list, default = None
list of strings to be ignored when considering rule mining.

session_id: int, default = None
If None, a random seed is generated and returned in the Information grid. The unique number is then distributed as a seed in all functions used during the experiment. This can be used for later reproducibility of the entire experiment.


Information Grid: Information grid is printed.

Environment: This function returns various outputs that are stored in variable as tuple. They are used by other functions in pycaret.


Create Model


create_model(metric=’confidence’, threshold = 0.5, min_support = 0.05, round = 4)


This function creates an association rules model using data and identifiers passed at setup stage. This function internally transforms the data for association rule mining. setup() function must be called before using create_model().

arules = create_model(metric = 'confidence')




metric : string, default = ‘confidence’
Metric to evaluate if a rule is of interest. Default is set to confidence. Other available metrics include ‘support’, ‘lift’, ‘leverage’, ‘conviction’. These metrics are computed as follows:

  • support(A->C) = support(A+C) [aka ‘support’], range: [0, 1]
  • confidence(A->C) = support(A+C) / support(A), range: [0, 1]
  • lift(A->C) = confidence(A->C) / support(C), range: [0, inf]
  • leverage(A->C) = support(A->C) – support(A)*support(C), range: [-1, 1]
  • conviction = [1 – support(C)] / [1 – confidence(A->C)], range: [0, inf]

threshold : float, default = 0.5
Minimal threshold for the evaluation metric, via the `metric` parameter, to decide whether a candidate rule is of interest.

min_support : float, default = 0.05
A float between 0 and 1 for minimum support of the itemsets returned. The support is computed as the fraction `transactions_where_item(s)_occur / total_transactions`.

round: integer, default = 4
Number of decimal places metrics in score grid will be rounded to.


Dataframe: Dataframe containing rules of interest with all metrics including antecedents, consequents, antecedent support, consequent support, support, confidence, lift, leverage, conviction.


  • Setting low values for min_support may increase training time.

Plot Model


plot_model(model, plot=’2d’)


This function takes a model dataframe returned by create_model() function. ‘2d’ and ‘3d’ plots are available.

# create a model
arules = create_model('arules')

# plot a model 




model : DataFrame, default = none
DataFrame returned by trained model using create_model().

plot : string, default = ‘2d’
Enter abbreviation of type of plot. The current list of plots supported are:

Estimator Abbrev. String
¬†Support, Confidence and Lift (2d) ‘2d’
Support, Confidence and Lift (3d) ‘3d’


Visual Plot: Prints the visual plot.


Was this page helpful?

Send feedback