只有整数,切片(`:`),省略号(`...

发布于 2025-02-03 10:37:40 字数 552 浏览 5 评论 0原文

我正在实施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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思慕 2025-02-10 10:37:40

我相信您的问题是:在您的循环中,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.

我纯我任性 2025-02-10 10:37:40

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

You can use // instead of single /. That converts to int directly.

时光是把杀猪刀 2025-02-10 10:37:40

在所有voxelcoord的s ...上放置一个int ...如下所示:

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)]

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

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)]
梦过后 2025-02-10 10:37:40

正如消息所说,如果您尝试使用非刻板值(例如float或string)索引一个numpy数组,则会发生此错误。

除了使用浮子索引数组的情况外(或者,如果动态创建索引,只需将其施加到INT中,例如arr [int(idx)]),另一个很常见的情况是何时使用字符串来索引numpy数组。解决方案是将字符串施放到整数中,然后再将其用于索引数组。

arr = np.array([1,2,4])

arr["1"]                  # <--- IndexError

arr[int("1")]             # <--- OK; cast to int, then index

如果索引器来自呼叫input(),则默认情况下返回字符串,通常会发生这种情况。

i = input()               # <--- `i` is a string
arr[i]                    # <--- IndexError

arr[int(i)]               # <--- cast to int, then index

使用Scikit-Learn时,使用字符串索引也很普遍。

例如,一些常见的预处理功能以熊猫的数据范围为例,但在转换时返回numpy ndarrays,这使得使用标签选择列是不可能的。在这种情况下,解决方案要么将其用作NDARRAY,要么从转换的数据中创建PANDAS数据框架,然后选择该转换的数据的列。

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame({'A': [1, 2, 3], 'B': [10, 20, 30]})
sc = MinMaxScaler()
df = sc.fit_transform(df)

df['A']        # <--- IndexError
df[:, 0]       # <--- OK

# explicitly create a pandas dataframe from transformed data
df1 = pd.DataFrame(sc.fit_transform(df), columns=df.columns)
df1['A']       # <--- OK

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 like arr[0] (or if the index is created dynamically, just cast it into an int like arr[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.

arr = np.array([1,2,4])

arr["1"]                  # <--- IndexError

arr[int("1")]             # <--- OK; cast to int, then index

This commonly happens if the indexer comes from a call to input() which returns a string by default.

i = input()               # <--- `i` is a string
arr[i]                    # <--- IndexError

arr[int(i)]               # <--- cast to int, then index

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.

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame({'A': [1, 2, 3], 'B': [10, 20, 30]})
sc = MinMaxScaler()
df = sc.fit_transform(df)

df['A']        # <--- IndexError
df[:, 0]       # <--- OK

# explicitly create a pandas dataframe from transformed data
df1 = pd.DataFrame(sc.fit_transform(df), columns=df.columns)
df1['A']       # <--- OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文