迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

如何解决 Python 错误 IndexError: Arrays Used as Indices Must Be of Integer (Or Boolean) Type

作者:迹忆客 最近更新:2023/05/16 浏览次数:

在 Python 中使用 Numpy 数组时,您可能会遇到处理索引或类型问题的不同错误消息。 在这些错误类型中,IndexError:用作索引的数组必须是整数(或布尔)类型可能很棘手。

当我们面对 IndexError 错误信息时,我们使用了错误的 Type。 在这种情况下,我们应该使用整数或布尔值,但数组索引接收另一种数据类型(字符串或浮点数)。

在本文中,我们将解释在 Numpy 中处理数字时如何处理 IndexError: Arrays used as indices must be of integer (or boolean) type 错误消息。


使用 astype() 解决 IndexError: arrays used as indices must be of integer (or boolean) type

Numpy 仅适用于两种类型,Integer 或 Boolean。 因此,如果有一个它不理解的类型,它就会抛出一个错误。

让我们重新创建错误消息以更好地理解此错误消息。 要重新创建错误消息,我们需要生成两个 Numpy 数组,index 和 array,从 index 中提取值,并使用提取的值访问 array 中的值。

对于提取的值,我们将使用第一列值。

import numpy as np

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

indices = index[:, 0]
print(array[indices])

输出:

Traceback (most recent call last):
  File "c:\Users\akinl\Documents\Python\index.py", line 13, in <module>
    print(array[indices])
IndexError: arrays used as indices must be of integer (or boolean) type

IndexError: arrays used as indices must be of integer (or boolean) type 错误消息中,我们知道问题源于 print(array[indices]) 部分。

因为我们知道它在语法上是正确的,所以我们知道我们正在寻找的问题存在于我们解析到数组绑定的内容中。 这将我们带到了索引绑定。

从错误消息中我们知道,索引绑定中的元素可能不是整数或布尔值。 dtype 属性可用于检查索引中元素的类型。

print(indices.dtype)

输出:

float64

现在,这证实了我们面临的问题的原因。 我们传递给数组绑定索引的值是 float64 而不是布尔值。

为了解决这个问题,我们需要将 indices 中的值转换为 Integer 或 Boolean。 将它们转换为整数更有意义。

下次将它们转换为布尔值可能会有用。

astype() 方法有助于修改 Numpy 数组的 dtype 属性。 要修改索引绑定的数据类型,我们可以使用以下内容。

indices = index[:,0].astype(int)

如果我们使用 indices.dtype 表达式检查 dtype 属性,我们会得到以下结果。

int32

现在,我们的代码变成:

import numpy as np

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

indices = index[:, 0].astype(int)
print(array[indices])

输出:

[[1 2 3]
 [4 5 6]]

我们可以将索引的值转换为布尔值。 让我们试验一下。

为此,我们有一个带有两个布尔值的 Numpy 数组。

indices = index[:, 0].astype(bool)
print(indices)

输出:

[False  True]

索引绑定的值为 [0. 1.] ,当将 0 转换为布尔值时,它给出 False,而任何其他数字给出 True。 让我们一起运行一切。

import numpy as np

index = np.array([
    [0, 1, 2.1],
    [1, 2, 3.4]
])
array = np.array([
    [1, 3, 5],
    [7, 9, 11]
])

indices = index[:, 0].astype(bool)
print(array[indices])

输出:

[[ 7  9 11]]

那是因为它只处理真值。

因此,当我们遇到 IndexError: arrays used as indices must be of integer (or boolean) type 错误消息时,请知道某处有错误的数据类型。 跟踪您的代码,并转换必要的值。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便