Changeset 9470:52bad2e86b1a in orange


Ignore:
Timestamp:
08/05/11 16:43:13 (3 years ago)
Author:
wencanluo <wencanluo@…>
Branch:
default
Convert:
f6f9fdbb122be4760d579f46e2c1a7ad69dcc802
Message:

BR-kNN method for multi-label is finished

Location:
orange
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/__init__.py

    r9464 r9470  
    6666_import("multilabel.lp") 
    6767_import("multilabel.mlknn") 
     68_import("multilabel.brknn") 
    6869_import("multilabel.mmp") 
    6970 
  • orange/Orange/multilabel/__init__.py

    r9464 r9470  
    88from lp import LabelPowersetClassifier 
    99 
     10from multiknn import MultikNNLearner 
     11from multiknn import MultikNNClassifier 
     12 
    1013from mlknn import MLkNNLearner 
    1114from mlknn import MLkNNClassifier 
    1215 
     16from brknn import BRkNNLearner 
     17from brknn import BRkNNClassifier 
     18 
    1319from mmp import MMPLearner 
    1420from mmp import MMPClassifier 
  • orange/Orange/multilabel/mlknn.py

    r9466 r9470  
    33    
    44.. index::  
    5    single: ML-kNN;  ML-kNN Learner 
     5   single: multilabel;  ML-kNN Learner 
    66 
    77*************************************** 
     
    5555import Orange 
    5656import label 
    57 import multibase as _multibase 
    58  
    59 class MLkNNLearner(_multibase.MultiLabelLearner): 
     57import multiknn as _multiknn 
     58 
     59class MLkNNLearner(_multiknn.MultikNNLearner): 
    6060    """ 
    6161    Class implementing the ML-kNN (Multi-Label k Nearest Neighbours) algorithm. The class is based on the  
     
    111111    .. attribute:: k 
    112112     
    113         Number of neighbors. If set to 0 (which is also the default value),  
    114         the square root of the number of instances is used. 
     113        Number of neighbors. The default value is 1  
    115114     
    116115    .. attribute:: smooth 
     
    120119    .. attribute:: knn 
    121120         
    122         :class:`Orange.classification.knn.kNNLearner` for nearest neighbor search 
     121        :class:`Orange.classification.knn.FindNearest` for nearest neighbor search 
    123122     
    124123    """ 
     
    140139        """ 
    141140         
    142         self = _multibase.MultiLabelLearner.__new__(cls, **argkw) 
    143          
    144         self.k = k 
     141        self = _multiknn.MultikNNLearner.__new__(cls, k, **argkw) 
    145142        self.smooth = smooth 
    146143         
     
    156153            self.__dict__[k] = kwds[k] 
    157154 
    158         self.num_labels = label.get_num_labels(instances) 
    159         self.label_indices = label.get_label_indices(instances) 
    160          
     155        _multiknn.MultikNNLearner.transfor_table(self,instances) 
     156 
    161157        num_labels = self.num_labels 
    162         label_indices = self.label_indices 
    163158        k = self.k 
    164159         
     
    175170        self.cond_nprobabilities  = [ [0.] * (k + 1) ] * num_labels 
    176171         
    177         #build a kNNLearner 
    178         #remove labels 
    179         indices_remove = [var for index, var in enumerate(label_indices)] 
    180         new_domain = label.remove_indices(instances,indices_remove)  
    181          
    182         new_class = Orange.data.variable.Discrete("label") 
    183         for e in instances: 
    184             class_value = label.get_label_bitstream(instances,e) 
    185             new_class.add_value(class_value) 
    186          
    187         new_domain = Orange.data.Domain(new_domain,new_class) 
    188          
    189         new_table = Orange.data.Table(new_domain) 
    190         for e in instances: 
    191             new_row = Orange.data.Instance( 
    192               new_domain,  
    193               [v.value for v in e if v.variable.attributes.has_key('label') <> 1] + 
    194                     [label.get_label_bitstream(instances,e)]) 
    195              
    196             new_table.append(new_row) 
    197         self.knn = Orange.classification.knn.kNNLearner(new_table,k) 
    198          
    199172        #Computing the prior probabilities P(H_b^l) 
    200173        self.compute_prior() 
     
    203176        self.compute_cond() 
    204177         
    205         return MLkNNClassifier(instances = instances, label_indices = label_indices,  
     178        return MLkNNClassifier(instances = self.instances, label_indices = self.label_indices,  
    206179                               prior_probabilities = self.prior_probabilities,  
    207180                               prior_nprobabilities = self.prior_nprobabilities, 
     
    235208 
    236209        for i  in range(num_instances): 
    237             neighbors = self.knn.findNearest(instances[i], k) 
     210            neighbors = self.knn(instances[i], k) 
    238211                  
    239212            # now compute values of temp_ci and temp_nci for every class label 
     
    262235                self.cond_nprobabilities[i][j] = (self.smooth + temp_nci[i][j]) / (self.smooth * (k + 1) + temp2) 
    263236  
    264 class MLkNNClassifier(_multibase.MultiLabelClassifier):       
     237class MLkNNClassifier(_multiknn.MultikNNClassifier):       
    265238    def __call__(self, example, result_type=Orange.classification.Classifier.GetValue): 
    266239        num_labels = len(self.label_indices) 
     
    272245         
    273246        #Computing y_t and r_t 
    274         neighbors = self.knn.findNearest(example, self.k) 
     247        neighbors = self.knn(example, self.k) 
    275248        for i in range(num_labels): 
    276249            # compute sum of aces in KNN 
  • orange/doc/Orange/hiearchy.txt

    r9464 r9470  
    3737      tuning 
    3838   multilabel 
    39       br; lp; mlknn; mmp; 
     39      br; lp; multiknn; mlknn; brknn; mmp 
  • orange/doc/Orange/rst/Orange.multilabel.rst

    r9464 r9470  
    1010   Orange.multilabel.br 
    1111   Orange.multilabel.lp 
     12   Orange.multilabel.multiknn 
    1213   Orange.multilabel.mlknn 
     14   Orange.multilabel.brknn 
    1315   Orange.multilabel.mmp 
  • orange/doc/Orange/rst/code/mlc-classify.py

    r9469 r9470  
    2727    print c,p 
    2828     
    29 mmp_cliassifer = Orange.multilabel.MMPLearner(data) 
     29br_cliassifer = Orange.multilabel.BRkNNLearner(data,k=1) 
    3030for e in data: 
    31     c,p = mmp_cliassifer(e,Orange.classification.Classifier.GetBoth) 
     31    c,p = br_cliassifer(e,Orange.classification.Classifier.GetBoth) 
    3232    print c,p 
Note: See TracChangeset for help on using the changeset viewer.