Python Implementation of the Apriori algorithm and python

Source: Internet
Author: User

Python Implementation of the Apriori algorithm and python

The Apriori algorithm is the originator of Data Mining in the intermediate frequency mode. It became popular since 1960s. Its algorithm concept is also very simple and simple. First, it mines the frequent mode with a length of 1, and then k = 2.

These frequent patterns merge the composition length into k frequent patterns, calculate their frequency, and ensure that all of its K-1 length subsets are also frequent, it is worth noting that, to avoid duplication, when merging, only those first K-2 characters are merged, while the character side of the k-1 is less than the other side.

The Python Implementation of the algorithm is as follows:

__author__ = 'linfuyuan'min_frequency = int(raw_input('please input min_frequency:'))file_name = raw_input('please input the transaction file:')transactions = []def has_infrequent_subset(candidate, Lk):    for i in range(len(candidate)):        subset = candidate[:-1]        subset.sort()        if not ''.join(subset) in Lk:            return False        lastitem = candidate.pop()        candidate.insert(0, lastitem)    return Truedef countFrequency(candidate, transactions):    count = 0    for transaction in transactions:        if transaction.issuperset(candidate):            count += 1    return countwith open(file_name) as f:    for line in f.readlines():        line = line.strip()        tokens = line.split(',')        if len(tokens) > 0:            transaction = set(tokens)            transactions.append(transaction)currentFrequencySet = {}for transaction in transactions:    for item in transaction:        time = currentFrequencySet.get(item, 0)        currentFrequencySet[item] = time + 1Lk = set()for (itemset, count) in currentFrequencySet.items():    if count >= min_frequency:        Lk.add(itemset)print ', '.join(Lk)while len(Lk) > 0:    newLk = set()    for itemset1 in Lk:        for itemset2 in Lk:            cancombine = True            for i in range(len(itemset1)):                if i < len(itemset1) - 1:                    cancombine = itemset1[i] == itemset2[i]                    if not cancombine:                        break                else:                    cancombine = itemset1[i] < itemset2[i]                    if not cancombine:                        break            if cancombine:                newitemset = []                for char in itemset1:                    newitemset.append(char)                newitemset.append(itemset2[-1])                if has_infrequent_subset(newitemset, Lk) and countFrequency(newitemset, transactions) >= min_frequency:                    newLk.add(''.join(newitemset))    print ', '.join(newLk)    Lk = newLk



Use C to implement the code of the basic apriori algorithm

When I wrote my homework, I designed the data structure as follows:
# Include <stdio. h>
# Include <string. h>
# Include <malloc. h>
# Define ITEM_NAME_LENGTH 20
# Define MIN_SUPPORT 2
// Item set structure
Struct ITEMSET
{
Char itemName [ITEM_NAME_LENGTH];
Struct ITEMSET * next;
};
// Database structure
Struct TRANSACTION
{
Unsigned int tranID;
Struct ITEMSET * itemPoint;
Struct TRANSACTION * next;
};
// Large project set structure
Struct BIGITEMSET
{
Struct ITEMSET * itemPoint;
Unsigned int count;
Struct BIGITEMSET * next;
};
// The following is the database
Char * tran1 [3] = {"1", "3", "4 "};
Char * tran2 [3] = {"2", "3", "5 "};
Char * tran3 [4] = {"1", "2", "3", "5 "};
Char * tran4 [2] = {"2", "5 "};
// The following is the variable Declaration
Struct TRANSACTION * tranHead;
Struct BIGITEMSET * bigHead;
Struct BIGITEMSET * test;
Struct BIGITEMSET * subSetHeadC1, * subSetHeadC2;
It is not difficult to write a program after you really understand the algorithm.

What programs are used to implement the apriori algorithm?

What language are you talking about? This is not the case. Since it is an algorithm, it can be implemented in any language.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.