Machine Learning in Action ---- kNN

------------ ------------ restore content begins

  . 1  # - * - Coding: UTF-. 8 - * - 
  2  "" " 
  . 3  the Created Thu-Nov 14 19:29:08 2019 ON
   . 4  
  . 5  @author: HTING
   . 6  " "" 
  . 7  
  . 8  # Import Scientific Computing packet module 
  . 9  Import numpy NP AS
 10  
. 11  # introduced into operator module 
12 is  import operator
 13 is  
14  # =================================== ========================================== 
15  # # import os module 
16  # os Import 
17  #================================================== =========================== 
18 is  
. 19  # Create a data set and the tag 
20 is  DEF CreateDataSet ():
 21 is      Group np.array = ([[ 1.0, 1.1 ], 
 22 is                        [1.0, 1.0 ], 
 23 is                        [0, 0],
 24                        [0, 0.1 ]])
 25      Labels = ( ' A ' , ' A ' , ' B ' , ' B ' )
 26 is      
27      returnGroup, Labels
 28  
29  
30  
31 is  ' '' 
32  
33 is  the Parameters:
 34 is  
35      inX - data (test set) for classification
 36      dataSet A - for the training data (training set)
 37 [      LABES - training data set label
 38 is      K - selecting a minimum distance k points
 39  
40  return:
 41 is  
42 is      sortedClassCount [0] [0] - predicted classification of the input data
 43 is  
44 is  ' '' 
45  
46 is  # - K-nearest neighbor algorithm 
47  
48  DEF classify0 (inX, k):
 49      
50      # Import dataSet A, Labels 
51 is      dataSet A, Labels =CreateDataSet ()
 52 is      
53 is      # calculates a distance 
54 is      # A.shape [i]: length of i-th dimension 
55      dataSetSize = dataSet.shape [0]
 56 is  
57 is      # a tile copied into the input vector and matrix data set as large as 
58      ' '' 
59          np.tile (A, reps): 
 60              array A get repeated a number of new array;
 61 is              A - array, List, tuple, dict, Matrix
 62 is                  , and basic data types int, string, float and bool type;
 63 is              reps . - tuple, List, dict, Array, int, BOOL
 64                  can not be a float, string, matrix type;
 65                  
66          np.tile (A, (m, n-)):
 67             数组A重复n次 --> nA;    # A重复n次
 68             nA --> m[nA].   # m 维的nA
 69     '''
 70     diffMat = np.tile(inX, (dataSetSize,1)) - dataSet
 71     sqDiffMat = diffMat ** 2
 72     
 73     '''
 74         In Numpy dimensions are called axes. 
 75         The number of axes is rank.
 76         
 77     '''
 78     sqDistances = sqDiffMat.sum(axis=1)
 79     # sqDistances = np.sum(sqDiffMat, axis=1)
 80     
 81     distances = sqDistances ** 0.5
 82     
 83     #According to the distance from small to large, and returns the corresponding index positions 
84      # A.argsort () [] 
85      sortedDistIndicies = distances.argsort ()
 86      
87      
88      # Create a dictionary, and the number of occurrences of the tag memory 
89      classCount = {}
 90      
91 is      # selecting the smallest distance k points 
92      for I in Range (k):
 93          '' ' 
94              for I in Range (m, n-, Z) | Range (Start, STOP, STEP)
 95              I <-> m - >. 1-n-, STEP = Z;
 96              default: m = 0, Z =. 1
 97          '' ' 
98          # label type lookup samples 
99         = voteIlabel Labels [sortedDistIndicies [I]]
 100          
101          # Sample tag type to be found in the dictionary +1 
102          '' ' 
103              if there voteIlabel,
 104                  then the generated dictionary classCount voteIlabel element, and it is the corresponding number 0 :
 105                  : classCount = {voteIlabel: 0} 
 106                  in this case classCount.get (voteIlabel, 0) function is to detect and generate new elements, brackets initialized as 0, then no effect;
 107              if there is a dictionary element voteIlabel when,
 108                  classCount.get (voteIlabel, 0) function is returned to the value corresponding to the element
 109          '' ' 
110          classCount [voteIlabel] = classCount.get (voteIlabel, 0) +. 1
 111          
112      #And returns the number of ordered tag type appears most 
113      '' ' 
114          the sorted (Iterable, CMP = None, Key = None, Reverse = False) -> the sorted new new List
 115              CMP - Accept function;
 1 16              Key - Element One Accept One function of, which iS function return,
 117                      the weight to Sort;
 1 18              Reverse - True -> positive Order;
 119                          False -> negative Order;
 120                          
121          operator.itemgetter ()
 122              for data acquisition which dimensional object, Some parameters for the serial number.
123              Note, operator.itemgetter function value is not obtained, but rather defines a function in order to obtain the value of the function by the action on the object.
124      '' ' 
125     sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
126 
127     return sortedClassCount[0][0]
128     
129     
130     

 

 

 

 

Guess you like

Origin www.cnblogs.com/HH520ting/p/11866679.html