Skip to content
Advertisement

AttributeError: ‘dict’ object has no attribute ‘data’

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.

import numpy as np
import math
import pandas as pd
import matplotlib.pyplot as plt

class m_KNN:
    gaits = []

    def CalculateDistance(self, gait1, gait2): # Get gait <-> gait distance
        gx = math.pow(gait1.GyroscopeX - gait2.GyroscopeX, 2)
        gy = math.pow(gait1.GyroscopeY - gait2.GyroscopeY, 2)
        gz = math.pow(gait1.GyroscopeZ - gait2.GyroscopeZ, 2)
        ax = math.pow(gait1.AccelerometerX - gait2.AccelerometerX, 2)
        ay = math.pow(gait1.AccelerometerY - gait2.AccelerometerY, 2)
        az = math.pow(gait1.AccelerometerZ - gait2.AccelerometerZ, 2)
        mx = math.pow(gait1.MagnetometerX - gait2.MagnetometerX, 2)
        my = math.pow(gait1.MagnetometerY - gait2.MagnetometerY, 2)
        mz = math.pow(gait1.MagnetometerZ - gait2.MagnetometerZ, 2)
        return math.sqrt(gx+gy+gz+ax+ay+az+mx+my+mz)

    def GetNearestList(self, gait, k): # Get the nearest data list number of K from test data
        distanceDataList = []
        nearestDataList = []

        for i in range(len(self.gaits)): # for making test data <-> training data's distance list
            distanceDataList.append(self.CalculateDistance(self.gaits[i], gait))

        for i in range(k): # for making the NearestDataList number of K
            nearestDataList.append(distanceDataList.index(min(distanceDataList)))
            distanceDataList.remove(min(distanceDataList))

        return nearestDataList


    def WeightedMajorityVote(self, answer): # Get test data's class by weighted-majority-vote
        targetClassList = []

        bias = -0.5

        weight0 = 0.2
        weight1 = 0.3
        weight2 = 0.5

        length = len(answer)
        for i in range(length):
            targetClassList.append(self.gaits[answer[i]].targetClass)

        setosaCount = targetClassList[:length//3].count(0) * weight2 + targetClassList[length//3:2*length//3].count(0) * weight1 + targetClassList[2*length//3:].count(0) * weight0 + bias
        versicolorCount = targetClassList[:length//3].count(1) * weight2 +targetClassList[length//3:2*length//3].count(1) * weight1 + targetClassList[2*length//3:].count(1) * weight0 + bias
        virginicaCount = targetClassList[:length//3].count(2) * weight2 +targetClassList[length//3:2*length//3].count(2) * weight1 + targetClassList[2*length//3:].count(2) * weight0 + bias

        maxIdx = 0

        if(versicolorCount > setosaCount and versicolorCount > virginicaCount) :
            maxIdx = 1

        if(virginicaCount > setosaCount and virginicaCount > versicolorCount):
            maxIdx = 2

        return targetClassList[maxIdx]





class GaitData:

    def __init__(self, data, targetClass):

        self.GyroscopeX = data[0]
        self.GyroscopeY = data[1]
        self.GyroscopeZ = data[2]
        self.AccelerometerX = data[3]
        self.AccelerometerY = data[4]
        self.AccelerometerZ = data[5]
        self.MagnetometerX = data[6]
        self.MagnetometerY = data[7]
        self.MagnetometerZ = data[8]
        self.targetClass = targetClass



m_knn = m_KNN()



gait = pd.read_csv('normal_LX.csv',  usecols=[1, 2, 3, 4, 5, 6, 7, 8])


target = pd.read_csv('abnormal_LX.csv', usecols=[1])


gait_dt = {'data': np.array(gait, dtype=object), 'target': np.array(target), 'target_names': ['정상', '비정상']}

k = 3

for iter in range(len(target)):

    if (iter % 15 == 0 and iter != 0):

        None
    else:

        m_knn.gaits.append(GaitData(gait_dt.data[iter], gait_dt.target[iter]))
print("nWeighted Majority Votenn")



for t in range(10):

    iter = 15 * (t + 1) - 1
    print("Test Data Index is", t, end=' / ')
    print("Computed Class is", m_knn.WeightedMajorityVote(m_knn.GetNearestList(GaitData(gait_dt[iter], target[iter]),k)), end = ' / ')
    print("True Class is", gait_dt.target_names[target[iter]], "n")

Advertisement

Answer

One line defines:

gait_dt = {'data': np.array(gait, dtype=object), 'target': np.array(target), 'target_names': ['정상', '비정상']}

That’s a dict comprehension statement

In the next loop you have

gait_dt.data[iter], gait_dt.target[iter]

It’s that use of .data that’s giving problem. With a dict have access values with

gait_dt['data'] 

syntax, not with the attribute syntax.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement