IndexError:进行排列时数组的索引太多

时间:2015-09-23 09:23:01

标签: python arrays numpy permutation

我正在尝试对给定数据进行排列和二值化,目前我的输入数据是' x'是3 * 3阵列。

即使存在许多线程,我也不知道如何解决此错误。

有人可以建议我如何解决此错误吗?

代码:

X = numpy.array([[1,2,3],[4,5,6],[7,8,9]])

step  = 1.0
noise = 1.0
triuind = (numpy.arange(3)[:,numpy.newaxis] <= numpy.arange(3)[numpy.newaxis,:]).flatten()

maxm = 0
for _ in range(10): maxm = numpy.maximum(maxm,realize(X).max(axis=0))
print("max:",maxm.shape)

R = transformedData(X)

def transformedData(traindata):
    G = normalize(expand(realize(traindata)))
    return G

def realize(X):
    def _realize_(x):
        inds = numpy.argsort(-(x**2).sum(axis=0)**.5+numpy.random.normal(0,noise,x[0].shape))
        x = x[inds,:][:,inds]*1
        x = x.flatten()[triuind]
        return x
    return numpy.array([_realize_(z) for z in X])

def expand(X):
    Xexp = []
    for i in range(X.shape[1]):
        for k in numpy.arange(0,maxm[i]+step,step):
            Xexp += [numpy.tanh((X[:,i]-k)/step)]
    return numpy.array(Xexp).T

def normalize(X):
    mean = X.mean(axis=0)
    std = (X - mean).std()
    return (X - mean)/std

错误:

IndexError                                Traceback (most recent call last)
<ipython-input-40-09eced555cec> in <module>()
      1 maxm = 0
----> 2 for _ in range(10): max = numpy.maximum(maxm,realize(X).max(axis=0))
      3 print("max:",maxm.shape)

<ipython-input-35-d182ddcf0bea> in realize(X)
      9         x = x.flatten()[triuind]
     10         return x
---> 11     return numpy.array([_realize_(z) for z in X])
     12 
     13 def expand(X):

<ipython-input-35-d182ddcf0bea> in _realize_(x)
      6     def _realize_(x):
      7         inds = numpy.argsort(-(x**2).sum(axis=0)**.5+numpy.random.normal(0,noise,x[0].shape))
----> 8         x = x[inds,:][:,inds]*1
      9         x = x.flatten()[triuind]
     10         return x

IndexError: too many indices for array

1 个答案:

答案 0 :(得分:0)

下面:

def realize(X):
    def _realize_(x):
        inds = numpy.argsort(-(x**2).sum(axis=0)**.5+numpy.random.normal(0,noise,x[0].shape))
        print(x)
        x = x[inds,:][:,inds]*1
        x = x.flatten()[triuind]
        return x
    return numpy.array([_realize_(z) for z in X])
<{1>} _realize_您输入的是1d数组,因此x = x[inds,:][:,inds]*1会引发IndexError