python-ML学习小记-L1-KNN

from Distance import Distance

cal=Distance()

k=3

s=[["t1",0],["t2",0]]

pt=[[[1,2],s[0][0],"NaN"]

,[[3,2],s[0][0],"NaN"]

,[[4,3],s[0][0],"NaN"]

,[[6,6],s[1][0],"NaN"]

,[[6,7],s[1][0],"NaN"]]

pre=[[4,1],"NaN"]

pNum=len(pt)

for i in range(pNum):

    pt[i][2]=cal.cEDistance(pt[i][0],pre[0])

'''sort

dis.sort()

sort2'''

for j in range(pNum-1):

    for i in range(pNum-1):

        if pt[i][2]>pt[i+1][2]:

            pt[i],pt[i+1]=pt[i+1],pt[i]

print pt

for i in range(k):

    for j in range(len(s)):

        if pt[i][1]==s[j][0]:

            s[j][1]+=1

for i in range(len(s)-1):

    for i in range(len(s)-1):

        if s[i][1]

            s[i],s[i+1]=s[i+1],s[i]

print s[0][0]

你可能感兴趣的:(python-ML学习小记-L1-KNN)