def main(args): """ Trains Apriori on the specified dataset (uses vote UCI dataset if no dataset specified). :param args: the commandline arguments :type args: list """ # load a dataset if len(args) <= 1: data_file = helper.get_data_dir() + os.sep + "vote.arff" else: data_file = args[1] helper.print_info("Loading dataset: " + data_file) loader = Loader("weka.core.converters.ArffLoader") data = loader.load_file(data_file) data.class_is_last() # build Apriori, using last attribute as class attribute apriori = Associator(classname="weka.associations.Apriori", options=["-c", "-1"]) apriori.build_associations(data) print(str(apriori)) # iterate association rules (low-level) helper.print_info("Rules (low-level)") # make the underlying rules list object iterable in Python rules = javabridge.iterate_collection(apriori.jwrapper.getAssociationRules().getRules().o) for i, r in enumerate(rules): # wrap the Java object to make its methods accessible rule = JWrapper(r) print(str(i+1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.getConsequenceSupport())) print(" - premise support: " + str(rule.getPremiseSupport())) print(" - total support: " + str(rule.getTotalSupport())) print(" - total transactions: " + str(rule.getTotalTransactions())) # iterate association rules (high-level) helper.print_info("Rules (high-level)") print("can produce rules? " + str(apriori.can_produce_rules())) print("rule metric names: " + str(apriori.rule_metric_names)) rules = apriori.association_rules() if rules is not None: print("producer: " + rules.producer) print("# rules: " + str(len(rules))) for i, rule in enumerate(rules): print(str(i+1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.consequence_support)) print(" - consequence: " + str(rule.consequence)) print(" - premise support: " + str(rule.premise_support)) print(" - premise: " + str(rule.premise)) print(" - total support: " + str(rule.total_support)) print(" - total transactions: " + str(rule.total_transactions)) print(" - metric names: " + str(rule.metric_names)) print(" - metric values: " + str(rule.metric_values)) print(" - metric value 'Confidence': " + str(rule.metric_value('Confidence'))) print(" - primary metric name: " + str(rule.primary_metric_name)) print(" - primary metric value: " + str(rule.primary_metric_value))
def main(): """ Just runs some example code. """ # load a dataset vote_file = helper.get_data_dir() + os.sep + "vote.arff" helper.print_info("Loading dataset: " + vote_file) loader = Loader("weka.core.converters.ArffLoader") vote_data = loader.load_file(vote_file) vote_data.class_is_last() # train and output associator associator = Associator(classname="weka.associations.Apriori", options=["-N", "9", "-I"]) associator.build_associations(vote_data) print(associator)
def weka_algorithm(algorithm, type, minsup, minconf): # Weka worker start jvm.start() loader = Loader(classname="weka.core.converters.ArffLoader") data = loader.load_file(arr_file) associator = Associator(classname=f"weka.associations.{algorithm}", options=["-M", minsup, "-C", minconf, "-N", "100"]) associator.build_associations(data) with open(f"results/{type}_weka_{algorithm}.output", "w") as fw: fw.write(associator.__str__()) fw.close() # Weka worker end jvm.stop()
def associateRule(request): jvm.start() data_dir = os.path.dirname(os.path.abspath(__file__)) data = converters.load_any_file(data_dir + "/templates/upload_files/export.csv") data.class_is_last() associator = Associator(classname="weka.associations.Apriori", options=["-C", "-1", "-I"]) # associator = Associator(classname="weka.associations.Apriori", options=["-N", "9", "-I"]) associator.build_associations(data) rules = str(associator) jvm.stop() return HttpResponse(rules)
def __build_associations(algorithm_name, data, result_dest=None): """ Function for building association algorithms. Based on name we select proper algorithm. :param algorithm_name: string :param data: weka arff data :param result_dest: results destination :return: None """ args, _sufix = parsers_dict[algorithm_name]() associator = Associator(classname=algorithms_path_dict[algorithm_name], options=args_to_weka_options(args, _sufix)) associator.build_associations(data) if result_dest: with open(result_dest, 'a') as file: file.write( __print_algorithm_header(associator.to_commandline(), __get_header_of_data(data), algorithm_name)) file.write(str(associator)) else: print( __print_algorithm_header(associator.to_commandline(), __get_header_of_data(data), algorithm_name)) print(str(associator))
def main(args): """ Trains Apriori on the specified dataset (uses vote UCI dataset if no dataset specified). :param args: the commandline arguments :type args: list """ # load a dataset if len(args) <= 1: data_file = helper.get_data_dir() + os.sep + "vote.arff" else: data_file = args[1] helper.print_info("Loading dataset: " + data_file) loader = Loader("weka.core.converters.ArffLoader") data = loader.load_file(data_file) data.class_is_last() # build Apriori, using last attribute as class attribute apriori = Associator(classname="weka.associations.Apriori", options=["-c", "-1"]) apriori.build_associations(data) print(str(apriori))
# More Data Mining with Weka - Class 3.3 # Copyright (C) 2014 Fracpete (fracpete at gmail dot com) # Use the WEKAMOOC_DATA environment variable to set the location # for the datasets import os data_dir = os.environ.get("WEKAMOOC_DATA") if data_dir is None: data_dir = "." + os.sep + "data" import os import weka.core.jvm as jvm from weka.core.converters import Loader from weka.associations import Associator jvm.start() # load weather.nominal fname = data_dir + os.sep + "weather.nominal.arff" print("\nLoading dataset: " + fname + "\n") loader = Loader(classname="weka.core.converters.ArffLoader") data = loader.load_file(fname) data.set_class_index(data.num_attributes() - 1) # build Apriori assoc = Associator(classname="weka.associations.Apriori") assoc.build_associations(data) print(assoc) jvm.stop()
def main(args): """ Trains Apriori on the specified dataset (uses vote UCI dataset if no dataset specified). :param args: the commandline arguments :type args: list """ # load a dataset if len(args) <= 1: data_file = helper.get_data_dir() + os.sep + "vote.arff" else: data_file = args[1] helper.print_info("Loading dataset: " + data_file) loader = Loader("weka.core.converters.ArffLoader") data = loader.load_file(data_file) data.class_is_last() # build Apriori, using last attribute as class attribute apriori = Associator(classname="weka.associations.Apriori", options=["-c", "-1"]) apriori.build_associations(data) print(str(apriori)) # iterate association rules (low-level) helper.print_info("Rules (low-level)") # make the underlying rules list object iterable in Python rules = javabridge.iterate_collection( apriori.jwrapper.getAssociationRules().getRules().o) for i, r in enumerate(rules): # wrap the Java object to make its methods accessible rule = JWrapper(r) print(str(i + 1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.getConsequenceSupport())) print(" - premise support: " + str(rule.getPremiseSupport())) print(" - total support: " + str(rule.getTotalSupport())) print(" - total transactions: " + str(rule.getTotalTransactions())) # iterate association rules (high-level) helper.print_info("Rules (high-level)") print("can produce rules? " + str(apriori.can_produce_rules())) print("rule metric names: " + str(apriori.rule_metric_names)) rules = apriori.association_rules() if rules is not None: print("producer: " + rules.producer) print("# rules: " + str(len(rules))) for i, rule in enumerate(rules): print(str(i + 1) + ". " + str(rule)) # output some details on rule print(" - consequence support: " + str(rule.consequence_support)) print(" - consequence: " + str(rule.consequence)) print(" - premise support: " + str(rule.premise_support)) print(" - premise: " + str(rule.premise)) print(" - total support: " + str(rule.total_support)) print(" - total transactions: " + str(rule.total_transactions)) print(" - metric names: " + str(rule.metric_names)) print(" - metric values: " + str(rule.metric_values)) print(" - metric value 'Confidence': " + str(rule.metric_value('Confidence'))) print(" - primary metric name: " + str(rule.primary_metric_name)) print(" - primary metric value: " + str(rule.primary_metric_value))
# More Data Mining with Weka - Class 3.3 # Copyright (C) 2014 Fracpete (fracpete at gmail dot com) # Use the WEKAMOOC_DATA environment variable to set the location # for the datasets import os data_dir = os.environ.get("WEKAMOOC_DATA") if data_dir is None: data_dir = "." + os.sep + "data" import weka.core.jvm as jvm from weka.core.converters import Loader from weka.associations import Associator jvm.start() # load weather.nominal fname = data_dir + os.sep + "weather.nominal.arff" print("\nLoading dataset: " + fname + "\n") loader = Loader(classname="weka.core.converters.ArffLoader") data = loader.load_file(fname) data.class_is_last() # build Apriori assoc = Associator(classname="weka.associations.Apriori") assoc.build_associations(data) print(assoc) jvm.stop()
import weka.core.jvm as jvm from weka.core.converters import Loader from weka.associations import Associator jvm.start() # load weather.nominal fname = data_dir + os.sep + "weather.nominal.arff" print("\nLoading dataset: " + fname + "\n") loader = Loader(classname="weka.core.converters.ArffLoader") data = loader.load_file(fname) data.class_is_last() # build Apriori (output as many rules as possible that min support of 0.01) print("\n--> Output many weather rules\n") assoc = Associator(classname="weka.associations.Apriori", options=["-M", "0.01", "-N", "10000"]) assoc.build_associations(data) print(assoc) # build Apriori (default options, outputting itemsets) print("\n--> Output weather itemsets\n") assoc = Associator(classname="weka.associations.Apriori", options=["-I"]) assoc.build_associations(data) print(assoc) # load supermarket fname = data_dir + os.sep + "supermarket.arff" print("\nLoading dataset: " + fname + "\n") loader = Loader(classname="weka.core.converters.ArffLoader") data = loader.load_file(fname) data.class_is_last()