在Orange中创建规则对象

时间:2018-06-07 18:20:28

标签: python python-3.x orange

我正在尝试在Orange 3中创建一个Rule对象,以便我可以将它附加到列表并将其传递给CN2Learner。我不知道如何做到这一点,因为我收到错误说

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 / Users / prateekjain / work / ruleidentification / com / learning / personalized / rules / orange_rule_learner。 PY Traceback(最近一次调用最后一次): 完成加载数据   文件“/Users/prateekjain/work/ruleidentification/com/learning/personalized/rules/orange_rule_learner.py”,第51行,in     cn2_classifier = cn2_learner(数据集)   在调用中输入文件“/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Orange/base.py”,第112行     data = self.preprocess(data)   文件“/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Orange/base.py”,第138行,预处理     data = pp(数据) TypeError:'Rule'对象不可调用

使用退出代码1完成处理

我希望有人可以指出我如何将规则纳入CN2分类器。

我的代码如下所示:

import Orange
from collections import namedtuple
from Orange.classification.rules import Selector, Rule


dataset = Orange.data.Table.from_file("~/testing-script1-orange.csv")
print("Finished loading data")

columns_obj = dataset.domain.attributes

attributes = []
discrete_variable_list = []


for attribute in columns_obj:
    attributes.append(attribute.name)
    discrete_variable_list.append(attribute)

#Lets try to create a Rule
index_gender = attributes.index("gender")
var = discrete_variable_list[index_gender]
var_val = var.to_val("female")

s1 = Selector(column=index_gender, op="==", value=var_val)

list_rule = []
list_rule.append(s1)

r = Rule(selectors=list_rule)
r.target_class="present"

base_rules_list = []
base_rules_list.append(r)
# construct the learning algorithm and use it to induce a classifier
cn2_learner = Orange.classification.rules.CN2SDUnorderedLearner(base_rules_list)
cn2_learner.rule_finder.general_validator.min_covered_examples = 10
cn2_learner.rule_finder.search_algorithm.beam_width = 10
cn2_learner.rule_finder.general_validator.max_rule_length = 2

cn2_classifier = cn2_learner(dataset)

# All rule-base classifiers can have their rules printed out like this:
class_list = dataset.domain.class_var.values
class_scores = namedtuple('Row',class_list)


for r in cn2_classifier.rule_list:
    print(r,r.quality,r.curr_class_dist.tolist(),class_scores(*r.probabilities))

0 个答案:

没有答案