2 回答

TA贡献1810条经验 获得超5个赞
Numpy 正在删除第二个示例中的单例维度。如果需要,您可以保留形状并使用以下内容获得第一个示例的等效项。
a[:, [2]] # get [[3],[9]]

TA贡献2003条经验 获得超2个赞
我认为您1D以稍微不明确的方式使用术语数组。
第一个选项返回形状为 (2, 1) 的数组,另一个选项返回形状为 (2, ) 的类似列表的数组。
它们都是“数学上”一维的,但它们具有不同的麻木形状。
所以你的目标是获得一个更像矩阵的数组,形状为 (2, 1),这是通过在所有维度上切片所需的索引来完成的,而不是选择特定的索引。
这是一个更直观的案例,可以通过在两个维度上进行切片或选择来将事情发挥到极致:
import numpy as np
a = np.array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]])
specific_index_value = a[0, 0]
print(specific_index_value)
print(type(specific_index_value))
print(str(specific_index_value) + ' is a scalar, not a 1X1 matrix')
>> 1
>> <class 'numpy.int32'>
>> 1 is a scalar, not a 1X1 matrix
sliced_index_value = a[:1, :1]
print(sliced_index_value)
print(type(sliced_index_value))
print(str(sliced_index_value) + ' is a matrix, with shape {}'.format(sliced_index_value.shape))
>> [[1]]
>> <class 'numpy.ndarray'>
>> [[1]] is a matrix, with shape (1, 1)
那有意义吗?祝你好运!
添加回答
举报