Skip to content

Commit

Permalink
Add DES python example
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaVolgushev committed Jan 7, 2025
1 parent dd75632 commit f508c0e
Show file tree
Hide file tree
Showing 2 changed files with 290 additions and 0 deletions.
130 changes: 130 additions & 0 deletions examples/basic/mining_nar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import desbordante
import pandas
from tabulate import tabulate

TABLE = 'examples/datasets/dog_breeds.csv'
CLR = {
'bold_default': '\033[1m',
'bold_red': '\u001b[1;31m',
'bold_green': '\033[1;32m',
'bold_yellow': '\033[1;33m',
'bold_blue': '\033[1;34m',
'gray': '\033[90m',
'default': '\033[0m',
'blue_bg': '\033[1;46m',
'default_bg': '\033[49m',
'red_bg': '\033[41m'
}

def print_table(table, title=None):
if title is not None:
print(title)

print(tabulate(table, tablefmt='pipe', stralign='center'))


def print_map(map, table):
column_names = list(table.columns)
for key, value in map.items():
print(column_names[key], end='')
print(value)


def print_10_discovered_nars(algo, table):
counter = 1
for nar in algo.get_nars()[:10]:
print("NAR " + str(counter) + f":{CLR['bold_default']}")
counter += 1
print_map(nar.ante, pdtable)
print(" |\n |\n V")
print_map(nar.cons, pdtable)
print(" support = ", nar.support)
print(" confidence = ", nar.confidence)
print(f"{CLR['default']}")


if __name__ == '__main__':
algo = desbordante.nar.algorithms.des()
algo.load_data(table=(TABLE, ',', True))
print("Numerical Association Rules (NAR) are an extension of traditional "
"Association Rules (AR), which search for patterns in data. Unlike ARs, "
"which work with binary attributes (e.g., whether an item was purchased "
"or not), NARs can handle numerical data (e.g., how many units of an item "
"were purchased). This makes NARs more flexible for discovering relationships "
"in datasets with numerical data.")
print("Suppose we have a table containing students' exam grades and how many "
"hours they studied for the exam. Such a table might hold the following "
"numerical association rule:\n")
print(f"{CLR['bold_default']}Study_Hours[15.5 - 30.2] {CLR['gray']}⎤-Antecedent")
print(f"{CLR['default']}{CLR['bold_default']}Subject[Topology] {CLR['gray']}⎦")
print(f"{CLR['default']}{CLR['bold_default']} |\n |\n V")
print(f"Grade[3 - 5]] {CLR['gray']}]-Consequent")
print(f"{CLR['default']}{CLR['bold_default']} support = ", 0.21)
print(" confidence = ", 0.93)
print()
print(f"{CLR['default']}This rule states that students who study Topology for "
"between 15.5 and 30.2 hours will receive a grade between 3 and 5. This "
"rule has a support measure of 0.21, which means that 21% of rows in the dataset "
"satisfy both the antecedent's and consequent's requirements. This rule also "
"has a confidence measure of 0.93, meaning that 93% of rows that satisfy the "
"antecedent also satisfy the consequent. Note that attributes can be integers, "
"floating points, or strings.\n")
print("Desbordante implements an algorithm called \"Differential Evolution Solver\" "
"(DES), described by Iztok Fister et al. in \"uARMSolver: A framework for "
"Association Rule Mining\". It is a nature-inspired stochastic optimization "
"algorithm.\n")
print("As a demonstration of working with some of DES' parameters, let's inspect "
"a dataset containing information about 159 dog breeds.\n")
pdtable = pandas.read_csv(TABLE)

print("Fragment of the dog_breeds.csv table:")
print(pdtable)
print("\nA fragment of the table is presented above. In total, each dog breed has"
" 13 attributes.")
print("Now, let's mine some NARs. We will use a minimum support of 0.1 and a minimum "
"confidence of 0.7. Note that the minimum support and confidence only affect "
"which of the discovered NARs are returned, but the mining process itself "
"remains unchanged. We will also use a population size of 500 and "
"max_fitness_evaluations of 700. Larger values for max_fitness_evaluations "
"tend to return broader rules encompassing more attributes. The population size "
"parameter affects the number of NARs being generated and mutated. Larger values "
"are slower but output more NARs.\n")
algo.execute(minconf=0.7, minsup=0.1, population_size=500,
max_fitness_evaluations=700)
print_10_discovered_nars(algo, pdtable)
print("\nThe above NAR is the only one discovered with these settings. The NAR "
"states that about 92% of all dog breeds of type "
"'Hound' have an intelligence rating between 6 and 8 out of 10 and are between "
"sizes 0 and 4 out of 5 (0 being 'Toy' and 5 being 'Giant'). This suggests "
"that, in general, hounds are intelligent dogs and no more than "
"8% of all hounds are of 'Giant' size. Let's see if that is true.\n")




hound_rows = pdtable[pdtable['Type'] == 'Hound']

line_number_counter = 0
red_line_indices = []
for index, row in hound_rows.iterrows():
if row['Intelligence'] < 6 or row['Intelligence'] > 8:
red_line_indices.append(line_number_counter)
line_number_counter += 1

hound_row_strings = hound_rows[['Name', 'Intelligence', 'Size']].to_string().splitlines()
for i in range(0, len(hound_row_strings)):
if i-1 in red_line_indices:
print(CLR['red_bg'], hound_row_strings[i], CLR['default_bg'])
else:
print(hound_row_strings[i])

print("\nAs can be seen, only 2 rows that have 'Type' equal to 'Hound' don't "
"have an intelligence rating between 6 and 8.")
print("Let's try again, but this time with different settings. This time, minsup "
"will have a more lenient value of 0.05 and the population size will be 700. "
"This will help discover more NARs. The value of max_fitness_evaluations "
"will also need to be increased to 1500 in accordance with the population "
"size to produce a non-empty result.\n")
algo.execute(minconf=0.7, minsup=0.05, population_size=700,
max_fitness_evaluations=1500)
print_10_discovered_nars(algo, pdtable)
160 changes: 160 additions & 0 deletions examples/datasets/dog_breeds.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
Name,Origin,Type,Friendliness,Life Span,Size,Grooming Needs,Exercise Requirements,Good with Children,Intelligence,Shedding,Health Issues Risk,Weight,Training Difficulty
Affenpinscher,Germany,Toy,7,14,1,High,1.5,Yes,8,Moderate,Low,4,6
Afghan Hound,Afghanistan,Hound,5,13,3,Very High,2,No,4,High,Moderate,25,8
Airedale Terrier,England,Terrier,8,12,2,High,2,Yes,7,Moderate,Low,21,6
Akita,Japan,Working,6,11,3,Moderate,2,With Training,7,High,High,45,9
Alaskan Malamute,Alaska USA,Working,7,11,3,High,3,Yes,6,Very High,Moderate,36,8
American Bulldog,USA,Working,8,11,3,Low,2,Yes,6,Moderate,Moderate,42,7
American Eskimo,USA,Non-Sporting,9,14,2,High,2,Yes,9,High,Low,12,5
American Foxhound,USA,Hound,8,12,2,Low,2.5,Yes,6,Moderate,Low,31,6
Australian Cattle Dog,Australia,Herding,7,14,2,Moderate,2.5,Yes,9,Moderate,Low,15,7
Australian Shepherd,USA,Herding,8,14,2,High,2.5,Yes,9,High,Moderate,25,7
Australian Terrier,Australia,Terrier,7,13,1,Moderate,1.5,Yes,7,Low,Low,6.5,6
Basenji,Central Africa,Hound,6,13,2,Low,2,No,6,Low,Low,10,9
Basset Hound,France,Hound,8,11,2,Low,1,Yes,5,Moderate,High,24,7
Beagle,England,Hound,9,13,2,Low,2,Yes,7,Moderate,Moderate,10,7
Bearded Collie,Scotland,Herding,8,13,2,Very High,2,Yes,8,High,Low,22,6
Bernese Mountain Dog,Switzerland,Working,9,8,3,High,1.5,Yes,7,High,High,43,5
Bichon Frise,Mediterranean,Non-Sporting,9,13,1,Very High,1,Yes,8,Low,Low,4,5
Black Russian Terrier,Russia,Working,7,11,3,High,2,Yes,8,Moderate,Moderate,50,8
Bloodhound,Belgium,Hound,7,11,3,Moderate,2,Yes,6,Moderate,High,43,8
Border Collie,UK,Herding,8,13,2,High,3,Yes,10,Moderate,Low,17,6
Border Terrier,UK,Terrier,8,13,1,Moderate,1.5,Yes,7,Moderate,Low,6,6
Borzoi,Russia,Hound,6,11,3,High,2,No,6,Moderate,Low,37,7
Boston Terrier,USA,Non-Sporting,9,12,1,Low,1.5,Yes,8,Low,Moderate,9,5
Bouvier des Flandres,Belgium,Herding,7,11,3,Very High,2,Yes,8,High,Moderate,34,7
Boxer,Germany,Working,9,11,3,Low,2,Yes,7,Moderate,Moderate,28,6
Briard,France,Herding,7,12,3,High,2,Yes,8,Moderate,Low,35,7
Brittany,France,Sporting,8,13,2,Moderate,2.5,Yes,8,Moderate,Low,17,5
Brussels Griffon,Belgium,Toy,7,13,1,High,1.5,Yes,8,Moderate,Moderate,4,7
Bull Terrier,England,Terrier,7,12,2,Low,2,With Training,7,Low,Moderate,27,7
Bullmastiff,England,Working,6,9,3,Low,1.5,With Training,6,Moderate,High,52,7
Cairn Terrier,Scotland,Terrier,8,14,1,Moderate,1.5,Yes,7,Moderate,Low,7,6
Canaan Dog,Israel,Herding,6,13,2,Low,2,With Training,8,Moderate,Low,21,8
Cane Corso,Italy,Working,6,11,3,Low,2,With Training,7,Low,Moderate,45,9
Cardigan Welsh Corgi,Wales,Herding,8,13,1,Moderate,2,Yes,8,High,Moderate,13,6
Cavalier King Charles Spaniel,UK,Toy,10,11,1,Moderate,1,Yes,6,Moderate,High,6,4
Chesapeake Bay Retriever,USA,Sporting,7,11,3,Moderate,2.5,Yes,7,Moderate,Low,30,7
Chihuahua,Mexico,Toy,6,16,1,Low,1,No,7,Low,Moderate,2,7
Chinese Crested,China,Toy,7,14,1,High,1,Yes,8,Low,Low,3,6
Chinese Shar-Pei,China,Non-Sporting,6,10,2,Low,1.5,With Training,6,Moderate,High,22,8
Chow Chow,China,Non-Sporting,5,10,2,High,1.5,No,5,High,High,26,9
Clumber Spaniel,England,Sporting,7,11,2,Moderate,1.5,Yes,6,High,Moderate,31,6
Cocker Spaniel,England,Sporting,9,13,2,High,1.5,Yes,7,High,Moderate,12,5
Collie,Scotland,Herding,9,13,2,High,2,Yes,8,High,Moderate,24,5
Curly-Coated Retriever,England,Sporting,7,12,3,Moderate,2,Yes,7,Low,Low,32,6
Dachshund,Germany,Hound,7,14,1,Low,1.5,With Training,7,Moderate,High,10,7
Dalmatian,Croatia,Non-Sporting,8,12,2,Low,2.5,Yes,7,Moderate,High,24,7
Dandie Dinmont Terrier,Scotland,Terrier,7,13,1,Moderate,1.5,Yes,7,Low,Low,8,7
Doberman Pinscher,Germany,Working,8,11,3,Low,2.5,With Training,9,Low,Moderate,35,7
English Bulldog,England,Non-Sporting,7,9,2,Low,1,Yes,6,Moderate,High,20,7
English Cocker Spaniel,England,Sporting,8,12,2,High,1.5,Yes,7,Moderate,Moderate,14,5
English Foxhound,England,Hound,7,12,2,Low,2.5,Yes,6,Moderate,Low,29,7
English Setter,England,Sporting,8,12,2,High,2,Yes,7,Moderate,Moderate,26,6
English Springer Spaniel,England,Sporting,9,13,2,High,2,Yes,7,Moderate,Moderate,20,6
English Toy Spaniel,England,Toy,8,11,1,High,1,Yes,7,Moderate,Moderate,5,6
Entlebucher Mountain Dog,Switzerland,Herding,7,12,2,Low,2.5,Yes,8,Moderate,Low,27,7
Field Spaniel,England,Sporting,7,12,2,High,2,Yes,7,Moderate,Low,18,6
Finnish Lapphund,Finland,Herding,8,12,2,High,2,Yes,7,High,Low,20,6
Finnish Spitz,Finland,Non-Sporting,7,13,2,Moderate,2,Yes,8,Moderate,Low,15,7
Flat-Coated Retriever,England,Sporting,9,11,3,Moderate,2.5,Yes,8,Moderate,Moderate,32,5
French Bulldog,France,Non-Sporting,8,11,1,Low,1,Yes,7,Low,High,11,6
German Pinscher,Germany,Working,7,13,2,Low,2,With Training,8,Moderate,Low,13,7
German Shepherd,Germany,Herding,8,11,3,Moderate,2.5,With Training,9,High,Moderate,31,6
German Shorthaired Pointer,Germany,Sporting,8,13,3,Low,2.5,Yes,8,Moderate,Low,26,6
German Wirehaired Pointer,Germany,Sporting,7,13,3,Moderate,2.5,Yes,8,Moderate,Low,27,7
Giant Schnauzer,Germany,Working,7,13,3,High,2,With Training,8,Low,Moderate,38,8
Golden Retriever,Scotland,Sporting,10,11,3,High,2,Yes,8,High,Moderate,29,4
Gordon Setter,Scotland,Sporting,8,11,3,High,2,Yes,7,Moderate,Moderate,26,6
Great Dane,Germany,Working,8,8,4,Low,2,Yes,6,Moderate,High,67,6
Great Pyrenees,France,Working,7,11,3,High,1.5,Yes,6,High,Moderate,46,8
Greater Swiss Mountain Dog,Switzerland,Working,8,10,3,Low,2,Yes,7,Moderate,Moderate,61,7
Greyhound,Egypt,Hound,7,11,3,Low,2,Yes,7,Low,Low,33,6
Harrier,England,Hound,8,12,2,Low,2,Yes,6,Moderate,Low,25,6
Havanese,Cuba,Toy,9,14,1,High,1.5,Yes,8,Low,Low,5,5
Ibizan Hound,Spain,Hound,7,13,2,Low,2,Yes,7,Low,Low,22,7
Icelandic Sheepdog,Iceland,Herding,8,12,2,Moderate,2,Yes,7,Moderate,Low,14,6
Irish Red and White Setter,Ireland,Sporting,8,12,3,High,2.5,Yes,7,Moderate,Moderate,27,6
Irish Setter,Ireland,Sporting,8,13,3,High,2.5,Yes,7,High,Moderate,29,6
Irish Terrier,Ireland,Terrier,7,13,2,Moderate,1.5,Yes,7,Low,Low,11,7
Irish Water Spaniel,Ireland,Sporting,8,11,3,High,2,Yes,7,Low,Low,24,6
Irish Wolfhound,Ireland,Hound,7,7,4,Moderate,2,Yes,6,Moderate,High,54,6
Italian Greyhound,Italy,Toy,7,13,1,Low,1.5,No,7,Low,Moderate,4,7
Japanese Chin,Japan,Toy,7,11,1,Moderate,1,Yes,7,Moderate,Low,3,6
Japanese Spitz,Japan,Non-Sporting,8,13,1,High,1.5,Yes,8,High,Low,6,6
Keeshond,Netherlands,Non-Sporting,8,13,2,High,1.5,Yes,8,High,Low,16,6
Kerry Blue Terrier,Ireland,Terrier,7,13,2,High,1.5,Yes,7,Low,Low,16,7
Komondor,Hungary,Working,6,11,3,Very High,2,With Training,6,Low,Low,40,8
Kuvasz,Hungary,Working,6,11,3,Moderate,2,With Training,7,Moderate,Moderate,42,8
Labrador Retriever,Canada,Sporting,10,11,3,Moderate,2,Yes,8,High,Moderate,30,4
Lakeland Terrier,England,Terrier,7,13,1,Moderate,1.5,Yes,7,Low,Low,7,7
Leonberger,Germany,Working,8,9,4,High,2,Yes,7,High,High,61,8
Lhasa Apso,Tibet,Non-Sporting,6,13,1,Very High,1,No,7,Low,Low,6,8
Lowchen,France,Non-Sporting,8,14,1,High,1.5,Yes,8,Low,Low,6,6
Maltese,Malta,Toy,8,13,1,Very High,1,No,7,Low,Low,2.5,6
Manchester Terrier,England,Terrier,7,15,2,Low,1.5,With Training,8,Moderate,Low,7,7
Mastiff,England,Working,6,9,4,Low,1.5,Yes,6,Moderate,High,78,7
Miniature Pinscher,Germany,Toy,6,13,1,Low,1.5,No,7,Low,Low,4,8
Miniature Schnauzer,Germany,Standard,8,12,1,High,1.5,Yes,8,Moderate,Low,8,6
Neapolitan Mastiff,Italy,Working,6,8,3,Moderate,1,Yes,5,Moderate,High,70,8
Newfoundland,Canada,Working,9,9,4,High,2,Yes,7,Moderate,Moderate,61,7
Norfolk Terrier,England,Terrier,8,14,1,Low,1.5,Yes,7,Low,Low,5,6
Norwegian Buhund,Norway,Herding,8,12,2,Moderate,2,Yes,7,Moderate,Low,12,6
Norwegian Elkhound,Norway,Hound,7,12,2,Moderate,2,Yes,7,Moderate,Moderate,23,7
Norwegian Lundehund,Norway,Non-Sporting,7,12,1,High,2,Yes,7,Moderate,High,8,7
Old English Sheepdog,England,Herding,8,10,3,Very High,2,Yes,6,High,Moderate,32,6
Otterhound,England,Hound,7,10,3,Very High,2.5,Yes,6,High,Moderate,37,7
Papillon,France,Toy,9,13,1,Moderate,1,Yes,8,Low,Low,3,5
Pekingese,China,Toy,6,12,1,Very High,1,With Training,5,Moderate,High,5,7
Pembroke Welsh Corgi,Wales,Herding,9,12,1,Moderate,2,Yes,8,Moderate,Moderate,12,5
Petit Basset Griffon Vendeen,France,Hound,7,12,1,High,2,Yes,6,High,Moderate,15,7
Pharaoh Hound,Malta,Hound,7,12,2,Moderate,2,Yes,8,Moderate,Low,20,7
Pointer,England,Sporting,8,12,2,Moderate,2.5,Yes,7,Moderate,Moderate,25,6
Pomeranian,Germany,Toy,8,12,1,High,1,Yes,7,High,Moderate,3,7
Poodle (Miniature),France,Non-Sporting,8,15,1,Very High,1.5,Yes,8,Low,Low,7,6
Poodle (Standard),France,Non-Sporting,9,12,3,Very High,2,Yes,8,Moderate,Moderate,22,5
Poodle (Toy),France,Toy,9,15,0,Very High,1,Yes,8,Low,Low,4,5
Portuguese Podengo,Portugal,Hound,7,12,2,Moderate,2,Yes,8,Moderate,Low,15,8
Portuguese Water Dog,Portugal,Sporting,8,11,2,High,2,Yes,8,Moderate,Moderate,18,6
Pudelpointer,Germany,Sporting,7,12,2,Moderate,2.5,Yes,8,Moderate,Low,23,7
Pug,China,Toy,8,12,1,Moderate,1,Yes,7,Moderate,High,7,6
Pyrenean Mountain Dog,France,Working,7,10,4,High,2,Yes,6,High,High,54,7
Rat Terrier,USA,Terrier,7,15,2,Low,2,Yes,7,Moderate,Moderate,5,7
Redbone Coonhound,USA,Hound,8,12,2,Moderate,2,Yes,6,Moderate,Low,25,7
Rhodesian Ridgeback,Africa,Hound,6,11,3,Moderate,2.5,With Training,8,Moderate,Moderate,36,7
Rottweiler,Germany,Working,7,9,3,Moderate,2,With Training,8,High,High,40,8
Saint Bernard,Switzerland,Working,9,8,4,High,2,Yes,6,High,Moderate,68,7
Saluki,Middle East,Hound,6,12,2,Moderate,2.5,No,7,Moderate,Moderate,22,7
Samoyed,Siberia,Working,9,12,2,High,2,Yes,7,High,Moderate,21,7
Schipperke,Belgium,Non-Sporting,7,13,1,Moderate,2,No,7,Moderate,Moderate,7,7
Scottish Deerhound,Scotland,Hound,6,8,3,Moderate,2,Yes,6,Moderate,Moderate,41,7
Scottish Terrier,Scotland,Terrier,7,12,1,Moderate,1.5,Yes,6,Moderate,High,9,7
Shetland Sheepdog,Scotland,Herding,9,12,2,High,2,Yes,8,High,Moderate,11,5
Shiba Inu,Japan,Non-Sporting,7,13,1,Moderate,2,No,7,Moderate,Moderate,9,7
Shih Tzu,China,Toy,8,14,1,Very High,1,Yes,6,Moderate,Moderate,4,7
Siberian Husky,Siberia,Working,8,12,2,Moderate,2.5,Yes,7,Moderate,Moderate,21,7
Silky Terrier,Australia,Terrier,7,13,1,High,1.5,Yes,7,Moderate,Low,4,7
Skye Terrier,Scotland,Terrier,7,13,1,Moderate,1.5,No,6,Moderate,Moderate,8,7
Sloughi,North Africa,Hound,6,12,2,Low,2.5,With Training,7,Low,Moderate,18,7
Smooth Fox Terrier,England,Terrier,7,13,1,Low,2,Yes,7,Moderate,Moderate,7,7
Soft-Coated Wheaten Terrier,Ireland,Terrier,8,12,2,High,2,Yes,7,Moderate,Moderate,12,6
Spanish Water Dog,Spain,Sporting,7,12,2,Very High,2,Yes,8,Moderate,Moderate,15,6
Spinone Italiano,Italy,Sporting,7,12,3,High,2,Yes,7,Moderate,Moderate,32,7
Staffordshire Bull Terrier,England,Terrier,7,12,2,Low,2,Yes,7,Moderate,High,16,7
Standard Poodle,France,Non-Sporting,9,12,3,Very High,2,Yes,8,Moderate,Moderate,26,5
Standard Schnauzer,Germany,Standard,7,15,2,High,2,Yes,7,Moderate,Moderate,14,6
Sussex Spaniel,England,Sporting,7,11,2,High,1.5,Yes,6,Moderate,Moderate,18,6
Swedish Vallhund,Sweden,Herding,8,12,2,Moderate,2,Yes,8,Moderate,Low,12,7
Thai Ridgeback,Thailand,Hound,6,12,2,Moderate,2,With Training,7,Moderate,Moderate,15,7
Tibetan Mastiff,Tibet,Working,7,10,3,High,2,With Training,6,Moderate,Moderate,45,8
Toy Fox Terrier,USA,Toy,8,15,0,Moderate,1.5,Yes,8,Low,Low,2,7
Treeing Walker Coonhound,USA,Hound,8,12,2,Moderate,2,Yes,7,Moderate,Moderate,20,7
Welsh Springer Spaniel,Wales,Sporting,8,13,2,High,2,Yes,7,Moderate,Moderate,16,6
Welsh Terrier,Wales,Terrier,7,14,2,Moderate,1.5,Yes,7,Moderate,Moderate,9,7
West Highland White Terrier,Scotland,Terrier,8,13,1,Moderate,1.5,Yes,7,Moderate,Moderate,7,7
Whippet,England,Hound,7,13,2,Low,2,Yes,8,Low,Low,8,6
Wire Fox Terrier,England,Terrier,7,14,1,Moderate,2,Yes,7,Moderate,Moderate,8,7
Wirehaired Dachshund,Germany,Hound,7,13,1,Moderate,1.5,With Training,7,Moderate,High,8,7
Wirehaired Pointing Griffon,Netherlands,Sporting,7,13,2,High,2,Yes,7,Moderate,Moderate,20,6
Xoloitzcuintli,Mexico,Non-Sporting,7,15,3,Low,2,With Training,8,Low,Moderate,25,6
Yorkshire Terrier,England,Toy,8,13,0,High,1,Yes,7,Moderate,Moderate,2.5,6

0 comments on commit f508c0e

Please sign in to comment.