summaryrefslogtreecommitdiff
path: root/abml
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@fri.uni-lj.si>2017-04-18 17:25:18 +0200
committerTimotej Lazar <timotej.lazar@fri.uni-lj.si>2017-04-18 17:25:18 +0200
commitcc41068e6b4566633ebc7a841290ce167671d2bf (patch)
treed94f6ff363a36917f1650abe46d174f4dde2dfb5 /abml
parentfa38d4ca99f863573b8900ef78d5191b8f2db561 (diff)
Store student submissions in files instead of .pickle
Diffstat (limited to 'abml')
-rw-r--r--abml/evaluate.py22
-rw-r--r--abml/learn_dist.py11
-rw-r--r--abml/rules_prolog.py14
3 files changed, 24 insertions, 23 deletions
diff --git a/abml/evaluate.py b/abml/evaluate.py
index 2e318fb..cb959ed 100644
--- a/abml/evaluate.py
+++ b/abml/evaluate.py
@@ -1,4 +1,4 @@
-import pickle
+import os.path
import argparse
import Orange
from Orange.evaluation import TestOnTestData, CA, AUC, LogLoss
@@ -8,30 +8,28 @@ import orangecontrib.evcrules.logistic as logistic
import orangecontrib.abml.abrules as rules
import orangecontrib.abml.argumentation as arg
-parser = argparse.ArgumentParser(description='Learn and test rules for prolog programs.')
-parser.add_argument('Name', type=str, help='Predicate name.')
+parser = argparse.ArgumentParser(description='Learn and evaluate rules for Prolog programs.')
+parser.add_argument('path', help='path to data directory')
args = parser.parse_args()
-name = args.Name
+path = args.path
# load data
-data = Orange.data.Table('data/{}/programs-train'.format(name))
+data = Orange.data.Table(os.path.join(path, 'programs-train'))
# create learner
-rule_learner = rp.Rules4Prolog(name, 0.9)
-
-
+rule_learner = rp.Rules4Prolog(path, 0.9)
# learn a classifier
classifier = rule_learner(data)
# save model
-fmodel = open("data/{}/model.txt".format(name), "wt")
+fmodel = open(os.path.join(path, 'model.txt'), "wt")
for r in classifier.rule_list:
print(r, r.curr_class_dist, r.quality)
fmodel.write("{} dist={} quality={}\n".format(str(r), str(r.curr_class_dist), r.quality))
# accuracy of model
-testdata = Orange.data.Table('data/{}/programs-test'.format(name))
+testdata = Orange.data.Table(os.path.join(path, 'programs-test'))
predictions = classifier(testdata)
acc = 0
for i, p in enumerate(predictions):
@@ -64,12 +62,12 @@ scores += "CA\tAUC\tLogLoss\tMethod\n"
for ni, n in enumerate(names):
scores += "{}\t{}\t{}\t{}\n".format(ca[ni], auc[ni], ll[ni], n)
print(scores)
-fscores = open("data/{}/scores.txt".format(name), "wt")
+fscores = open(os.path.join(path, 'scores.txt'), 'wt')
fscores.write(scores)
all_rules = classifier.rule_list
all_rules.sort(key = lambda r: r.quality, reverse=True)
-rfile = open("data/{}/rules.txt".format(name), "wt")
+rfile = open(os.path.join(path, 'rules.txt'), 'wt')
for r in all_rules:
print(r, r.curr_class_dist, r.quality)
rfile.write("{} {} {}\n".format(r, r.curr_class_dist, r.quality))
diff --git a/abml/learn_dist.py b/abml/learn_dist.py
index 58e4968..44e19c3 100644
--- a/abml/learn_dist.py
+++ b/abml/learn_dist.py
@@ -1,15 +1,16 @@
+import os.path
import pickle
import argparse
from Orange.data import Table
import abml.rules_prolog as rp
parser = argparse.ArgumentParser(description='Learn and test rules for prolog programs.')
-parser.add_argument('Name', type=str, help='Predicate name.')
+parser.add_argument('path', help='path to data directory')
args = parser.parse_args()
-name = args.Name
+path = args.path
-data = Table('data/{}/programs-train'.format(name))
+data = Table(os.path.join(path, 'programs-train'))
-rule_learner = rp.create_learner(name, evds=False)
+rule_learner = rp.create_learner(path, evds=False)
rule_learner.calculate_evds(data)
-pickle.dump(rule_learner.evds, open("data/{}/evds.pickle".format(name), "wb"))
+pickle.dump(rule_learner.evds, open(os.path.join(path, 'evds.pickle'), "wb"))
diff --git a/abml/rules_prolog.py b/abml/rules_prolog.py
index 9edd674..f67ba93 100644
--- a/abml/rules_prolog.py
+++ b/abml/rules_prolog.py
@@ -1,6 +1,8 @@
-import numpy as np
-import pickle
import itertools
+import os.path
+import pickle
+
+import numpy as np
from Orange.classification.rules import _RuleClassifier, GuardianValidator
import orangecontrib.abml.abrules as rules
from Orange.classification.rules import Rule
@@ -92,14 +94,14 @@ class NegativeFirstClassifier(_RuleClassifier):
return probabilities
class Rules4Prolog:
- def __init__(self, name, threshold):
+ def __init__(self, path, threshold):
self.threshold = threshold
self.learner = rules.ABRuleLearner(width=50, parent_alpha=0.05)
self.learner.rule_finder.general_validator = TrueCondValidator(self.learner.rule_finder.general_validator.max_rule_length,
self.learner.rule_finder.general_validator.min_covered_examples)
self.learner.rule_validator = PureAccuracyValidator(0, self.threshold)
self.learner.classifier = NegativeFirstClassifier
- self.learner.evds = pickle.load(open("data/{}/evds.pickle".format(name), "rb"))
+ self.learner.evds = pickle.load(open(os.path.join(path, 'evds.pickle'), 'rb'))
def __call__(self, data):
# first learn rules for negative class (quality should be higher than
@@ -161,13 +163,13 @@ class Rules4Prolog:
-def create_learner(name, evds=True):
+def create_learner(path, evds=True):
rule_learner = rules.ABRuleLearner(width=50, parent_alpha=0.05)
rule_learner.rule_finder.general_validator = TrueCondValidator(rule_learner.rule_finder.general_validator.max_rule_length,
rule_learner.rule_finder.general_validator.min_covered_examples)
rule_learner.rule_validator = PureAccuracyValidator(0, 0.8)
rule_learner.classifier = NegativeFirstClassifier
if evds:
- rule_learner.evds = pickle.load(open("data/{}/evds.pickle".format(name), "rb"))
+ rule_learner.evds = pickle.load(open(os.path.join(path, 'evds.pickle'), 'rb'))
return rule_learner