2021-03-07 20:51

# KNN算法报错Traceback(most recent call last)求指点！

```import csv

#读取
import random

with open('泽泻.csv','r') as file:

#分组
random.shuffle(datas)
n=len(datas)//8

test_set=datas[0:n]
train_set=datas[n:]

print(test_set)

#KNN
#距离
def distance(d1,d2):
res=0

for key in ("Li7","Be9","B11","Mg24","Al27","Si28","P31","Ca43","Sc45","Ti47","V51","Cr52","Mn55","Fe57","Co59","Ni60","Cu63"):
res+=(float(d1[key])-float(d2[key]))**2

return res**0.5

K=5
def knn(data):
#1.距离
res=[
for train in train_set
]

#2.排序--升序
res=sorted(res,key=lambda item:item['distance'])

#3.取前K个
res2=res[0:K]

#4.加权平均
result={'scps':0,'scjj':0,'scms':0,'fjjo':0}

#总距离
sum=0
for r in res2:
sum+=r['distance']

#加权平均
for r in res2:
result[r['result']]+=1-int(r['distance'])/sum

List=[result['scjj'],result['scms'],result['scps'],result['fjjo']]
Max = List[0]  # 定义变量Max用来存储最大值，初始值赋值为列表中任意一个值
for result in List:
if result > Max:
Max = result
print("产地", Max)

#测试阶段
correct=0
for test in test_set:
result2=knn(test)

if result==result2:
correct+=1

print(correct)
print(len(test_set))

print("准确率：{:.2f}%".format(100*correct/len(test_set)))```

C:\Users\MI\PycharmProjects\pythonProject1\venv\Scripts\python.exe C:/Users/MI/PycharmProjects/pythonProject1/main.py
[OrderedDict([('NUMBER', '23'), ('MADE IN', 'SCMS'), ('Li7', '-0.19076'), ('Be9', '-0.43392'), ('B11', '6.26521'), ('Mg24', '1058.95009'), ('Al27', '29.79623'), ('Si28', '57.76329'), ('P31', '3951.6313'), ('Ca43', '384.75796'), ('Sc45', '-0.00845'), ('Ti47', '15.67376'), ('V51', '0.0396'), ('Cr52', '0.64036'), ('Mn55', '140.32365'), ('Fe57', '72.06235'), ('Co59', '0.10151'), ('Ni60', '0.54709'), ('Cu63', '12.99252'), ('Zn66', '69.79617'), ('Ga69', '0.32461'), ('Ge74', '-0.05042'), ('As75', '-0.01965'), ('Se82', '0.05993'), ('Sr88', '13.7484'), ('Y89', '0.00095'), ('Zr90', '-0.00854'), ('Nb93', '-0.00951'), ('Mo98', '0.09503'), ('Ag107', '-0.02107'), ('Cd111', '0.19343'), ('Sn118', '0.0317'), ('Sb121', '-0.03601'), ('I127', '0.01656'), ('Cs133', '0.0477'), ('Ba138', '6.61879'), ('La139', '0.00891'), ('Ce140', '0.0275'), ('Pr141', '-0.00623'), ('Nd146', '-0.04114'), ('Sm147', '-0.06185'), ('Eu153', '-0.01529'), ('Gd157', '-0.05246'), ('Tb159', '-0.0102'), ('Dy163', '-0.04126'), ('Ho165', '-0.01085'), ('Er166', '-0.03344'), ('Tm169', '-0.01131'), ('Yb172', '-0.05223'), ('Lu175', '-0.01256'), ('Tl205', '0.04357'), ('Pb208', '0.03478'), ('Th232', '-0.01079'), ('U238', '-0.00651'), ('Hg202', '0.25103')]), OrderedDict([('NUMBER', '38'), ('MADE IN', 'SCMS'), ('Li7', '-0.20038'), ('Be9', '-0.46457'), ('B11', '6.8582'), ('Mg24', '1146.45634'), ('Al27', '30.78726'), ('Si28', '61.56183'), ('P31', '3807.71266'), ('Ca43', '466.78775'), ('Sc45', '-0.07493'), ('Ti47', '14.89309'), ('V51', '0.0424'), ('Cr52', '0.76246'), ('Mn55', '111.22555'), ('Fe57', '73.39458'), ('Co59', '0.09521'), ('Ni60', '0.65951'), ('Cu63', '15.3477'), ('Zn66', '67.15154'), ('Ga69', '0.42166'), ('Ge74', '-0.05366'), ('As75', '-0.0221'), ('Se82', '0.08958'), ('Sr88', '15.73633'), ('Y89', '0.00122'), ('Zr90', '-0.00962'), ('Nb93', '-0.00985'), ('Mo98', '0.10009'), ('Ag107', '-0.02207'), ('Cd111', '0.27118'), ('Sn118', '0.03531'), ('Sb121', '-0.04017'), ('I127', '0.00069'), ('Cs133', '0.04205'), ('Ba138', '8.39563'), ('La139', '0.01208'), ('Ce140', '0.03351'), ('Pr141', '-0.00717'), ('Nd146', '-0.04767'), ('Sm147', '-0.0712'), ('Eu153', '-0.01811'), ('Gd157', '-0.06239'), ('Tb159', '-0.01238'), ('Dy163', '-0.04931'), ('Ho165', '-0.01297'), ('Er166', '-0.03943'), ('Tm169', '-0.0135'), ('Yb172', '-0.06317'), ('Lu175', '-0.01527'), ('Tl205', '0.04148'), ('Pb208', '0.03708'), ('Th232', '-0.01744'), ('U238', '-0.0121'), ('Hg202', '0.32283')]), OrderedDict([('NUMBER', '8'), ('MADE IN', 'SCPS'), ('Li7', '0.07292'), ('Be9', '0.15135'), ('B11', '6.79449'), ('Mg24', '888.89382'), ('Al27', '31.44988'), ('Si28', '54.01897'), ('P31', '3655.71199'), ('Ca43', '472.53931'), ('Sc45', '0.07059'), ('Ti47', '15.17865'), ('V51', '0.03666'), ('Cr52', '0.6055'), ('Mn55', '166.79752'), ('Fe57', '54.94576'), ('Co59', '0.08394'), ('Ni60', '0.95705'), ('Cu63', '12.91657'), ('Zn66', '60.25624'), ('Ga69', '0.43087'), ('Ge74', '0.01401'), ('As75', '0.12529'), ('Se82', '0.09834'), ('Sr88', '11.11966'), ('Y89', '0.01009'), ('Zr90', '0.00517'), ('Nb93', '0.00222'), ('Mo98', '0.12668'), ('Ag107', '0.00919'), ('Cd111', '0.29243'), ('Sn118', '0.72231'), ('Sb121', '0.0057'), ('I127', '0.05635'), ('Cs133', '0.04936'), ('Ba138', '8.54864'), ('La139', '0.03187'), ('Ce140', '0.05934'), ('Pr141', '0.00657'), ('Nd146', '0.02386'), ('Sm147', '0.01512'), ('Eu153', '0.00603'), ('Gd157', '0.01116'), ('Tb159', '0.00127'), ('Dy163', '0.00571'), ('Ho165', '0.00162'), ('Er166', '0.00472'), ('Tm169', '0.00163'), ('Yb172', '0.00526'), ('Lu175', '0.0015'), ('Tl205', '0.07481'), ('Pb208', '0.14013'), ('Th232', '0.00154'), ('U238', '0.00194'), ('Hg202', '0.0152')]), OrderedDict([('NUMBER', '17'), ('MADE IN', 'SCPS'), ('Li7', '0.0791'), ('Be9', '0.16164'), ('B11', '7.13783'), ('Mg24', '970.2156'), ('Al27', '51.97529'), ('Si28', '74.24137'), ('P31', '4095.48759'), ('Ca43', '534.14616'), ('Sc45', '0.0716'), ('Ti47', '17.17465'), ('V51', '0.06404'), ('Cr52', '0.73563'), ('Mn55', '181.92244'), ('Fe57', '75.65754'), ('Co59', '0.09199'), ('Ni60', '0.87028'), ('Cu63', '12.11039'), ('Zn66', '56.73303'), ('Ga69', '0.42325'), ('Ge74', '0.01241'), ('As75', '0.12878'), ('Se82', '0.09881'), ('Sr88', '11.61834'), ('Y89', '0.01313'), ('Zr90', '0.01026'), ('Nb93', '0.00384'), ('Mo98', '0.14994'), ('Ag107', '0.00936'), ('Cd111', '0.3218'), ('Sn118', '1.67536'), ('Sb121', '0.0067'), ('I127', '0.07734'), ('Cs133', '0.0464'), ('Ba138', '8.16709'), ('La139', '0.02918'), ('Ce140', '0.05885'), ('Pr141', '0.00631'), ('Nd146', '0.02616'), ('Sm147', '0.01534'), ('Eu153', '0.0054'), ('Gd157', '0.0129'), ('Tb159', '0.00134'), ('Dy163', '0.00658'), ('Ho165', '0.00139'), ('Er166', '0.00523'), ('Tm169', '0.00138'), ('Yb172', '0.00432'), ('Lu175', '0.00117'), ('Tl205', '0.05868'), ('Pb208', '0.14005'), ('Th232', '0.00269'), ('U238', '0.00229'), ('Hg202', '0.01478')]), OrderedDict([('NUMBER', '57'), ('MADE IN', 'SCJJ'), ('Li7', '-0.16077'), ('Be9', '-0.37976'), ('B11', '7.6587'), ('Mg24', '1078.43522'), ('Al27', '53.19608'), ('Si28', '62.26314'), ('P31', '3489.33534'), ('Ca43', '381.86198'), ('Sc45', '-0.02212'), ('Ti47', '14.25639'), ('V51', '0.08714'), ('Cr52', '1.6568'), ('Mn55', '220.76746'), ('Fe57', '82.76437'), ('Co59', '0.0741'), ('Ni60', '0.70359'), ('Cu63', '12.42866'), ('Zn66', '78.31361'), ('Ga69', '0.5201'), ('Ge74', '-0.04507'), ('As75', '-0.01483'), ('Se82', '0.097'), ('Sr88', '14.68383'), ('Y89', '-0.0002'), ('Zr90', '-0.00289'), ('Nb93', '-0.00684'), ('Mo98', '0.38908'), ('Ag107', '-0.01307'), ('Cd111', '0.43029'), ('Sn118', '0.00724'), ('Sb121', '-0.02825'), ('I127', '0.03054'), ('Cs133', '0.00713'), ('Ba138', '10.22662'), ('La139', '0.00975'), ('Ce140', '0.02273'), ('Pr141', '-0.00522'), ('Nd146', '-0.0338'), ('Sm147', '-0.05182'), ('Eu153', '-0.01158'), ('Gd157', '-0.04229'), ('Tb159', '-0.00827'), ('Dy163', '-0.0337'), ('Ho165', '-0.0087'), ('Er166', '-0.02593'), ('Tm169', '-0.00919'), ('Yb172', '-0.04085'), ('Lu175', '-0.01011'), ('Tl205', '0.00168'), ('Pb208', '0.06983'), ('Th232', '-0.00815'), ('U238', '-0.00939'), ('Hg202', '0.1851')]), OrderedDict([('NUMBER', '35'), ('MADE IN', 'SCMS'), ('Li7', '-0.20534'), ('Be9', '-0.45738'), ('B11', '6.58782'), ('Mg24', '1120.37167'), ('Al27', '38.10905'), ('Si28', '67.71786'), ('P31', '3979.11233'), ('Ca43', '412.536'), ('Sc45', '-0.06396'), ('Ti47', '15.74847'), ('V51', '0.0473'), ('Cr52', '0.62484'), ('Mn55', '137.8507'), ('Fe57', '66.20347'), ('Co59', '0.10333'), ('Ni60', '0.58215'), ('Cu63', '18.75435'), ('Zn66', '72.27112'), ('Ga69', '0.35009'), ('Ge74', '-0.05679'), ('As75', '-0.0529'), ('Se82', '0.11874'), ('Sr88', '14.23488'), ('Y89', '0.0012'), ('Zr90', '-0.00893'), ('Nb93', '-0.00997'), ('Mo98', '0.10112'), ('Ag107', '-0.02385'), ('Cd111', '0.21655'), ('Sn118', '0.19906'), ('Sb121', '-0.03996'), ('I127', '0.02991'), ('Cs133', '0.04432'), ('Ba138', '7.00252'), ('La139', '0.01448'), ('Ce140', '0.03723'), ('Pr141', '-0.00707'), ('Nd146', '-0.04732'), ('Sm147', '-0.0737'), ('Eu153', '-0.01915'), ('Gd157', '-0.06279'), ('Tb159', '-0.01271'), ('Dy163', '-0.04965'), ('Ho165', '-0.01322'), ('Er166', '-0.04019'), ('Tm169', '-0.01365'), ('Yb172', '-0.06444'), ('Lu175', '-0.01556'), ('Tl205', '0.03389'), ('Pb208', '0.13043'), ('Th232', '-0.01644'), ('U238', '-0.01344'), ('Hg202', '0.33002')]), OrderedDict([('NUMBER', '15'), ('MADE IN', 'SCPS'), ('Li7', '0.06442'), ('Be9', '0.1073'), ('B11', '7.47806'), ('Mg24', '989.90334'), ('Al27', '35.91258'), ('Si28', '56.63802'), ('P31', '4518.81035'), ('Ca43', '562.02389'), ('Sc45', '0.06772'), ('Ti47', '17.77593'), ('V51', '0.04376'), ('Cr52', '0.65504'), ('Mn55', '148.89025'), ('Fe57', '67.43556'), ('Co59', '0.08852'), ('Ni60', '0.8865'), ('Cu63', '15.68955'), ('Zn66', '65.48877'), ('Ga69', '0.47583'), ('Ge74', '0.00967'), ('As75', '0.1533'), ('Se82', '0.17945'), ('Sr88', '13.48208'), ('Y89', '0.0107'), ('Zr90', '0.00547'), ('Nb93', '0.00238'), ('Mo98', '0.19526'), ('Ag107', '0.00931'), ('Cd111', '0.41935'), ('Sn118', '0.20404'), ('Sb121', '0.00764'), ('I127', '0.0969'), ('Cs133', '0.06751'), ('Ba138', '9.70337'), ('La139', '0.02922'), ('Ce140', '0.05274'), ('Pr141', '0.0059'), ('Nd146', '0.02383'), ('Sm147', '0.01491'), ('Eu153', '0.00642'), ('Gd157', '0.01074'), ('Tb159', '0.002'), ('Dy163', '0.00794'), ('Ho165', '0.00173'), ('Er166', '0.00642'), ('Tm169', '0.00169'), ('Yb172', '0.00683'), ('Lu175', '0.00199'), ('Tl205', '0.07755'), ('Pb208', '0.16456'), ('Th232', '0.00183'), ('U238', '0.00196'), ('Hg202', '0.01979')]), OrderedDict([('NUMBER', '31'), ('MADE IN', 'SCMS'), ('Li7', '-0.20566'), ('Be9', '-0.48053'), ('B11', '7.08583'), ('Mg24', '1215.50059'), ('Al27', '42.01565'), ('Si28', '76.47073'), ('P31', '4221.14874'), ('Ca43', '465.85134'), ('Sc45', '-0.03941'), ('Ti47', '16.71209'), ('V51', '0.05819'), ('Cr52', '1.00837'), ('Mn55', '119.14111'), ('Fe57', '85.03262'), ('Co59', '0.09392'), ('Ni60', '0.67094'), ('Cu63', '14.15623'), ('Zn66', '74.10324'), ('Ga69', '0.35893'), ('Ge74', '-0.05739'), ('As75', '-0.03198'), ('Se82', '0.12051'), ('Sr88', '15.07901'), ('Y89', '0.00156'), ('Zr90', '-0.00957'), ('Nb93', '-0.01066'), ('Mo98', '0.10684'), ('Ag107', '-0.0251'), ('Cd111', '0.27068'), ('Sn118', '0.01519'), ('Sb121', '-0.04398'), ('I127', '0.01819'), ('Cs133', '0.04075'), ('Ba138', '7.29485'), ('La139', '0.01266'), ('Ce140', '0.03595'), ('Pr141', '-0.00761'), ('Nd146', '-0.04892'), ('Sm147', '-0.07675'), ('Eu153', '-0.01956'), ('Gd157', '-0.06595'), ('Tb159', '-0.013'), ('Dy163', '-0.05207'), ('Ho165', '-0.01341'), ('Er166', '-0.04208'), ('Tm169', '-0.0145'), ('Yb172', '-0.06643'), ('Lu175', '-0.01617'), ('Tl205', '0.04108'), ('Pb208', '0.0167'), ('Th232', '-0.01675'), ('U238', '-0.01237'), ('Hg202', '0.34076')]), OrderedDict([('NUMBER', '59'), ('MADE IN', 'SCJJ'), ('Li7', '-0.1678'), ('Be9', '-0.39959'), ('B11', '7.76189'), ('Mg24', '1073.57296'), ('Al27', '60.70893'), ('Si28', '60.04673'), ('P31', '3951.78001'), ('Ca43', '373.37163'), ('Sc45', '-0.01398'), ('Ti47', '15.56632'), ('V51', '0.12617'), ('Cr52', '1.92846'), ('Mn55', '248.68651'), ('Fe57', '97.37762'), ('Co59', '0.08706'), ('Ni60', '0.79912'), ('Cu63', '11.84578'), ('Zn66', '89.44117'), ('Ga69', '0.60395'), ('Ge74', '-0.04723'), ('As75', '-0.00675'), ('Se82', '0.11829'), ('Sr88', '15.69852'), ('Y89', '0.00026'), ('Zr90', '0.00106'), ('Nb93', '-0.0073'), ('Mo98', '0.47451'), ('Ag107', '-0.01304'), ('Cd111', '0.55889'), ('Sn118', '0.00598'), ('Sb121', '-0.03229'), ('I127', '0.00796'), ('Cs133', '0.0091'), ('Ba138', '12.39134'), ('La139', '0.01162'), ('Ce140', '0.026'), ('Pr141', '-0.00524'), ('Nd146', '-0.03417'), ('Sm147', '-0.05357'), ('Eu153', '-0.01174'), ('Gd157', '-0.04697'), ('Tb159', '-0.00923'), ('Dy163', '-0.03702'), ('Ho165', '-0.0099'), ('Er166', '-0.02983'), ('Tm169', '-0.00999'), ('Yb172', '-0.04659'), ('Lu175', '-0.0114'), ('Tl205', '0.0006'), ('Pb208', '0.1056'), ('Th232', '-0.00928'), ('U238', '-0.01038'), ('Hg202', '0.21057')])]
Traceback (most recent call last):
File "C:/Users/MI/PycharmProjects/pythonProject1/main.py", line 67, in <module>
result2=knn(test)
File "C:/Users/MI/PycharmProjects/pythonProject1/main.py", line 39, in knn
res=sorted(res,key=lambda item:item['distance'])
TypeError: '<' not supported between instances of 'collections.OrderedDict' and 'collections.OrderedDict'

Process finished with exit code 1

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 2条回答

• 兔子先生〃 1月前

点赞 评论 复制链接分享
• 兔子先生〃 1月前
``````import csv

#读取
import random

with open('泽泻.csv','r') as file:

#分组
random.shuffle(datas)
n=len(datas)//8

test_set=datas[0:n]
train_set=datas[n:]

print(test_set)

#KNN
#距离
def distance(d1,d2):
res=0

for key in ("Li7","Be9","B11","Mg24","Al27","Si28","P31","Ca43","Sc45","Ti47","V51","Cr52","Mn55","Fe57","Co59","Ni60","Cu63"):
res+=(float(d1[key])-float(d2[key]))**2

return res**0.5

K=5
def knn(data):
#1.距离
res=[
for train in train_set
]

#2.排序--升序
res=sorted(res,key=lambda item:item['distance'])

#3.取前K个
res2=res[0:K]

#4.加权平均
result={'scps':0,'scjj':0,'scms':0,'fjjo':0}

#总距离
sum=0
for r in res2:
sum+=r['distance']

#加权平均
for r in res2:
result[r['result']]+=1-int(r['distance'])/sum

List=[result['scjj'],result['scms'],result['scps'],result['fjjo']]
Max = List[0]  # 定义变量Max用来存储最大值，初始值赋值为列表中任意一个值
for result in List:
if result > Max:
Max = result
print("产地", Max)

#测试阶段
correct=0
for test in test_set: