如何在python中实现K近邻,从而从零开始获得更好的精度

2024-09-30 14:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

测试列表

Power, Label
23.5, light
34.5, light
52.3, light
82.3, fan
102.2, fan
110.3, fan

列车清单

tPower
24.3
32.5
45.3
def euclidean_distance(row1, row2, df):

    distance = 0.0

    #for row in range(len(df['tRealPower'])):
    for index, row in df.iterrows():
            distance += (row['RealPower'] - row['tRealPower'])**2
            d = sqrt(distance)
            distlist.append(d)
    return distlist

目前,row1是train list,row2是test list,但是我得到的结果有一个错误,它显示了具有相同值集的2 list

我试图实现KNN参考其他资源,但无法用于我的任务

我想做的是有一组具有不同设备标签的列值,我希望在测试值时能够使用K最近的Neaigbour分类器来获得最接近不同标签的列值集,并得到[预测标签和精度]的结果


Tags: indf列表for标签listrowdistance