Changeset 11607:8ecd4831def9 in orange
 Timestamp:
 07/01/13 16:25:11 (10 months ago)
 Branch:
 default
 Location:
 source/orange
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

source/orange/lib_learner.cpp
r11606 r11607 1453 1453 raiseError("Error saving SVM model"); 1454 1454 } 1455 if(svm>kernelFunc) 1456 return Py_BuildValue("O(OOOsO)N", self>ob_type, 1457 WrapOrange(svm>classVar), 1455 1456 return Py_BuildValue("O(OsOOO)N", self>ob_type, 1457 WrapOrange(svm>domain), 1458 buf.c_str(), 1459 WrapOrange(svm>supportVectors), 1460 WrapOrange(svm>kernelFunc), 1458 1461 WrapOrange(svm>examples), 1459 WrapOrange(svm>supportVectors),1460 buf.c_str(),1461 WrapOrange(svm>kernelFunc),1462 packOrangeDictionary(self));1463 else1464 return Py_BuildValue("O(OOOs)N", self>ob_type,1465 WrapOrange(svm>classVar),1466 WrapOrange(svm>examples),1467 WrapOrange(svm>supportVectors),1468 buf.c_str(),1469 1462 packOrangeDictionary(self)); 1470 1463 PyCATCH … … 1480 1473 raiseError("Error saving SVM model."); 1481 1474 } 1482 if(svm>kernelFunc) 1483 return Py_BuildValue("O(OOOsbO)N", self>ob_type, 1484 WrapOrange(svm>classVar), 1485 WrapOrange(svm>examples), 1486 WrapOrange(svm>supportVectors), 1487 buf.c_str(), 1488 (char)(svm>useNonMeta? 1: 0), 1489 WrapOrange(svm>kernelFunc), 1490 packOrangeDictionary(self)); 1491 else 1492 return Py_BuildValue("O(OOOsb)N", self>ob_type, 1493 WrapOrange(svm>classVar), 1494 WrapOrange(svm>examples), 1495 WrapOrange(svm>supportVectors), 1475 1476 return Py_BuildValue("O(OsbOOO)N", self>ob_type, 1477 WrapOrange(svm>domain), 1496 1478 buf.c_str(), 1497 1479 (char)(svm>useNonMeta? 1: 0), 1480 WrapOrange(svm>supportVectors), 1481 WrapOrange(svm>kernelFunc), 1482 WrapOrange(svm>examples), 1498 1483 packOrangeDictionary(self)); 1499 1484 PyCATCH … … 1523 1508 1524 1509 1525 PyObject * SVMClassifier_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) BASED_ON(ClassifierFD, "(Variable, Examples, Examples, string, [kernelFunc]) > SVMClassifier") 1526 {PyTRY 1527 PVariable classVar; 1510 PyObject * SVMClassifier_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) BASED_ON(ClassifierFD, "(Domain, model_string, supportVectors, [kernelFunc, examples]) > SVMClassifier") 1511 { 1512 PyTRY 1513 PDomain domain; 1514 char* model_string = NULL; 1515 PExampleTable supportVectors; 1516 PKernelFunc kernel; 1528 1517 PExampleTable examples; 1529 PExampleTable supportVectors; 1530 char* model_string; 1531 PKernelFunc kernel; 1518 1532 1519 if (PyArg_ParseTuple(args, "")) 1533 1520 return WrapNewOrange(mlnew TSVMClassifier(), type); 1521 1534 1522 PyErr_Clear(); 1535 1536 if (!PyArg_ParseTuple(args, "O&O&O&sO&:__new__", cc_Variable, &classVar, ccn_ExampleTable, &examples, cc_ExampleTable, &supportVectors, &model_string, cc_KernelFunc, &kernel)) 1537 return NULL; 1523 1524 if (!PyArg_ParseTuple(args, "O&sO&O&O&", 1525 cc_Domain, &domain, 1526 &model_string, 1527 cc_ExampleTable, &supportVectors, 1528 ccn_KernelFunc, &kernel, 1529 ccn_ExampleTable, &examples)) { 1530 // Old pickle arguments format. 1531 PVariable classVar; 1532 if (!PyArg_ParseTuple(args, "O&O&O&sO&:__new__", 1533 cc_Variable, &classVar, 1534 ccn_ExampleTable, &examples, 1535 cc_ExampleTable, &supportVectors, 1536 &model_string, 1537 cc_KernelFunc, &kernel)) { 1538 return NULL; 1539 } 1540 PyErr_Clear(); 1541 domain = supportVectors>domain; 1542 } 1538 1543 1539 1544 string buffer(model_string); … … 1542 1547 raiseError("Error building LibSVM Model"); 1543 1548 1544 PSVMClassifier svm = mlnew TSVMClassifier( classVar, examples, supportVectors, model, kernel);1549 PSVMClassifier svm = mlnew TSVMClassifier(domain, model, supportVectors, kernel, examples); 1545 1550 1546 1551 return WrapOrange(svm); … … 1550 1555 1551 1556 1552 PyObject * SVMClassifierSparse_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) BASED_ON(SVMClassifier, "(Variable, Examples, Examples, string, [useNonMeta, kernelFunc]) > SVMClassifierSparse") 1553 {PyTRY 1554 PVariable classVar; 1557 PyObject * SVMClassifierSparse_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) BASED_ON(SVMClassifier, "(Domain, model_string, useNonMeta, supportVectors, [kernelFunc, examples]) > SVMClassifierSparse") 1558 { 1559 PyTRY 1560 PDomain domain; 1561 char* model_string = NULL; 1562 char useNonMeta = 0; 1563 1564 PExampleTable supportVectors; 1565 PKernelFunc kernel; 1555 1566 PExampleTable examples; 1556 PExampleTable supportVectors; 1557 char* model_string; 1558 char useNonMeta; 1559 PKernelFunc kernel; 1567 1560 1568 if (PyArg_ParseTuple(args, "")) 1561 1569 return WrapNewOrange(mlnew TSVMClassifierSparse(), type); 1570 1562 1571 PyErr_Clear(); 1563 1564 if (!PyArg_ParseTuple(args, "O&O&O&sbO&:__new__", cc_Variable, &classVar, ccn_ExampleTable, &examples, cc_ExampleTable, &supportVectors, &model_string, &useNonMeta, cc_KernelFunc, &kernel)) 1565 return NULL; 1566 1572 1573 if (!PyArg_ParseTuple(args, "O&sbO&O&O&:__new__", 1574 cc_Domain, &domain, 1575 &model_string, 1576 &useNonMeta, 1577 cc_ExampleTable, &supportVectors, 1578 ccn_KernelFunc, &kernel, 1579 ccn_ExampleTable, &examples)) { 1580 1581 // Old pickle arguments format. 1582 PVariable classVar; 1583 if (!PyArg_ParseTuple(args, "O&O&O&sbO&:__new__", 1584 cc_Variable, &classVar, 1585 ccn_ExampleTable, &examples, 1586 cc_ExampleTable, &supportVectors, 1587 &model_string, 1588 &useNonMeta, 1589 cc_KernelFunc, &kernel)) { 1590 return NULL; 1591 } 1592 1593 PyErr_Clear(); 1594 domain = supportVectors>domain; 1595 } 1596 1567 1597 string buffer(model_string); 1568 1598 svm_model* model = svm_load_model_alt(buffer); … … 1570 1600 raiseError("Error building LibSVM Model"); 1571 1601 1572 PSVMClassifier svm = mlnew TSVMClassifierSparse( classVar, examples, supportVectors, model, useNonMeta != 0, kernel);1602 PSVMClassifier svm = mlnew TSVMClassifierSparse(domain, model, useNonMeta != 0, supportVectors, kernel, examples); 1573 1603 1574 1604 return WrapOrange(svm); 
source/orange/libsvm_interface.cpp
r11606 r11607 727 727 PExampleTable supportVectors = extract_support_vectors(model, examples); 728 728 729 PVariable classVar; 730 731 if (param.svm_type == ONE_CLASS) { 732 classVar = mlnew TFloatVariable("one class"); 733 } else { 734 classVar = examples>domain>classVar; 735 } 736 737 return PClassifier(createClassifier(classVar, examples, supportVectors, model)); 729 PDomain domain = examples>domain; 730 731 return PClassifier(createClassifier(examples>domain, model, supportVectors, examples)); 738 732 } 739 733 … … 766 760 767 761 TSVMClassifier* TSVMLearner::createClassifier( 768 P Variable classVar, PExampleTable examples, PExampleTable supportVectors, svm_model* model) {762 PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples) { 769 763 if (kernel_type != PRECOMPUTED) { 770 764 examples = NULL; 771 765 } 772 return mlnew TSVMClassifier( classVar, examples, supportVectors, model, kernelFunc);766 return mlnew TSVMClassifier(domain, model, supportVectors, kernelFunc, examples); 773 767 } 774 768 … … 790 784 791 785 TSVMClassifier* TSVMLearnerSparse::createClassifier( 792 P Variable classVar, PExampleTable examples, PExampleTable supportVectors, svm_model* model) {786 PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples) { 793 787 if (kernel_type != PRECOMPUTED) { 794 788 examples = NULL; 795 789 } 796 return mlnew TSVMClassifierSparse( classVar, examples, supportVectors, model, useNonMeta, kernelFunc);790 return mlnew TSVMClassifierSparse(domain, model, useNonMeta, supportVectors, kernelFunc, examples); 797 791 } 798 792 799 793 800 794 TSVMClassifier::TSVMClassifier( 801 const PVariable &var, 802 PExampleTable examples, 795 PDomain domain, svm_model * model, 803 796 PExampleTable supportVectors, 804 svm_model* model, 805 PKernelFunc kernelFunc) { 806 this>classVar = var; 797 PKernelFunc kernelFunc, 798 PExampleTable examples 799 ) : TClassifierFD(domain) { 800 this>model = model; 801 this>supportVectors = supportVectors; 802 this>kernelFunc = kernelFunc; 807 803 this>examples = examples; 808 this>supportVectors = supportVectors; 809 this>model = model; 810 this>kernelFunc = kernelFunc; 811 812 domain = supportVectors>domain; 804 813 805 svm_type = svm_get_svm_type(model); 814 806 kernel_type = model>param.kernel_type; 815 807 808 if (svm_type == ONE_CLASS) { 809 this>classVar = mlnew TFloatVariable("one class"); 810 } 811 816 812 computesProbabilities = model && svm_check_probability_model(model) && \ 817 (svm_type != NU_SVR && svm_type != EPSILON_SVR); // Disable prob. estimation for regression813 (svm_type != NU_SVR && svm_type != EPSILON_SVR); // Disable prob. estimation for regression 818 814 819 815 int nr_class = svm_get_nr_class(model); … … 823 819 * class interface. 824 820 */ 825 if (svm_type == C_SVC  svm_type == NU_SVC){ 826 nSV = mlnew TIntList(nr_class); // num of SVs for each class (sum = model>l) 827 for(i = 0;i < nr_class; i++) 828 nSV>at(i) = model>nSV[i]; 829 } 821 if (svm_type == C_SVC  svm_type == NU_SVC) { 822 nSV = mlnew TIntList(nr_class); // num of SVs for each class (sum(nSV) == model>l) 823 for(i = 0;i < nr_class; i++) { 824 nSV>at(i) = model>nSV[i]; 825 } 826 } 830 827 831 828 coef = mlnew TFloatListList(nr_class1); 832 for(i = 0; i < nr_class  1; i++) {829 for(i = 0; i < nr_class  1; i++) { 833 830 TFloatList *coefs = mlnew TFloatList(model>l); 834 for(int j = 0;j < model>l; j++) 831 for(int j = 0;j < model>l; j++) { 835 832 coefs>at(j) = model>sv_coef[i][j]; 836 coef>at(i)=coefs; 837 } 838 rho = mlnew TFloatList(nr_class*(nr_class1)/2); 839 for(i = 0; i < nr_class*(nr_class1)/2; i++) 833 } 834 coef>at(i) = coefs; 835 } 836 837 // Number of binary classifiers in the model 838 int nr_bin_cls = nr_class * (nr_class  1) / 2; 839 840 rho = mlnew TFloatList(nr_bin_cls); 841 for(i = 0; i < nr_bin_cls; i++) { 840 842 rho>at(i) = model>rho[i]; 841 if(model>probA){ 842 probA = mlnew TFloatList(nr_class*(nr_class1)/2); 843 if (model>param.svm_type != NU_SVR && model>param.svm_type != EPSILON_SVR && model>probB) // Regression has only probA 844 probB = mlnew TFloatList(nr_class*(nr_class1)/2); 845 for(i=0; i<nr_class*(nr_class1)/2; i++){ 843 } 844 845 if(model>probA) { 846 probA = mlnew TFloatList(nr_bin_cls); 847 if (model>param.svm_type != NU_SVR && model>param.svm_type != EPSILON_SVR && model>probB) { 848 // Regression only has probA 849 probB = mlnew TFloatList(nr_bin_cls); 850 } 851 852 for(i=0; i<nr_bin_cls; i++) { 846 853 probA>at(i) = model>probA[i]; 847 if (model>param.svm_type != NU_SVR && model>param.svm_type != EPSILON_SVR && model>probB) 854 if (model>param.svm_type != NU_SVR && model>param.svm_type != EPSILON_SVR && model>probB) { 848 855 probB>at(i) = model>probB[i]; 849 } 850 } 851 } 856 } 857 } 858 } 859 } 860 852 861 853 862 TSVMClassifier::~TSVMClassifier(){ … … 856 865 } 857 866 } 867 858 868 859 869 PDistribution TSVMClassifier::classDistribution(const TExample & example){ 
source/orange/libsvm_interface.hpp
r11606 r11607 67 67 WRAPPER(KernelFunc) 68 68 69 //#include "callback.hpp"70 69 71 70 class ORANGE_API TSVMLearner : public TLearner{ … … 108 107 virtual int getNumOfElements(PExampleGenerator examples); 109 108 virtual TSVMClassifier* createClassifier( 110 PVariable var, PExampleTable examples, PExampleTable supportVectors, svm_model* model);109 PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples); 111 110 }; 112 111 … … 119 118 virtual int getNumOfElements(PExampleGenerator examples); 120 119 virtual TSVMClassifier* createClassifier( 121 P Variable classVar, PExampleTable examples, PExampleTable supportVectors, svm_model* model);120 PDomain domain, svm_model* model, PExampleTable supportVectors, PExampleTable examples); 122 121 }; 123 122 124 123 125 class ORANGE_API TSVMClassifier : public TClassifierFD {124 class ORANGE_API TSVMClassifier : public TClassifierFD { 126 125 public: 127 126 __REGISTER_CLASS 128 TSVMClassifier() {127 TSVMClassifier() { 129 128 this>model = NULL; 130 129 }; 131 130 132 TSVMClassifier( const PVariable & , PExampleTable examples, PExampleTable supportVectors,133 svm_model* model, PKernelFunc kernelFunc);131 TSVMClassifier(PDomain, svm_model * model, PExampleTable supportVectors, 132 PKernelFunc kernelFunc=NULL, PExampleTable examples=NULL); 134 133 135 134 ~TSVMClassifier(); … … 146 145 PFloatList probB; //P probB  pairwise probability information 147 146 PExampleTable supportVectors; //P support vectors 148 PExampleTable examples; //P (training instances when svm_type == Custom) 149 PKernelFunc kernelFunc; //P custom kernel function 147 148 PExampleTable examples; //P training instances when svm_type == Custom 149 PKernelFunc kernelFunc; //P custom kernel function used when svm_type == Custom 150 150 151 151 int svm_type; //P(&SVMLearner_SVMType) SVM type (C_SVC=0, NU_SVC, ONE_CLASS, EPSILON_SVR=3, NU_SVR=4) … … 162 162 }; 163 163 164 class ORANGE_API TSVMClassifierSparse : public TSVMClassifier {164 class ORANGE_API TSVMClassifierSparse : public TSVMClassifier { 165 165 public: 166 166 __REGISTER_CLASS 167 TSVMClassifierSparse(){}; 168 TSVMClassifierSparse(PVariable var, PExampleTable examples, PExampleTable supportVectors, 169 svm_model* model, bool useNonMeta, PKernelFunc kernelFunc 170 ) :TSVMClassifier(var, examples, supportVectors, model, kernelFunc){ 167 TSVMClassifierSparse() {}; 168 169 TSVMClassifierSparse( 170 PDomain domain, svm_model * model, bool useNonMeta, 171 PExampleTable supportVectors, 172 PKernelFunc kernelFunc=NULL, 173 PExampleTable examples=NULL 174 ) : TSVMClassifier(domain, model, supportVectors, kernelFunc, examples) { 171 175 this>useNonMeta = useNonMeta; 172 176 } 173 177 174 bool useNonMeta; //P include non meta attributes178 bool useNonMeta; //PR include non meta attributes 175 179 176 180 protected:
Note: See TracChangeset
for help on using the changeset viewer.