# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
from collections import defaultdict

class Eclat:
    def __init__(self, min_support=0.5):
        self.min_support = min_support

    def fit(self, transactions):
        self.transactions = transactions
        self.items = self._get_unique_items(transactions)
        self.itemsets = self._get_frequent_itemsets(transactions)

    def _get_unique_items(self, transactions):
        items = set()
        for transaction in transactions:
            items.update(transaction)
        return items

    def _get_frequent_itemsets(self, transactions):
        itemsets = defaultdict(int)
        for transaction in transactions:
            for item in self.items:
                if item in transaction:
                    itemsets[frozenset([item])] += 1

        num_transactions = len(transactions)
        frequent_itemsets = {}
        for itemset, support in itemsets.items():
            if support / num_transactions >= self.min_support:
                frequent_itemsets[itemset] = support

        return frequent_itemsets

    def _join_itemsets(self, itemset1, itemset2):
        return itemset1.union(itemset2)

    def _get_candidate_itemsets(self, frequent_itemsets):
        candidate_itemsets = {}
        for itemset1 in frequent_itemsets:
            for itemset2 in frequent_itemsets:
                if len(itemset1.union(itemset2)) == len(itemset1) + 1:
                    candidate_itemset = self._join_itemsets(itemset1, itemset2)
                    if candidate_itemset not in candidate_itemsets:
                        candidate_itemsets[candidate_itemset] = 0
        return candidate_itemsets

    def _count_support(self, candidate_itemsets):
        itemsets = defaultdict(int)
        for transaction in self.transactions:
            for itemset in candidate_itemsets:
                if itemset.issubset(transaction):
                    itemsets[itemset] += 1
        return itemsets

    def _prune_itemsets(self, candidate_itemsets, min_support):
        pruned_itemsets = {}
        num_transactions = len(self.transactions)
        for itemset, support in candidate_itemsets.items():
            if support / num_transactions >= min_support:
                pruned_itemsets[itemset] = support
        return pruned_itemsets

    def _generate_association_rules(self, frequent_itemsets):
        association_rules = []
        for itemset in frequent_itemsets:
            if len(itemset) > 1:
                for i in range(1, len(itemset)):
                    for subset in itertools.combinations(itemset, i):
                        antecedent = set(subset)
                        consequent = itemset.difference(antecedent)
                        support_itemset = frequent_itemsets[itemset]
                        support_antecedent = frequent_itemsets[frozenset(antecedent)]
                        confidence = support_itemset / support_antecedent
                        association_rules.append((antecedent, consequent, confidence))
        return association_rules

    def print_frequent_itemsets(self):
        for itemset, support in self.itemsets.items():
            print(f"Itemset: {list(itemset)}, Support: {support}")

    def print_association_rules(self):
        association_rules = self._generate_association_rules(self.itemsets)
        for antecedent, consequent, confidence in association_rules:
            print(f"Rule: {list(antecedent)} => {list(consequent)}, Confidence: {confidence}")

# Example usage:
transactions = [
    {'milk', 'bread', 'butter'},
    {'milk', 'bread'},
    {'milk', 'butter'},
    {'bread', 'butter'},
    {'milk', 'bread', 'butter'},
    {'tea', 'coffee'},
    {'coffee', 'sugar'},
    {'tea', 'sugar'},
    {'tea', 'coffee', 'sugar'}
]

eclat = Eclat(min_support=0.5)
eclat.fit(transactions)
eclat.print_frequent_itemsets()

 

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling