Python 只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34952651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:45:23  来源:igfitidea点击:

only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

pythonpython-3.xnumpyfftdft

提问by Messit?zil

I am implementing fft as part of my homework. My problem lies in the implemention of shuffling data elements using bit reversal. I get the following warning:

我正在实施fft作为我作业的一部分。我的问题在于使用位反转实现改组数据元素。我收到以下警告:

DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future.

data[x], data[y] = data[y], data[x]

弃用警告:使用非整数而不是整数将导致将来出错。

数据[x],数据[y] = 数据[y],数据[x]

And the auto grading system (provided by university) returns the following:

自动评分系统(由大学提供)返回以下内容:

error: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices.

错误:只有整数、切片 ( :)、省略号 ( ...)、 numpy.newaxis ( None) 和整数或布尔数组是有效索引。

My code is:

我的代码是:

def shuffle_bit_reversed_order(data: np.ndarray) -> np.ndarray:
    """
    Shuffle elements of data using bit reversal of list index.

    Arguments:
    data: data to be transformed (shape=(n,), dtype='float64')

    Return:
    data: shuffled data array
    """

    # implement shuffling by reversing index bits

    size = data.size

    half = size/2;

    for x in range(size):
        xx = np.int(x)
        n = np.int(half)

        y = 0

        while n > 0:
            y += n * np.mod(xx,2)
            n /= 2
            xx = np.int(xx /2)

        if (y > x):

            data[x], data[y] = data[y], data[x]

    return data

I have already implemented the function for fft but it won't work until I get this shuffling function working. I think the problem is my data is of type 'float64' and I may have used it as an integer but I don't know how I can solve it.

我已经实现了 fft 的功能,但在我让这个改组功能工作之前它不会工作。我认为问题是我的数据是“float64”类型的,我可能将它用作整数,但我不知道如何解决它。

采纳答案by L. Hovan

I believe your problem is this: in your while loop, n is divided by 2, but never cast as an integer again, so it becomes a float at some point. It is then added onto y, which is then a float too, and that gives you the warning.

我相信你的问题是这样的:在你的 while 循环中,n 除以 2,但再也不会被转换为整数,所以它在某个时候变成了一个浮点数。然后将它添加到 y 上,然后它也是一个浮点数,这会给你警告。

回答by Sarvagya Gupta

You can use // instead of single /. That converts to intdirectly.

您可以使用 // 而不是单个 /。那int直接转换为。

回答by Vijay Meduri

put a intinfront of the all the voxelCoord's...Like this below :

int所有的东西放在voxelCoord前面……像下面这样:

patch = numpyImage [int(voxelCoord[0]),int(voxelCoord[1])- int(voxelWidth/2):int(voxelCoord[1])+int(voxelWidth/2),int(voxelCoord[2])-int(voxelWidth/2):int(voxelCoord[2])+int(voxelWidth/2)]