An error occurred while executing the KNN algorithm. I don’t know where the error occurred. Can anyone help me? Please. There is a code below. I don’t know why, but the code was cut.
JavaScript
x
115
115
1
import numpy as np
2
import math
3
import pandas as pd
4
import matplotlib.pyplot as plt
5
6
class m_KNN:
7
gaits = []
8
9
def CalculateDistance(self, gait1, gait2): # Get gait <-> gait distance
10
gx = math.pow(gait1.GyroscopeX - gait2.GyroscopeX, 2)
11
gy = math.pow(gait1.GyroscopeY - gait2.GyroscopeY, 2)
12
gz = math.pow(gait1.GyroscopeZ - gait2.GyroscopeZ, 2)
13
ax = math.pow(gait1.AccelerometerX - gait2.AccelerometerX, 2)
14
ay = math.pow(gait1.AccelerometerY - gait2.AccelerometerY, 2)
15
az = math.pow(gait1.AccelerometerZ - gait2.AccelerometerZ, 2)
16
mx = math.pow(gait1.MagnetometerX - gait2.MagnetometerX, 2)
17
my = math.pow(gait1.MagnetometerY - gait2.MagnetometerY, 2)
18
mz = math.pow(gait1.MagnetometerZ - gait2.MagnetometerZ, 2)
19
return math.sqrt(gx+gy+gz+ax+ay+az+mx+my+mz)
20
21
def GetNearestList(self, gait, k): # Get the nearest data list number of K from test data
22
distanceDataList = []
23
nearestDataList = []
24
25
for i in range(len(self.gaits)): # for making test data <-> training data's distance list
26
distanceDataList.append(self.CalculateDistance(self.gaits[i], gait))
27
28
for i in range(k): # for making the NearestDataList number of K
29
nearestDataList.append(distanceDataList.index(min(distanceDataList)))
30
distanceDataList.remove(min(distanceDataList))
31
32
return nearestDataList
33
34
35
def WeightedMajorityVote(self, answer): # Get test data's class by weighted-majority-vote
36
targetClassList = []
37
38
bias = -0.5
39
40
weight0 = 0.2
41
weight1 = 0.3
42
weight2 = 0.5
43
44
length = len(answer)
45
for i in range(length):
46
targetClassList.append(self.gaits[answer[i]].targetClass)
47
48
setosaCount = targetClassList[:length//3].count(0) * weight2 + targetClassList[length//3:2*length//3].count(0) * weight1 + targetClassList[2*length//3:].count(0) * weight0 + bias
49
versicolorCount = targetClassList[:length//3].count(1) * weight2 +targetClassList[length//3:2*length//3].count(1) * weight1 + targetClassList[2*length//3:].count(1) * weight0 + bias
50
virginicaCount = targetClassList[:length//3].count(2) * weight2 +targetClassList[length//3:2*length//3].count(2) * weight1 + targetClassList[2*length//3:].count(2) * weight0 + bias
51
52
maxIdx = 0
53
54
if(versicolorCount > setosaCount and versicolorCount > virginicaCount) :
55
maxIdx = 1
56
57
if(virginicaCount > setosaCount and virginicaCount > versicolorCount):
58
maxIdx = 2
59
60
return targetClassList[maxIdx]
61
62
63
64
65
66
class GaitData:
67
68
def __init__(self, data, targetClass):
69
70
self.GyroscopeX = data[0]
71
self.GyroscopeY = data[1]
72
self.GyroscopeZ = data[2]
73
self.AccelerometerX = data[3]
74
self.AccelerometerY = data[4]
75
self.AccelerometerZ = data[5]
76
self.MagnetometerX = data[6]
77
self.MagnetometerY = data[7]
78
self.MagnetometerZ = data[8]
79
self.targetClass = targetClass
80
81
82
83
m_knn = m_KNN()
84
85
86
87
gait = pd.read_csv('normal_LX.csv', usecols=[1, 2, 3, 4, 5, 6, 7, 8])
88
89
90
target = pd.read_csv('abnormal_LX.csv', usecols=[1])
91
92
93
gait_dt = {'data': np.array(gait, dtype=object), 'target': np.array(target), 'target_names': ['정상', '비정상']}
94
95
k = 3
96
97
for iter in range(len(target)):
98
99
if (iter % 15 == 0 and iter != 0):
100
101
None
102
else:
103
104
m_knn.gaits.append(GaitData(gait_dt.data[iter], gait_dt.target[iter]))
105
print("nWeighted Majority Votenn")
106
107
108
109
for t in range(10):
110
111
iter = 15 * (t + 1) - 1
112
print("Test Data Index is", t, end=' / ')
113
print("Computed Class is", m_knn.WeightedMajorityVote(m_knn.GetNearestList(GaitData(gait_dt[iter], target[iter]),k)), end = ' / ')
114
print("True Class is", gait_dt.target_names[target[iter]], "n")
115
Advertisement
Answer
One line defines:
JavaScript
1
2
1
gait_dt = {'data': np.array(gait, dtype=object), 'target': np.array(target), 'target_names': ['정상', '비정상']}
2
That’s a dict
comprehension statement
In the next loop you have
JavaScript
1
2
1
gait_dt.data[iter], gait_dt.target[iter]
2
It’s that use of .data
that’s giving problem. With a dict
have access values with
JavaScript
1
2
1
gait_dt['data']
2
syntax, not with the attribute syntax.