diff --git a/examples/basic/mining_nar.py b/examples/basic/mining_nar.py new file mode 100644 index 0000000000..0f7f5acf57 --- /dev/null +++ b/examples/basic/mining_nar.py @@ -0,0 +1,82 @@ +import desbordante +import pandas +from tabulate import tabulate + +TABLE = 'examples/datasets/dog_breeds.csv' +COLOR_CODES = { + 'bold_red': '\u001b[1;31m', + 'bold_green': '\033[1;32m', + 'bold_yellow': '\033[1;33m', + 'bold_blue': '\033[1;34m', + 'green': '\033[32m', + 'yellow': '\033[33m', + 'default': '\033[0m', + 'blue_bg': '\033[1;46m', + 'default_bg': '\033[1;49m' +} + + +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) + ":") + 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() + + +if __name__ == '__main__': + algo = desbordante.nar.algorithms.des() + algo.load_data(table=(TABLE, ',', True)) + print("As a demonstration, let's look at a dataset containing information about 159 dog breeds.\n") + print("+-------------------------------+----------------+--------------+----------------+-------------+---") + print("| Name | Origin | Type | Friendliness | Life Span | . . .") + print("+-------------------------------+----------------+--------------+----------------+-------------+---") + print("| Affenpinscher | Germany | Toy | 7 | 14 | . . .") + print("| Afghan Hound | Afghanistan | Hound | 5 | 13 |") + print("| Airedale Terrier | England | Terrier | 8 | 12 |") + print("| Akita | Japan | Working | 6 | 11 |") + print("| Alaskan Malamute | Alaska USA | Working | 7 | 11 |") + print("| American Bulldog | USA | Working | 8 | 11 |") + print("| American Eskimo | USA | Non-Sporting | 9 | 14 |") + print("| American Foxhound | USA | Hound | 8 | 12 |") + print("| Australian Cattle Dog | Australia | Herding | 7 | 14 |") + print(" . . . . . . . . . . . . . . .\n") + print("A fragment of the table is presented above. In total each dog breed has" + " 13 attributes. Some attributes are floating points, some are integers" + " and some are strings.") + print("Now, let's mine some NARs with minimum support of 0.1 and minimum " + "confidence of 0.7 and see what we discover.\n") + algo.execute(minconf=0.7, minsup=0.1, population_size=500, + max_fitness_evaluations=700) + pdtable = pandas.read_csv(TABLE) + print_10_discovered_nars(algo, pdtable) + print("The above NAR is the only one that gets discovered when using such " + "settings. The NAR states that about 92% of all dog breeds of type " + "\'Hound\' have an intellignce 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.\n") + print("Let's try again but this time with different settings to discover" + " more NARs.\n") + algo.execute(minconf=0.7, minsup=0.05, population_size=700, + max_fitness_evaluations=1500) + print_10_discovered_nars(algo, pdtable) diff --git a/examples/datasets/dog_breeds.csv b/examples/datasets/dog_breeds.csv new file mode 100644 index 0000000000..05f271d147 --- /dev/null +++ b/examples/datasets/dog_breeds.csv @@ -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