只有整数,切片(`:`),省略号(`...
我正在实施FFT,当我使用位反转将数据元素洗牌时,我会收到以下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis
(`None`) and integer or boolean arrays are valid indices.
我的代码是:
def shuffle_bit_reversed_order(data: np.ndarray) -> np.ndarray:
x = data.size
n = x / 2
y = n * np.mod(x, 2)
data[x], data[y] = data[y], data[x]
return data
我认为问题是我的数据是类型的“ float64”,我可能已经将其用作整数,但我不喜欢知道我如何解决它。
I am implementing fft and when I shuffle the data elements using bit reversal, I get the following error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis
(`None`) and integer or boolean arrays are valid indices.
My code is:
def shuffle_bit_reversed_order(data: np.ndarray) -> np.ndarray:
x = data.size
n = x / 2
y = n * np.mod(x, 2)
data[x], data[y] = data[y], data[x]
return data
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您的问题是:在您的循环中,n除以2,但再也不会像整数一样施放,因此在某个时候它变成了浮动。然后将其添加到Y中,然后是float,这会给您警告。
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.
您可以使用//而不是单个 /。直接转换为
int
。You can use // instead of single /. That converts to
int
directly.在所有
voxelcoord
的s ...上放置一个int
...如下所示:put a
int
infront of the all thevoxelCoord
's...Like this below :正如消息所说,如果您尝试使用非刻板值(例如float或string)索引一个numpy数组,则会发生此错误。
除了使用浮子索引数组的情况外(或者,如果动态创建索引,只需将其施加到INT中,例如
arr [int(idx)]
),另一个很常见的情况是何时使用字符串来索引numpy数组。解决方案是将字符串施放到整数中,然后再将其用于索引数组。如果索引器来自呼叫
input()
,则默认情况下返回字符串,通常会发生这种情况。使用Scikit-Learn时,使用字符串索引也很普遍。
例如,一些常见的预处理功能以熊猫的数据范围为例,但在转换时返回numpy ndarrays,这使得使用标签选择列是不可能的。在这种情况下,解决方案要么将其用作NDARRAY,要么从转换的数据中创建PANDAS数据框架,然后选择该转换的数据的列。
As the message says, this error occurs if you try to index a numpy array using a non-integer value such as a float or a string.
Apart from the case where a float is used to index an array, e.g.
arr[0.]
, whose solution is to convert the float into an int likearr[0]
(or if the index is created dynamically, just cast it into an int likearr[int(idx)]
), another pretty common case is when a string is used to index a numpy array. The solution is to cast the string into an integer before using it to index an array.This commonly happens if the indexer comes from a call to
input()
which returns a string by default.Indexing using a string is also prevalent when using scikit-learn.
For example, some common preprocessing functions take pandas dataframes but return numpy ndarrays upon transformation, which makes it not possible to select columns using labels. In that case, a solution is either to use it as a ndarray or create a pandas dataframe from the transformed data and select columns of that transformed data.