Changeset 7327:16bd08b46a2d in orange


Ignore:
Timestamp:
02/03/11 18:34:20 (3 years ago)
Author:
mocnik <mocnik@…>
Branch:
default
Convert:
935252b3b6c730ab6630102d26105c5a0b36f939
Message:

Updating the knn module documentation during Bohinj retreat.

Location:
orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/classification/knn.py

    r7305 r7327  
    22.. index: knn 
    33 
    4 Module :mod:`orange.classification.knn` includes classes for classification based on nearest neighbours 
    5 algorithm and also classes for finding instances near the given one.  
     4Module :mod:`orange.classification.knn` includes classes for classification 
     5based on nearest neighbours algorithm and also classes for finding instances 
     6near the given one.  
    67 
    78==================== 
     
    3031    .. method:: __call__(instances) 
    3132         
    32         Return learned kNNClassifier 
     33        Return instance of :class:`kNNClassifier` that learns from the 
     34        :obj:`instances`. 
    3335         
    3436        :param instances: table of instances 
     
    5658metrics will be used. :class:`kNNLearner` then constructs an instance of  
    5759:class:`FindNearest_BruteForce`. Together with ID of meta feature with  
    58 weights of instances, :obj:`k` and :obj:`rankWeight`, it is passed to a :class:`kNNClassifier`. 
    59  
    60 .. class:: kNNClassifier(domain, weightID, k, FindNearest, rankWeight, nExamples) 
     60weights of instances, :attr:`kNNLearner.k` and :attr:`kNNLearner.rankWeight`, 
     61it is passed to a :class:`kNNClassifier`. 
     62 
     63.. class:: kNNClassifier(domain, weightID, k, FindNearest, rankWeight, \ 
     64nExamples) 
    6165 
    6266    .. method:: __call__(instance) 
     
    6569        :type instance: Orange.data.Instance 
    6670         
    67         :param return_type: return value and probabilities, only value or only probabilities 
    68         :type return_type: Orange.classifier.getBoth, Orange.classifier.getValue, Orange.classifier.getProbilities 
    69          
    70         :rtype: :class:`Orange.data.Value`, :class:`Orange.statistics.distribution`, or a tuple with both 
     71        :param return_type: return value and probabilities, only value or only 
     72                            probabilities 
     73        :type return_type: Orange.classifier.getBoth,  
     74                           Orange.classifier.getValue, 
     75                           Orange.classifier.getProbilities 
     76         
     77        :rtype: :class:`Orange.data.Value`, 
     78                :class:`Orange.statistics.distribution`, or a tuple with both 
    7179         
    7280    .. method:: findNearest(instance) 
     
    7583         
    7684    :param instance: given instance 
    77     :type instance: Orange.data.instance 
    78          
    79     :rtype: :class:`Orange.data.instance` 
     85    :type instance: Orange.data.Instance 
     86         
     87    :rtype: :class:`Orange.data.Instance` 
    8088     
    8189     
     
    96104     
    97105    The number of learning instances. It is used to compute the number of  
    98     neighbours if :obj:`k` is zero. 
     106    neighbours if :attr:`kNNClassifier.k` is zero. 
    99107 
    100108When called to classify an instance, the classifier first calls  
    101109:meth:`kNNClassifier.findNearest`  
    102 to retrieve a list with :obj:`k` nearest neighbours. The component  
    103 :meth:`kNNClassifier.findNearest` has  
     110to retrieve a list with :attr:`kNNClassifier.k` nearest neighbours. The 
     111component :meth:`kNNClassifier.findNearest` has  
    104112a stored table of instances (those that have been passed to the learner)  
    105113together with their weights. If instances are weighted (non-zero  
     
    107115 
    108116If :meth:`kNNClassifier.findNearest` returns only one neighbour  
    109 (this is the case if :obj:`k=1`), :class:`kNNClassifier` returns the neighbour's 
    110 class. 
     117(this is the case if :obj:`k=1`), :class:`kNNClassifier` returns the 
     118neighbour's class. 
    111119 
    112120Otherwise, the retrieved neighbours vote about the class prediction 
     
    117125on the setting of :obj:`rankWeight`. 
    118126 
    119 * if :obj:`rankWeight` is :obj:`false`, :obj:`t` is a distance from the instance being 
    120   classified 
    121 * if :obj:`rankWeight` is :obj:`true`, neighbours are ordered and :obj:`t` is the position 
    122   of the neighbour on the list (a rank) 
    123  
    124  
    125 In both cases, :obj:`s` is chosen so that the impact of the farthest instance is 
    126 0.001. 
     127* if :obj:`rankWeight` is :obj:`false`, :obj:`t` is a distance from the 
     128  instance being classified 
     129* if :obj:`rankWeight` is :obj:`true`, neighbours are ordered and :obj:`t` 
     130  is the position of the neighbour on the list (a rank) 
     131 
     132 
     133In both cases, :obj:`s` is chosen so that the impact of the farthest instance 
     134is 0.001. 
    127135 
    128136Weighting gives the classifier certain insensitivity to the number of 
     
    197205.. class:: FindNearest 
    198206 
    199     A class for brute force search for nearest neighbours. It stores a table of 
    200     instances (it's its own copy of instances, not only Orange.data.Table with 
    201     references to another Orange.data.Table). When asked for neighbours, it 
    202     measures distances to all instances, stores them in a heap and returns the 
    203     first k as an Orange.data.Table with references to instances stored in 
     207    A class for brute force search for nearest neighbours. It stores a table  
     208    of instances (it's its own copy of instances, not only Orange.data.Table 
     209    with references to another Orange.data.Table). When asked for neighbours, 
     210    it measures distances to all instances, stores them in a heap and returns  
     211    the first k as an Orange.data.Table with references to instances stored in 
    204212    FindNearest's field instances). 
    205213     
     
    219227     
    220228    :param instance: given instance 
    221     :type instance: Orange.data.instance 
     229    :type instance: Orange.data.Instance 
    222230     
    223231    :param n: number of neighbours 
    224232    :type n: int 
    225233     
    226     :rtype: list(Orange.data.instance) 
     234    :rtype: list of :obj:`Orange.data.Instance` 
    227235     
    228236.. class:: FindNearestConstructor() 
     
    235243    If there are more instances with the same distance fighting for the last 
    236244    places, the tie is resolved by randomly picking the appropriate number of 
    237     instances. A local random generator is constructed and initiated by a constant 
    238     computed from the reference instance. The effect of this is that same random 
    239     neighbours will be chosen for the instance each time FindNearest_BruteForce 
     245    instances. A local random generator is constructed and initiated by a 
     246    constant computed from the reference instance. The effect of this is that 
     247    same random neighbours will be chosen for the instance each time 
     248    FindNearest_BruteForce 
    240249    is called. 
    241250     
     
    245254    distances between instances. Learning can be, for instances, storing the 
    246255    ranges of continuous features or the number of value of a discrete feature 
    247     (see the page about measuring distances for more information). The result of 
    248     learning is an instance of ExamplesDistance that should be used for measuring 
    249     distances between instances. 
     256    (see the page about measuring distances for more information). The result 
     257    of learning is an instance of ExamplesDistance that should be used for 
     258    measuring distances between instances. 
    250259     
    251260    .. attribute:: includeSame 
    252261     
    253     Tells whether to include the examples that are same as the reference; default is true. 
     262    Tells whether to include the examples that are same as the reference; 
     263    default is true. 
    254264     
    255265    .. method:: __call__(table, weightID, distanceID) 
    256266     
    257         Constructs an instance of FindNearest that would return neighbours of a 
    258         given instance, obeying weightID when counting them (also, some measures 
    259         of distance might consider weights as well) and store the distances in a 
    260         meta attribute with ID distanceID. 
     267        Constructs an instance of FindNearest that would return neighbours of 
     268        a given instance, obeying weightID when counting them (also, some  
     269        measures of distance might consider weights as well) and store the  
     270        distances in a meta attribute with ID distanceID. 
    261271     
    262272        :param table: table of instances 
  • orange/doc/Orange/rst/code/knnlearner.py

    r7295 r7327  
    88table = Orange.data.Table("iris") 
    99 
     10print "Testing using euclidean distance" 
    1011rndind = Orange.core.MakeRandomIndices2(table, p0=0.8) 
    1112train = table.select(rndind, 0) 
     
    1718    print instance.getclass(), knn(instance) 
    1819 
    19 print "\n\n" 
     20print "\n" 
     21print "Testing using hamming distance" 
    2022table = Orange.data.Table("iris") 
    2123knn = Orange.classification.knn.kNNLearner() 
Note: See TracChangeset for help on using the changeset viewer.