【问题标题】:Is it possible not to be able to index axis 1 of a 2-D array?是否可能无法索引二维数组的轴 1?
【发布时间】:2021-09-18 20:00:16
【问题描述】:

看看这个

arr = np.array([1, 2, 3, 4, 5], ndmin=2)
print(arr.ndim)
print(arr.shape)

输出

>>>2
>>>(1, 5)

很明显维数是2 但是为什么当我尝试在轴 1 中索引元素时它不起作用,即第二维

print(arr[1, 0]) # First element in second dimension
# Traceback
IndexError: index 1 is out of bounds for axis 0 with size 1

【问题讨论】:

    标签: arrays python-3.x numpy-ndarray


    【解决方案1】:

    考虑不同的形状

    arr = np.array([1, 2, 3, 4, 5])[:, None]
    arr.ndim
    2
    arr.shape
    (5, 1)
    print(arr[1, 0])
    2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2023-03-07
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2018-06-25
      • 2020-07-30
      相关资源
      最近更新 更多