📄 lib_learner.cpp
字号:
PyObject *AssociationRules_count(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "(AssociationRule) -> int") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_count(self, obj); }
PyObject *AssociationRules_filter(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "([filter-function]) -> AssociationRules") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_filter(self, args); }
PyObject *AssociationRules_index(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "(AssociationRule) -> int") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_index(self, obj); }
PyObject *AssociationRules_insert(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "(index, item) -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_insert(self, args); }
PyObject *AssociationRules_native(TPyOrange *self) PYARGS(METH_NOARGS, "() -> list") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_native(self); }
PyObject *AssociationRules_pop(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "() -> AssociationRule") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_pop(self, args); }
PyObject *AssociationRules_remove(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "(AssociationRule) -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_remove(self, obj); }
PyObject *AssociationRules_reverse(TPyOrange *self) PYARGS(METH_NOARGS, "() -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_reverse(self); }
PyObject *AssociationRules_sort(TPyOrange *self, PyObject *args) PYARGS(METH_VARARGS, "([cmp-func]) -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_sort(self, args); }
PyObject *AssociationRules__reduce__(TPyOrange *self, PyObject *) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_reduce(self); }
/************* CLASSIFICATION TREES ************/
#include "tdidt.hpp"
#include "tdidt_split.hpp"
#include "tdidt_stop.hpp"
#include "callback.hpp"
C_CALL(TreeLearner, Learner, "([examples] [, weight=, split=, stop=, nodeLearner=, lookDownOnUnknown=]) -/-> Classifier")
C_NAMED(TreeNode, Orange, "([lookDownOnUnknown=, branchSelector=, nodeClassifier=, branches=, contingency=])")
C_NAMED(TreeClassifier, ClassifierFD, "([domain=, tree=, descender=])")
C_NAMED(TreeStopCriteria_common, TreeStopCriteria, "([maxMajority=, minExamples=])")
HIDDEN(TreeStopCriteria_Python, TreeStopCriteria)
NO_PICKLE(TreeStopCriteria_Python)
C_CALL(TreeSplitConstructor_Combined, TreeSplitConstructor, "([examples, [weight, domainContingency, apriorClass, candidates] [discreteTreeSplitConstructor=, continuousTreeSplitConstructor=]) -/-> (Classifier, descriptions, sizes, quality)")
ABSTRACT(TreeSplitConstructor_Measure, TreeSplitConstructor)
C_CALL(TreeSplitConstructor_Attribute, TreeSplitConstructor_Measure, "([measure=, worstAcceptable=, minSubset=])")
C_CALL(TreeSplitConstructor_ExhaustiveBinary, TreeSplitConstructor_Measure, "([measure=, worstAcceptable=, minSubset=])")
C_CALL(TreeSplitConstructor_Threshold, TreeSplitConstructor_Measure, "([measure=, worstAcceptable=, minSubset=])")
PYXTRACT_IGNORE C_CALL(TreeSplitConstructor_LR, TreeSplitConstructor, "([minSubset=])")
BASED_ON(TreeExampleSplitter, Orange)
C_CALL(TreeExampleSplitter_IgnoreUnknowns, TreeExampleSplitter, "([node, examples[, weight]]) -/-> (ExampleGeneratorList, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsToCommon, TreeExampleSplitter, "([node, examples[, weight]]) -/-> (ExampleGeneratorList, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsToAll, TreeExampleSplitter, "([node, examples[, weight]]) -/-> (ExampleGeneratorList, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsToRandom, TreeExampleSplitter, "([node, examples[, weight]]) -/-> (ExampleGeneratorList, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsToBranch, TreeExampleSplitter, "([node, examples[, weight]]) -/-> (ExampleGeneratorList, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsAsBranchSizes, TreeExampleSplitter, "([branchIndex, node, examples[, weight]]) -/-> (ExampleGenerator, [list of weight ID's])")
C_CALL(TreeExampleSplitter_UnknownsAsSelector, TreeExampleSplitter, "([branchIndex, node, examples[, weight]]) -/-> (ExampleGenerator, [list of weight ID's])")
C_CALL(TreeDescender_UnknownToBranch, TreeDescender, "(node, example) -/-> (node, {distribution | None})")
C_CALL(TreeDescender_UnknownToCommonBranch, TreeDescender, "(node, example) -/-> (node, {distribution | None})")
C_CALL(TreeDescender_UnknownToCommonSelector, TreeDescender, "(node, example) -/-> (node, {distribution | None})")
C_CALL(TreeDescender_UnknownMergeAsBranchSizes, TreeDescender, "(node, example) -/-> (node, {distribution | None})")
C_CALL(TreeDescender_UnknownMergeAsSelector, TreeDescender, "(node, example) -/-> (node, {distribution | None})")
ABSTRACT(TreePruner, Orange)
C_CALL (TreePruner_SameMajority, TreePruner, "([tree]) -/-> tree")
C_CALL (TreePruner_m, TreePruner, "([tree]) -/-> tree")
PyObject *TreeNode_treesize(PyObject *self, PyObject *, PyObject *) PYARGS(METH_NOARGS, "() -> int")
{ PyTRY
return PyInt_FromLong(PyOrange_AsTreeNode(self)->treeSize());
PyCATCH
}
PyObject *TreeNode_removestoredinfo(PyObject *self, PyObject *, PyObject *) PYARGS(METH_NOARGS, "() -> None")
{ PyTRY
PyOrange_AsTreeNode(self)->removeStoredInfo();
RETURN_NONE;
PyCATCH
}
PyObject *TreeStopCriteria_new(PyTypeObject *type, PyObject *args, PyObject *keywords) BASED_ON(Orange, "()")
{ if (type == (PyTypeObject *)&PyOrTreeStopCriteria_Type) {
PyObject *name=NULL;
if (args && !PyArg_ParseTuple(args, "|O", &name))
PYERROR(PyExc_TypeError, "TreeStopCriteria: invalid arguments - name or callback function expected", PYNULL);
if (!args || !name || name && PyString_Check(name)) {
PyObject *self = WrapNewOrange(mlnew TTreeStopCriteria(), type);
if (name)
PyObject_SetAttrString(self, "name", name);
return self;
}
// (args && name && !PyStringString_Check(name)
return setCallbackFunction(WrapNewOrange(mlnew TTreeStopCriteria_Python(), type), args);
}
return WrapNewOrange(mlnew TTreeStopCriteria_Python(), type);
}
/* This is all twisted: Python classes are derived from TreeStopCriteria;
although the underlying C++ structure is TreeStopCriteria_Python,
the Python base is always TreeStopCritera. We must therefore define
TreeStopCriteria__reduce__ to handle both C++ objects, and need not
define TreeStopCriteria_Python__reduce__
*/
PyObject *TreeStopCriteria__reduce__(PyObject *self)
{
POrange orself = PyOrange_AS_Orange(self);
if (orself.is_derived_from(TTreeStopCriteria_Python) && PyObject_HasAttrString(self, "__callback")) {
PyObject *packed = packOrangeDictionary(self);
PyObject *callback = PyDict_GetItemString(packed, "__callback");
PyDict_DelItemString(packed, "__callback");
return Py_BuildValue("O(O)N", self->ob_type, callback, packed);
}
/* This works for ordinary (not overloaded) TreeStopCriteria
and for Python classes derived from TreeStopCriteria.
The latter have different self->ob_type, so TreeStopCriteria_new will construct
an instance of TreeStopCriteria_Python */
return Py_BuildValue("O()N", self->ob_type, packOrangeDictionary(self));
}
PyObject *TreeStopCriteria_lowcall(PyObject *self, PyObject *args, PyObject *keywords, bool allowPython)
{
static TTreeStopCriteria _cbdefaultStop;
PyTRY
NO_KEYWORDS
CAST_TO(TTreeStopCriteria, stop);
if (!stop)
PYERROR(PyExc_SystemError, "attribute error", PYNULL);
PExampleGenerator egen;
PDomainContingency dcont;
int weight = 0;
if (!PyArg_ParseTuple(args, "O&|O&O&:TreeStopCriteria.__call__", pt_ExampleGenerator, &egen, pt_weightByGen(egen), &weight, ptn_DomainContingency, &dcont))
return PYNULL;
bool res;
if (allowPython || (stop->classDescription() != &TTreeStopCriteria_Python::st_classDescription))
res = (*stop)(egen, weight, dcont);
else
res = _cbdefaultStop(egen, weight, dcont);
return PyInt_FromLong(res ? 1 : 0);
PyCATCH
}
PyObject *TreeStopCriteria_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("([examples, [weight, domainContingency]) -> bool")
{ return TreeStopCriteria_lowcall(self, args, keywords, false); }
PyObject *TreeStopCriteria_Python_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("([examples, [weight, domainContingency, apriorClass, candidates]) -/-> (Classifier, descriptions, sizes, quality)")
{ return TreeStopCriteria_lowcall(self, args, keywords, false); }
PyObject *TreeSplitConstructor_new(PyTypeObject *type, PyObject *args, PyObject *keywords) BASED_ON(Orange, "<abstract>")
{ if (type == (PyTypeObject *)&PyOrTreeSplitConstructor_Type)
return setCallbackFunction(WrapNewOrange(mlnew TTreeSplitConstructor_Python(), type), args);
else
return WrapNewOrange(mlnew TTreeSplitConstructor_Python(), type);
}
PyObject *TreeSplitConstructor__reduce__(PyObject *self)
{
return callbackReduce(self, PyOrTreeSplitConstructor_Type);
}
PyObject *TreeSplitConstructor_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("(examples[, weight, contingency, apriori class distribution, candidates, nodeClassifier]) -> (Classifier, descriptions, sizes, quality)")
{ PyTRY
NO_KEYWORDS
if (PyOrange_OrangeBaseClass(self->ob_type) == &PyOrTreeSplitConstructor_Type) {
PyErr_Format(PyExc_SystemError, "TreeSplitConstructor.call called for '%s': this may lead to stack overflow", self->ob_type->tp_name);
return PYNULL;
}
PExampleGenerator gen;
int weightID = 0;
PDomainContingency dcont;
PDistribution apriori;
PyObject *pycandidates = PYNULL;
PClassifier nodeClassifier;
if (!PyArg_ParseTuple(args, "O&|O&O&O&OO&:TreeSplitConstructor.call", pt_ExampleGenerator, &gen, pt_weightByGen(gen), &weightID, ccn_DomainContingency, &dcont, ccn_Distribution, &apriori, &pycandidates, ccn_Classifier, &nodeClassifier))
return PYNULL;
vector<bool> candidates;
if (pycandidates) {
PyObject *iterator = PyObject_GetIter(pycandidates);
if (!iterator)
PYERROR(PyExc_SystemError, "TreeSplitConstructor.call: cannot iterate through candidates; a list exected", PYNULL);
for(PyObject *item = PyIter_Next(iterator); item; item = PyIter_Next(iterator)) {
candidates.push_back(PyObject_IsTrue(item) != 0);
Py_DECREF(item);
}
Py_DECREF(iterator);
if (PyErr_Occurred())
return PYNULL;
}
PClassifier branchSelector;
PStringList descriptions;
PDiscDistribution subsetSizes;
float quality;
int spentAttribute;
branchSelector = SELF_AS(TTreeSplitConstructor)(descriptions, subsetSizes, quality, spentAttribute,
gen, weightID, dcont, apriori, candidates, nodeClassifier);
return Py_BuildValue("NNNfi", WrapOrange(branchSelector), WrapOrange(descriptions), WrapOrange(subsetSizes), quality, spentAttribute);
PyCATCH
}
PyObject *TreeExampleSplitter_new(PyTypeObject *type, PyObject *args, PyObject *keywords) BASED_ON(Orange, "<abstract>")
{ if (type == (PyTypeObject *)&PyOrTreeExampleSplitter_Type)
return setCallbackFunction(WrapNewOrange(mlnew TTreeExampleSplitter_Python(), type), args);
else
return WrapNewOrange(mlnew TTreeExampleSplitter_Python(), type);
}
PyObject *TreeExampleSplitter__reduce__(PyObject *self)
{
return callbackReduce(self, PyOrTreeExampleSplitter_Type);
}
PyObject *TreeExampleSplitter_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("(node, examples[, weight]) -/-> (ExampleGeneratorList, list of weight ID's")
{ PyTRY
NO_KEYWORDS
if (PyOrange_OrangeBaseClass(self->ob_type) == &PyOrTreeExampleSplitter_Type) {
PyErr_Format(PyExc_SystemError, "TreeExampleSplitter.call called for '%s': this may lead to stack overflow", self->ob_type->tp_name);
return PYNULL;
}
PTreeNode node;
PExampleGenerator gen;
int weightID = 0;
if (!PyArg_ParseTuple(args, "O&O&|O&:TreeExampleSplitter.call", cc_TreeNode, &node, pt_ExampleGenerator, &gen, pt_weightByGen(gen), &weightID))
return PYNULL;
vector<int> newWeights;
PExampleGeneratorList egl = SELF_AS(TTreeExampleSplitter)(node, gen, weightID, newWeights);
if (newWeights.size()) {
PyObject *pyweights = PyList_New(newWeights.size());
int i = 0;
ITERATE(vector<int>, li, newWeights)
PyList_SetItem(pyweights, i++, PyInt_FromLong(*li));
return Py_BuildValue("NN", WrapOrange(egl), pyweights);
}
else {
return Py_BuildValue("NO", WrapOrange(egl), Py_None);
}
PyCATCH
}
PyObject *TreeDescender_new(PyTypeObject *type, PyObject *args, PyObject *keywords) BASED_ON(Orange, "<abstract>")
{ if (type == (PyTypeObject *)&PyOrTreeDescender_Type)
return setCallbackFunction(WrapNewOrange(mlnew TMeasureAttribute_Python(), type), args);
else
return WrapNewOrange(mlnew TTreeDescender_Python(), type);
}
PyObject *TreeDescender__reduce__(PyObject *self)
{
return callbackReduce(self, PyOrTreeDescender_Type);
}
PyObject *TreeDescender_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("(node, example) -/-> (node, {distribution | None})")
{ PyTRY
NO_KEYWORDS
if (PyOrange_OrangeBaseClass(self->ob_type) == &PyOrTreeDescender_Type) {
PyErr_Format(PyExc_SystemError, "TreeDescender.call called for '%s': this may lead to stack overflow", self->ob_type->tp_name);
return PYNULL;
}
PTreeNode onode;
TExample *example;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -