本文整理汇总了Python中weka.classifiers.Classifier.make_copy方法的典型用法代码示例。如果您正苦于以下问题:Python Classifier.make_copy方法的具体用法?Python Classifier.make_copy怎么用?Python Classifier.make_copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.classifiers.Classifier
的用法示例。
在下文中一共展示了Classifier.make_copy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_learning_curve
# 需要导入模块: from weka.classifiers import Classifier [as 别名]
# 或者: from weka.classifiers.Classifier import make_copy [as 别名]
def plot_learning_curve(classifiers, train, test=None, increments=100, metric="percent_correct",
title="Learning curve", label_template="[#] @ $", key_loc="lower right",
outfile=None, wait=True):
"""
Plots
:param classifiers: list of Classifier template objects
:type classifiers: list of Classifier
:param train: dataset to use for the building the classifier, used for evaluating it test set None
:type train: Instances
:param test: optional dataset to use for the testing the built classifiers
:type test: Instances
:param increments: the increments (>= 1: # of instances, <1: percentage of dataset)
:type increments: float
:param metric: the name of the numeric metric to plot (Evaluation.<metric>)
:type metric: str
:param title: the title for the plot
:type title: str
:param label_template: the template for the label in the plot
(#: 1-based index, @: full classname, !: simple classname, $: options)
:type label_template: str
:param key_loc: the location string for the key
:type key_loc: str
:param outfile: the output file, ignored if None
:type outfile: str
:param wait: whether to wait for the user to close the plot
:type wait: bool
"""
if not plot.matplotlib_available:
logger.error("Matplotlib is not installed, plotting unavailable!")
return
if not train.has_class():
logger.error("Training set has no class attribute set!")
return
if (test is not None) and (train.equal_headers(test) is not None):
logger.error("Training and test set are not compatible: " + train.equal_headers(test))
return
if increments >= 1:
inc = increments
else:
inc = round(train.num_instances * increments)
steps = []
cls = []
evls = {}
for classifier in classifiers:
cl = Classifier.make_copy(classifier)
cls.append(cl)
evls[cl] = []
if test is None:
tst = train
else:
tst = test
for i in xrange(train.num_instances):
if (i > 0) and (i % inc == 0):
steps.append(i+1)
for cl in cls:
# train
if cl.is_updateable:
if i == 0:
tr = Instances.copy_instances(train, 0, 1)
cl.build_classifier(tr)
else:
cl.update_classifier(train.get_instance(i))
else:
if (i > 0) and (i % inc == 0):
tr = Instances.copy_instances(train, 0, i + 1)
cl.build_classifier(tr)
# evaluate
if (i > 0) and (i % inc == 0):
evl = Evaluation(tst)
evl.test_model(cl, tst)
evls[cl].append(getattr(evl, metric))
fig, ax = plt.subplots()
ax.set_xlabel("# of instances")
ax.set_ylabel(metric)
ax.set_title(title)
fig.canvas.set_window_title(title)
ax.grid(True)
i = 0
for cl in cls:
evl = evls[cl]
i += 1
plot_label = label_template.\
replace("#", str(i)).\
replace("@", cl.classname).\
replace("!", cl.classname[cl.classname.rfind(".") + 1:]).\
replace("$", join_options(cl.config))
ax.plot(steps, evl, label=plot_label)
plt.draw()
plt.legend(loc=key_loc, shadow=True)
if outfile is not None:
plt.savefig(outfile)
if wait:
plt.show()
示例2: main
# 需要导入模块: from weka.classifiers import Classifier [as 别名]
# 或者: from weka.classifiers.Classifier import make_copy [as 别名]
def main():
"""
Just runs some example code.
"""
# load a dataset
data_file = helper.get_data_dir() + os.sep + "vote.arff"
helper.print_info("Loading dataset: " + data_file)
loader = Loader("weka.core.converters.ArffLoader")
data = loader.load_file(data_file)
data.class_is_last()
# classifier
classifier = Classifier(classname="weka.classifiers.trees.J48")
# randomize data
folds = 10
seed = 1
rnd = Random(seed)
rand_data = Instances.copy_instances(data)
rand_data.randomize(rnd)
if rand_data.class_attribute.is_nominal:
rand_data.stratify(folds)
# perform cross-validation and add predictions
predicted_data = None
evaluation = Evaluation(rand_data)
for i in xrange(folds):
train = rand_data.train_cv(folds, i)
# the above code is used by the StratifiedRemoveFolds filter,
# the following code is used by the Explorer/Experimenter
# train = rand_data.train_cv(folds, i, rnd)
test = rand_data.test_cv(folds, i)
# build and evaluate classifier
cls = Classifier.make_copy(classifier)
cls.build_classifier(train)
evaluation.test_model(cls, test)
# add predictions
addcls = Filter(
classname="weka.filters.supervised.attribute.AddClassification",
options=["-classification", "-distribution", "-error"])
# setting the java object directory avoids issues with correct quoting in option array
addcls.set_property("classifier", Classifier.make_copy(classifier))
addcls.inputformat(train)
addcls.filter(train) # trains the classifier
pred = addcls.filter(test)
if predicted_data is None:
predicted_data = Instances.template_instances(pred, 0)
for n in xrange(pred.num_instances):
predicted_data.add_instance(pred.get_instance(n))
print("")
print("=== Setup ===")
print("Classifier: " + classifier.to_commandline())
print("Dataset: " + data.relationname)
print("Folds: " + str(folds))
print("Seed: " + str(seed))
print("")
print(evaluation.summary("=== " + str(folds) + " -fold Cross-Validation ==="))
print("")
print(predicted_data)