【发布时间】:2020-05-08 23:27:16
【问题描述】:
所以我真的放弃了。我想用shape(10000000,3) 预先分配一个巨大的2d-numpy 数组,每列有一个特定的dtype。
例子:
a b c
-------- --------- --------
uint32 float32 uint8
------ ------ ------
90 2.43 4
100 2.42 2
123 2.33 1
所以from the docs 我可以像这样创建一个二维数组:
arr = np.zeros((4,3))
arr
Out[6]:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
到目前为止还不错,但是 dtypes 呢?
In [16]: arr.dtype
Out[16]: dtype('float64')
所有浮点数 - 所以让我们定义 dtype:
dtype_L1 = np.dtype({'names': ['a', 'b', 'c'],
'formats': [np.uint32, np.float32, np.uint8]})
并比较两者:
In [25]: arr_dtype = np.zeros((4,3), dtype=dtype_L1)
In [26]: arr = np.zeros((4,3))
In [27]: arr[0,0]
Out[27]: 0.0
In [28]: arr_dtype[0,0]
Out[28]: (0, 0., 0)
In [29]: type(arr_dtype[0,0])
Out[29]: numpy.void
In [30]: type(arr[0,0])
Out[30]: numpy.float64
In [31]: arr.shape
Out[31]: (4, 3)
In [32]: arr_dtype.shape
Out[32]: (4, 3)
所以-我不明白为什么arr_dtype 与arr 不同,只是每列其他dtype。有人可以指导一个方向吗?看起来我正在创建一个维度太高的数组..:
**更新:一维太深..? **
>>> arr[0,0]
0 ## Correct
>>> arr_dtype[0,0]
(0, 0., 0)
它真的在这里保存了 dtyped 数组?!深入一维:
>>> type(arr_dtype[0,0][0])
<class 'numpy.uint32'>
>>> type(arr_dtype[0,0][1])
<class 'numpy.float32'>
>>> type(arr_dtype[0,0][2])
<class 'numpy.uint8'>
# all good - But one level too deep.
- 预期:
numpy正在建立一个 4x3 矩阵,其中 每个元素 是一个数字。 12 个数字完全正确。 - 观察到:
numpy正在建立一个 4x3 矩阵,其中 每个元素 是一个shape (3,)结构。所以我有 4x3x3 字段 = 36 个数字。
那么是否可以通过其他方式申请dtype?
最终解决方案
您基本上需要决定什么更重要:节省空间还是将所有数据集中到一个array?一个数组只能有一个 dtype。因此,如果您需要不同的数据类型,请选择 Y 轴长度相同的多个数组。否则,像arr_dtype = np.zeros((4,3), dtype=np.float32) 一样创建它,并确保将dtype 设置为每个数组的正确类型。感谢 cmets!
【问题讨论】:
-
arr_dtype和arr具有不同的形状和数据类型。一个的字段与另一个的列不同。只有复合数据类型允许混合数据类型。 -
对不起@hpaulj,但你的评论并没有帮助我前进。我想要一个简单的数组:3 列,4 行。最零列的类型为
unit32,第一列为float32,第二列为unit8。我认为如果我能看到如何做到这一点会更清楚。 -
你不能有一个“简单”的数组,每列都有不同的数据类型。
-
@hpaulj 好的...那么我怎样才能使用三种不同的按列 dtypes 获得一些
structured array呢?我仍然试图弄清楚为什么代码是错误的(根据你的评论)。所以从one of many examples看来,这里应用的dtype属性是正确的?很高兴得到建议。 -
dt = np.dtype(...); arr = np.zeros((2000,), dtype=dt)生成结构化数组。arr=np.zeros((2000,3), dtype=float)制作二维浮点数组。当一个或多个列是 string dtype 和/或 float 和 int 的混合时,结构化数组最有意义。它实际上只是创建 3 个单独的数组的替代方法,每个数组都有自己的 dtype。您无法跨字段进行数学运算,因此使用复合 dtype 几乎没有计算优势。
标签: python-3.x numpy multidimensional-array numpy-ndarray