<jupyter_start><jupyter_text>MIN APRIORI Apriori Algorithm     –Lk: frequent k-itemsets–Ck: candidate k-itemsets–Let k=1–Generate L1= {frequent 1-itemsets}–Repeat until Lkis emptyCandidate Generation: Generate Ck+1 from LkCandidate Pruning: Prune candidate itemsets in Ck+1 containing subsets of length k that are infrequentSupport Counting: Count the support of each candidate in Ck+1 by scanning the DBCandidate Elimination: Eliminate candidates in Ck+1 that are infrequent, leaving only those that are frequent => Lk+1<jupyter_code>import pandas as pd
import itertools 
crimes = pd.read_csv("./dataset/aggregated.csv")
crimes.head(n=5)
sum_crimes = []
for i in range(3,13):
    sumi=0
    for j in range(len(crimes)):
        sumi+=crimes.iloc[j][i]
    sum_crimes.append(sumi)
sum_crimes<jupyter_output><empty_output><jupyter_text>Converting the dataset into a format suitable to apply Min-Apriori concept.<jupyter_code>for i in range(len(crimes)):
    for j in range(3,13):
        x=5
        #print(crimes.iloc[i][j], sum_crimes[j-3])
        crimes.iat[i,j]=crimes.iat[i,j]/sum_crimes[j-3]
crimes.head(n=5)
crimes = crimes.drop(['State', 'Year'], axis=1)
crimes.head(n=10)
freq_itemsets = []
freq_itemsets.append([[i] for i in range(1,11)])
freq_itemsets
sup = {}
for i in range(1,11):
    sup[tuple([i])]=1
sup<jupyter_output><empty_output><jupyter_text><jupyter_code>def has_infrequent_subset(kitemset,kplusone_item, k):
    kitems=list(itertools.combinations(kplusone_item, k+1))
    kitems=[list(a) for a in kitems]
    for kitem in kitems:
        if kitem not in kitemset:
            return 1
    return 0<jupyter_output><empty_output><jupyter_text><jupyter_code>def apriori_gen(kitemset,k):
    pos_kplusone_itemset = []
    for i in range(len(kitemset)):
        for j in range(i+1,len(kitemset)):
            kplusone_item = list(set(kitemset[i]).union(set(kitemset[j])))
            if(len(kplusone_item)==k+2):
                flag=has_infrequent_subset(kitemset,kplusone_item, k)
                if(flag==0):
                    pos_kplusone_itemset.append(kplusone_item)
    
    pos_kplusone_itemset = set(tuple(sorted(sub)) for sub in pos_kplusone_itemset)
    pos_kplusone_itemset = [list(i) for i in pos_kplusone_itemset]
    return pos_kplusone_itemset<jupyter_output><empty_output><jupyter_text><jupyter_code>def apriori(min_sup):
    k=0
    while(1):
        kplusone_itemset = []
        kitemset = freq_itemsets[k]
        pos_kplusone_itemset = apriori_gen(kitemset,k)
        for kplusone_item in pos_kplusone_itemset:
            sumi=0
            for x in range(len(crimes)):
                mini=10000
                for y in kplusone_item:
                    mini=min(mini,crimes.iloc[x][y])
                sumi+=mini
            if sumi>=min_sup:
                sup[tuple(kplusone_item)]=sumi
                kplusone_itemset.append(kplusone_item)
        if(len(kplusone_itemset)==0):
           break
        #print(kplusone_itemset)
        freq_itemsets.append(kplusone_itemset)
        k=k+1
    return(freq_itemsets)
min_sup=0.35
freq_itemsets = apriori(min_sup)

crime = ["", "Murder", "Rape", "Foeticide", "Kidnapping and Abduction", "Abetment of suicide", "Exposure and abandonment", "Procuration of minor girls", "Selling of girls for prostitution", "Prohibition of child marriage act", "Other Crimes"]
for k_itemsets in freq_itemsets:
    for itemset in k_itemsets:
        print("[", end =" ")
        for item in itemset:
            print(crime[item]+", ", end =" ")
        print("]")
sup<jupyter_output><empty_output><jupyter_text><jupyter_code>def get_rules(min_conf):
    rules = []
    for i in range(1,len(freq_itemsets)):
        for itemset in freq_itemsets[i]:
            #print(itemset)
            for j in range(1, len(itemset)):
                listx=list(itertools.combinations(itemset, j))
                for x in listx:
                    conf = sup[tuple(itemset)]/sup[tuple(x)]
                    x=set(x)
                    y=set(itemset).difference(x)
                    if conf>=min_conf:
                        print("conf of",tuple([x,y]),"=",conf)
                        rules.append(tuple([x,y]))
    return rules
min_conf=0.5
rules = get_rules(min_conf)
#finalrules = []
for rule in rules:
    #finalrule = []
    a = rule[0]
    b=rule[1]
    for elem in a:
        print(crime[elem]+", ", end=" ")
        #finalrule.append(crime[elem])
    print("--->", end=" ")
    #finalrule.append("--->")
    for elem in b:
        print(crime[elem]+", ", end=" ")
        #finalrule.append(crime[elem])
    #finalrules.append(finalrule)
    print()<jupyter_output>Murder,  ---> Rape,  
Rape,  ---> Murder,  
Murder,  ---> Kidnapping and Abduction,  
Kidnapping and Abduction,  ---> Murder,  
Rape,  ---> Other Crimes,  
Other Crimes,  ---> Rape,  
Rape,  ---> Kidnapping and Abduction,  
Kidnapping and Abduction,  ---> Rape,  
Murder,  Rape,  ---> Other Crimes,  
Murder,  Other Crimes,  ---> Rape,  
Rape,  Other Crimes,  ---> Murder,  
Murder,  Rape,  ---> Kidnapping and Abduction,  
Murder,  Kidnapping and Abduction,  ---> Rape,  
Rape,  Kidnapping and Abduction,  ---> Murder,
{'fraction_non_alphanumeric': 0.12947551770853494, 'fraction_numerical': 0.01064447455002903, 'mean_word_length': 2.9662317728319265, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '472961', 'license': 'MIT', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1850, 'n_words': 378, 'path': 'min_apriori.ipynb', 'repo_name': 'PranjalGupta2199/crime-analysis-report', 'size': '5167'}