📄 lib_learner.cpp
字号:
/*
This file is part of Orange.
Orange is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Orange is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Orange; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Authors: Janez Demsar, Blaz Zupan, 1996--2002
Contact: janez.demsar@fri.uni-lj.si
*/
/********************************
This file includes constructors and specialized methods for ML* object, defined in project Learners
*********************************/
#ifdef _MSC_VER
#pragma warning (disable : 4786 4114 4018 4267 4244)
#endif
#include <string>
#include "vars.hpp"
#include "domain.hpp"
#include "examples.hpp"
#include "examplegen.hpp"
#include "nearest.hpp"
#include "estimateprob.hpp"
#include "induce.hpp"
#include "cost.hpp"
#include "measures.hpp"
#include "distance.hpp"
#include "contingency.hpp"
#include "callback.hpp"
#include "cls_orange.hpp"
#include "cls_value.hpp"
#include "cls_example.hpp"
#include "lib_kernel.hpp"
#include "converts.hpp"
#include "vectortemplates.hpp"
#include "slist.hpp"
#include "externs.px"
/* ************ MAJORITY AND COST ************ */
#include "majority.hpp"
C_CALL(MajorityLearner, Learner, "([examples] [, weight=, estimate=]) -/-> Classifier")
C_CALL(CostLearner, Learner, "([examples] [, weight=, estimate=, costs=]) -/-> Classifier")
//#include "linreg.hpp"
PYXTRACT_IGNORE C_CALL(LinRegLearner, Learner, "([examples] [, weight=]) -/-> Classifier")
PYXTRACT_IGNORE C_NAMED(LinRegClassifier, ClassifierFD, "([classifier=, costs=])")
#include "costwrapper.hpp"
C_CALL(CostWrapperLearner, Learner, "([examples] [, weight=, costs=]) -/-> Classifier")
C_NAMED(CostWrapperClassifier, Classifier, "([classifier=, costs=])")
/************* ASSOCIATION RULES ************/
#include "assoc.hpp"
C_CALL(AssociationLearner, Learner, "([examples] [, weight=, conf=, supp=, voteWeight=]) -/-> Classifier")
C_NAMED(AssociationClassifier, ClassifierFD, "([rules=, voteWeight=])")
C_CALL3(AssociationRulesInducer, AssociationRulesInducer, Orange, "([examples[, weightID]], confidence=, support=]) -/-> AssociationRules")
C_CALL3(AssociationRulesSparseInducer, AssociationRulesSparseInducer, Orange, "([examples[, weightID]], confidence=, support=]) -/-> AssociationRules")
bool operator < (const TAssociationRule &, const TAssociationRule &) { return false; }
bool operator > (const TAssociationRule &, const TAssociationRule &) { return false; }
PyObject *AssociationRulesInducer_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("(examples[, weightID]) -> AssociationRules")
{
PyTRY
NO_KEYWORDS
int weightID;
PExampleGenerator egen = exampleGenFromArgs(args, weightID);
if (!egen)
return PYNULL;
return WrapOrange(SELF_AS(TAssociationRulesInducer)(egen, weightID));
PyCATCH
}
PyObject *AssociationRulesSparseInducer_call(PyObject *self, PyObject *args, PyObject *keywords) PYDOC("(examples[, weightID]) -> AssociationRules")
{
PyTRY
NO_KEYWORDS
int weightID = 0;
PExampleGenerator egen = exampleGenFromArgs(args, weightID);
if (!egen)
return PYNULL;
return WrapOrange(SELF_AS(TAssociationRulesSparseInducer)(egen, weightID));
PyCATCH
}
bool convertFromPython(PyObject *, PAssociationRule &);
PyObject *AssociationRule_new(PyTypeObject *type, PyObject *args, PyObject *) BASED_ON(Orange, "(left, right, support, confidence)")
{ PyTRY
PAssociationRule rule;
return convertFromPython(args, rule) ? WrapOrange(rule) : PYNULL;
PyCATCH
}
PyObject *AssociationRule__reduce__(PyObject *self)
{
PyTRY
CAST_TO(TAssociationRule, arule);
return Py_BuildValue("O(NN)N", self->ob_type,
Example_FromWrappedExample(arule->left),
Example_FromWrappedExample(arule->right),
packOrangeDictionary(self));
PyCATCH
}
PyObject *AssociationRule_appliesLeft(PyObject *self, PyObject *arg, PyObject *) PYARGS(METH_O, "(example) -> bool")
{ PyTRY
if (!PyOrExample_Check(arg))
PYERROR(PyExc_TypeError, "attribute error (example expected)", PYNULL);
CAST_TO(TAssociationRule, rule)
return PyInt_FromLong(rule->appliesLeft(PyExample_AS_ExampleReference(arg)) ? 1 : 0);
PyCATCH
}
PyObject *AssociationRule_appliesRight(PyObject *self, PyObject *arg, PyObject *) PYARGS(METH_O, "(example) -> bool")
{ PyTRY
if (!PyOrExample_Check(arg))
PYERROR(PyExc_TypeError, "attribute error (example expected)", PYNULL);
CAST_TO(TAssociationRule, rule)
return PyInt_FromLong(rule->appliesRight(PyExample_AS_ExampleReference(arg)) ? 1 : 0);
PyCATCH
}
PyObject *AssociationRule_appliesBoth(PyObject *self, PyObject *arg, PyObject *) PYARGS(METH_O, "(example) -> bool")
{ PyTRY
if (!PyOrExample_Check(arg))
PYERROR(PyExc_TypeError, "attribute error (example expected)", PYNULL);
CAST_TO(TAssociationRule, rule)
return PyInt_FromLong(rule->appliesBoth(PyExample_AS_ExampleReference(arg)) ? 1 : 0);
PyCATCH
}
PyObject *AssociationRule_native(PyObject *self)
{ PyTRY
CAST_TO(TAssociationRule, rule)
return Py_BuildValue("NNff", Example_FromWrappedExample(rule->left), Example_FromWrappedExample(rule->right), rule->support, rule->confidence);
PyCATCH
}
bool convertFromPython(PyObject *obj, PAssociationRule &rule)
{ if (PyOrOrange_Check(obj))
if (!PyOrange_AS_Orange(obj)) {
rule = PAssociationRule();
return true;
}
else if (PyOrAssociationRule_Check(obj)) {
rule = PyOrange_AsAssociationRule(obj);
return true;
}
TExample *le, *re;
switch (PyTuple_Size(obj)) {
case 6:
float nAppliesLeft, nAppliesRight, nAppliesBoth, nExamples;
if (PyArg_ParseTuple(obj, "O&O&ffff:convertFromPython(AssociationRule)", ptr_Example, &le, ptr_Example, &re, &nAppliesLeft, &nAppliesRight, &nAppliesBoth, &nExamples)) {
PExample nle = mlnew TExample(*le);
PExample nre = mlnew TExample(*re);
rule = mlnew TAssociationRule(nle, nre, nAppliesLeft, nAppliesRight, nAppliesBoth, nExamples);
return true;
}
else
break;
case 2:
case 3:
case 4: {
float support = -1, confidence = -1;
if (PyArg_ParseTuple(obj, "O&O&|ff:convertFromPython(AssociationRule)", ptr_Example, &le, ptr_Example, &re, &support, &confidence)) {
PExample nle = mlnew TExample(*le);
PExample nre = mlnew TExample(*re);
rule = mlnew TAssociationRule(nle, nre);
rule->support = support;
rule->confidence = confidence;
return true;
}
else
break;
}
case 1:
if (PyArg_ParseTuple(obj, "O&:convertFromPython(AssociationRule)", cc_AssociationRule, &rule))
return true;
else
break;
}
PYERROR(PyExc_TypeError, "invalid arguments", false);
}
string side2string(PExample ex)
{ string res;
if (ex->domain->variables->empty())
ITERATE(TMetaValues, mi, ex->meta) {
if (res.length())
res += " ";
res += ex->domain->getMetaVar((*mi).first)->name;
}
else {
string val;
TVarList::const_iterator vi(ex->domain->variables->begin());
for(TExample::const_iterator ei(ex->begin()), ee(ex->end()); ei!=ee; ei++, vi++)
if (!(*ei).isSpecial()) {
if (res.length())
res += " ";
(*vi)->val2str(*ei, val);
res += (*vi)->name + "=" + val;
}
}
return res;
}
PyObject *AssociationRule_str(TPyOrange *self)
{
PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "str", "repr");
if (result)
return result;
CAST_TO(TAssociationRule, rule);
return PyString_FromFormat("%s -> %s", side2string(rule->left).c_str(), side2string(rule->right).c_str());
}
PyObject *AssociationRule_repr(TPyOrange *self)
{
PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "repr", "str");
if (result)
return result;
CAST_TO(TAssociationRule, rule);
return PyString_FromFormat("%s -> %s", side2string(rule->left).c_str(), side2string(rule->right).c_str());
}
PAssociationRules PAssociationRules_FromArguments(PyObject *arg) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::P_FromArguments(arg); }
PyObject *AssociationRules_FromArguments(PyTypeObject *type, PyObject *arg) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_FromArguments(type, arg); }
PyObject *AssociationRules_new(PyTypeObject *type, PyObject *arg, PyObject *kwds) BASED_ON(Orange, "(<list of AssociationRule>)") ALLOWS_EMPTY { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_new(type, arg, kwds); }
PyObject *AssociationRules_getitem_sq(TPyOrange *self, int index) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_getitem(self, index); }
int AssociationRules_setitem_sq(TPyOrange *self, int index, PyObject *item) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_setitem(self, index, item); }
PyObject *AssociationRules_getslice(TPyOrange *self, int start, int stop) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_getslice(self, start, stop); }
int AssociationRules_setslice(TPyOrange *self, int start, int stop, PyObject *item) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_setslice(self, start, stop, item); }
int AssociationRules_len_sq(TPyOrange *self) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_len(self); }
PyObject *AssociationRules_richcmp(TPyOrange *self, PyObject *object, int op) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_richcmp(self, object, op); }
PyObject *AssociationRules_concat(TPyOrange *self, PyObject *obj) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_concat(self, obj); }
PyObject *AssociationRules_repeat(TPyOrange *self, int times) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_repeat(self, times); }
PyObject *AssociationRules_str(TPyOrange *self) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_str(self); }
PyObject *AssociationRules_repr(TPyOrange *self) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_str(self); }
int AssociationRules_contains(TPyOrange *self, PyObject *obj) { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_contains(self, obj); }
PyObject *AssociationRules_append(TPyOrange *self, PyObject *item) PYARGS(METH_O, "(AssociationRule) -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_append(self, item); }
PyObject *AssociationRules_extend(TPyOrange *self, PyObject *obj) PYARGS(METH_O, "(sequence) -> None") { return ListOfWrappedMethods<PAssociationRules, TAssociationRules, PAssociationRule, &PyOrAssociationRule_Type>::_extend(self, obj); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -