问题描述
这就是问题:我有一个变量x
,它是一个numpy.ndarray
.此结构的大小为1000.如果我执行x[0]
,则得到4个数字的numpy.void
.如果我执行x[1]
,那么我会得到另一个numpy.void
,它也是4个数字,等等.
So here's the deal: I have variable x
which is a numpy.ndarray
. The size of this structure is 1000. If I do x[0]
, then I get a numpy.void
, of 4 numbers. If I do x[1]
, then I get another numpy.void
, also of 4 numbers, etc.
我只是想做的事情:我想对这个数据结构进行切片,以便提取大小为1000x3的numpy矩阵.
What I simply want to do: I want to slice this data structure, so that I extract a numpy matrix, of size 1000x3.
我该怎么做?谢谢
推荐答案
听起来像是具有结构化的数组,类似于以下简单示例:
Sounds like you have a structured array, something like this simple example:
In [158]: x = np.ones((5,), dtype='i,i,f,f')
In [159]: x
Out[159]:
array([(1, 1, 1., 1.), (1, 1, 1., 1.), (1, 1, 1., 1.),
(1, 1, 1., 1.), (1, 1, 1., 1.)],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f4'), ('f3', '<f4')])
In [160]: x[0]
Out[160]: (1, 1, 1., 1.)
In [161]: type(x[0])
Out[161]: numpy.void
x[0]
是一条记录,显示为元组.您可以按名称访问字段(而不是按列"索引访问):
x[0]
is a record, displayed as a tuple. You access fields by name (not by 'column' index):
In [162]: x['f0']
Out[162]: array([1, 1, 1, 1, 1], dtype=int32)
In [163]: x['f2'] = np.arange(5)
In [165]: x['f1'] = [10,12,8,0,3]
In [166]: x
Out[166]:
array([(1, 10, 0., 1.), (1, 12, 1., 1.), (1, 8, 2., 1.),
(1, 0, 3., 1.), (1, 3, 4., 1.)],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f4'), ('f3', '<f4')])
In [168]: x[['f2','f3']] # 2 fields at once
Out[168]:
array([( 0., 1.), ( 1., 1.), ( 2., 1.), ( 3., 1.), ( 4., 1.)],
dtype=[('f2', '<f4'), ('f3', '<f4')])
当列"应包含不同的内容(例如,一个字符串包含一个字符串,另一个包含整数)时,这非常方便.但是将这样的数组转换为相同数字类型的2d数组可能很尴尬.
This is handy when 'columns' should contain different things, for example strings in one, integers in another. But it can be awkward to convert such an array to a 2d array of the same numeric type.
view
和astype
在有限的情况下有效,但是tolist
是我所知道的最可靠的转换媒介.
view
and astype
work in limited cases, but tolist
is the most robust conversion medium that I'm aware of.
In [179]: x.tolist()
Out[179]:
[(1, 10, 0.0, 1.0),
(1, 12, 1.0, 1.0),
(1, 8, 2.0, 1.0),
(1, 0, 3.0, 1.0),
(1, 3, 4.0, 1.0)]
In [180]: np.array(x.tolist())
Out[180]:
array([[ 1., 10., 0., 1.],
[ 1., 12., 1., 1.],
[ 1., 8., 2., 1.],
[ 1., 0., 3., 1.],
[ 1., 3., 4., 1.]])
请注意,结构化数组的tolist
是元组的列表,而二维数组的tolist
是列表的列表.朝这个方向发展,差异无所谓.相反,差异很重要.
Note that the tolist
for the structured array is a list of tuples, whereas tolist
for a 2d array is a list of lists. Going this direction that difference doesn't matter. Going the other way the difference matters.
您是如何生成此数组的?是来自csv
和genfromtxt
吗?作为其他数字程序包的输出吗?
How did you generate this array? From a csv
with genfromtxt
? As output from some other numeric package?
这篇关于如何切片由numpy.void数字组成的numpy.ndarray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!