html tool

2018年4月8日星期日

knn-注释


 18 def classify0(inX, dataSet, labels, k):                   
 19     dataSetSize = dataSet.shape[0]                         
 20     diffMat = tile(inX, (dataSetSize,1)) - dataSet  #参见 numpy.tile(A, B)     
 23     sqDiffMat = diffMat**2
 24     sqDistances = sqDiffMat.sum(axis=1)  #参见sum(axis=1)                   
 25     distances = sqDistances**0.5 
 26     sortedDistIndicies = distances.argsort()               
 27     classCount={}                                         
 28     for i in range(k):
 29         voteIlabel = labels[sortedDistIndicies[i]]
30         classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1

参考:
sum(axis=1) -- https://blog.csdn.net/ikerpeng/article/details/17026011

2.python的 numpy当中
现在对于数据的处理更多的还是numpy。没有axis参数表示全部相加,axis=0表示按列相加,axis=1表示按照行的方向相加
[python] view plain copy
  1. >>> import numpy as np  
  2. >>> a=np.sum([[0,1,2],[2,1,3]])  
  3. >>> a  
  4. 9  
  5. >>> a.shape  
  6. ()  
  7. >>> a=np.sum([[0,1,2],[2,1,3]],axis=0)  
  8. >>> a  
  9. array([225])  
  10. >>> a.shape  
  11. (3,)  
  12. >>> a=np.sum([[0,1,2],[2,1,3]],axis=1)  
  13. >>> a  
  14. array([36])  
  15. >>> a.shape  
  16. (2,)  

numpy.tile(A,B) -- https://blog.csdn.net/ksearch/article/details/21388985


重复A,B次,这里的B可以时int类型也可以是远组类型。
[python] view plain copy
  1. >>> import numpy  
  2. >>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次  
  3. array([0000000000])  
  4. >>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次,行1次  
  5. array([[00]])  
  6. >>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次,行2次  
  7. array([[00],  
  8.        [00]])  
  9. >>> numpy.tile([0,0],(3,1))  
  10. array([[00],  
  11.        [00],  
  12.        [00]])  
  13. >>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次,行1次  
  14. array([[000000]])  
  15. >>> numpy.tile([0,0],(2,3))"font-family: Arial, Helvetica, sans-serif;">#在列方向上重复[0,0]3次,行2次  
  16. array([[000000],  
  17.        [000000]])  

没有评论:

发表评论