如何修复“ int”对象没有属性“ triu_indices”

时间:2019-05-25 03:11:01

标签: python numpy for-loop int double

我正在使用double for循环来计算值。此for循环获取向量的i元素和同一向量的i + 1元素,然后进行一些计算。但是,当第二个for循环的第二个迭代开始时,出现错误“ int”对象没有属性“ triu_indices”

我有三个矩阵和一些函数。我也使用double来做(我不认为这是Python的方法,但是我正在学习)

我有这个:

import numpy as np 

#The three matrixes
flowMatrixSymNoCeros = np.array([[0,4,6,2,4,4],
                                 [4,0,4,2,2,8], 
                                 [6,4,0,2,2,6], 
                                 [2,2,2,0,6,2], 
                                 [4,2,2,6,0,10], 
                                 [4,8,6,2,10,0]])

flowMatrixSymCeros   = np.array([[0,0,6,2,4,0],
                                 [0,0,4,2,2,8], 
                                 [6,4,0,2,2,6], 
                                 [2,2,2,0,6,2], 
                                 [4,2,2,6,0,0], 
                                 [0,8,6,2,0,0]])

closenessRatingSymNoceros = np.array([[0,5,3,2,6,4],
                                      [5,0,5,2,6,2],
                                      [3,5,0,1,2,1],
                                      [2,2,1,0,2,2],
                                      [6,6,2,2,0,6],
                                      [4,2,1,2,6,0]])

matrices = np.array([flowMatrixSymNoCeros,
                    flowMatrixSymCeros,
                    closenessRatingSymNoceros])

def normalMatrixesAsym(matrices):
    matrixes = np.copy(matrices)
    matrixes = np.absolute(matrixes) 
    normalMatrixes = []
    for matriz in matrixes:
        s = np.sum(matriz)  
        normalMatrixes.append(matriz / s) 
    return np.asarray(normalMatrixes)

def sdwm(symetria, normalMatrix):

    SD= 0
    normalizedMatrix = np.copy(normalMatrix)
    m = normalizedMatrix.shape[0]
    sd = lambda num,den : (num/den)**(1/2) 
    # maskUpper = np.mask_indices(m, np.triu, 1) 
    # maskLower = np.mask_indices(m, np.tril, -1) 
    upper = normalizedMatrix[np.triu_indices(m,1)] 
    lower = normalizedMatrix[np.tril_indices(m,-1)] 
    upperAbs = np.abs(upper)
    if(symetria):
        media = np.absolute(np.mean(upper))
        num = np.sum((upperAbs - np.mean(media))**2) 
        den = ((m * (m-1))/2)-1 
        SD = sd(num,den) #calculo del SD
    else:
        # lower = np.tril(normalizedMatrix,-1) 
        matrixNoDiag = np.append(upper,lower)
        matrixNoDiagAbs = np.abs(matrixNoDiag)
        mean = np.absolute(np.mean(matrixNoDiag))
        num = np.sum((matrixNoDiagAbs - mean)**2) 
        den = (m*(m-1))-1 #calcula el denominador
        SD = sd(num,den) #calcula el SD
    return SD

def calcularCriticalValues(funcion, symetria, normalMatrixes):

    normalMatrices = np.copy(normalMatrixes)
    criticalValules = []
    for normalMatriz in normalMatrices:
        criticalValules.append(funcion(symetria,normalMatriz))
    return np.asarray(criticalValules)


normalizedMatrices = normalMatrixesAsym(matrices)
SD = calcularCriticalValues(nm.sdwm,False,normalizedMatrices)
m=len(matrices)
R= np.zeros((m,m))
n = len(normalizedMatrices[0])
for i,matrix in enumerate(normalizedMatrices):
        upperI = matrix[np.triu_indices(n,1)] 
        lowerI = matrix[np.tril_indices(n,-1)] 
        matrixNoDiagI = np.append(upperI,lowerI)
        meanI = np.absolute(np.mean(matrixNoDiagI))
        matrixNoDiagIAbs = np.abs(matrixNoDiagI)
        for j in range(i+1,m):
                matrixJ =normalizedMatrices[j]    
                upperJ = matrixJ[np.triu_indices(n,1)] #the problem is here
                lowerJ = matrixJ[np.tril_indices(n,-1)] 
                matrixNoDiagJ = np.append(upperJ,lowerJ)
                meanJ = np.absolute(np.mean(matrixNoDiagJ))
                matrixNoDiagJAbs = np.abs(matrixNoDiagJ)
                num = np.sum((matrixNoDiagIAbs - meanI)*(matrixNoDiagJAbs - 
                meanJ))
                np = (n*(n-1))-1 #n''
                den = np*SD[i]*SD[j]
                r = num/den
                R[i][j] = r
print(R)

我期望的是一个名为R的矩阵,其中包含算法所进行的计算。 这是

>>>R
>>>[[0. 0.456510 0.987845]
    [0. 0.156457 0.987845]
    [0. 0.       0.      ]]

我得到的错误是:

AttributeError                            Traceback (most recent call last)
 in ()
    204                 # print(i,j)
    205                 matrixJ =normalizedMatrices[j]
--> 206                 upperJ = matrixJ[np.triu_indices(n,1)] # Obtiene elementos diagonal superior
    207                 lowerJ = matrixJ[np.tril_indices(n,-1)] # Obtiene elementos diagonal superior
    208                 matrixNoDiagJ = np.append(upperJ,lowerJ)

AttributeError: 'int' object has no attribute 'triu_indices'

1 个答案:

答案 0 :(得分:0)

问题是您在该循环np中使用np = (n*(n-1))-1 #n''作为变量。您正在将其分配给一个int值,该值遮盖了导入的np。您需要重命名该变量。