Ошибка типа: только массивы размером 1 могут быть преобразованы в скаляры Python

#python #pandas #numpy #vectorization #knn

Вопрос:

При реализации алгоритма KNN произошла следующая ошибка. Я посмотрел его и выяснил, что нам нужно использовать векторизацию, но даже если мы ее применим, ошибки продолжают возникать. Можно ли использовать векторизацию для массивов? Я хочу знать, как устранить ошибку. Я умоляю тебя.

 *Test Data Index is 0 / Traceback (most recent call last):
  File "C:UserssimjaPycharmProjectspythonProject1main.py", line 103, in <module>
    print("Computed Class is", m_knn.WeightedMajorityVote(m_knn.GetNearestList(GaitData(gait_dt['data'], gait_dt['target']),k)), end = ' / ')
  File "C:UserssimjaPycharmProjectspythonProject1main.py", line 27, in GetNearestList
    distanceDataList.append(self.CalculateDistance(self.gaits[i], gait))
  File "C:UserssimjaPycharmProjectspythonProject1main.py", line 11, in CalculateDistance
    gx = math.pow(gait1.GyroscopeX - gait2.GyroscopeX, 2)
TypeError: only size-1 arrays can be converted to Python scalars*
 
 
class m_KNN:

    gaits = [] # gait's list


    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.concat([pd.read_csv('normal_LX.csv',  usecols=[1,2,3,4,5,6,7,8])])
target = pd.concat([pd.read_csv('normal_LX.csv', usecols=[2])])

gait_dt = {'data': np.array(gait, dtype=object).astype(np.float64), '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'], gait_dt['target']))

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['data'], gait_dt['target']),k)), end = ' / ')
    print("True Class is", gait_dt.target_names[target[iter]], "n")
 

Комментарии:

1. math функции работают только со скалярами, одиночными значениями, а не с массивами.