Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向只有一行的numpy rec数组添加数据列_Python_Arrays_Numpy_Structured Array - Fatal编程技术网

Python 向只有一行的numpy rec数组添加数据列

Python 向只有一行的numpy rec数组添加数据列,python,arrays,numpy,structured-array,Python,Arrays,Numpy,Structured Array,我需要向numpy rec数组添加一列数据。我在这里看到了很多答案,但它们似乎不适用于只包含一行的rec数组 假设我有一个rec数组x: >>> x = np.rec.array([1, 2, 3]) >>> print(x) rec.array((1, 2, 3), dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8')]) 如果我尝试使用普通的append\u fields方法

我需要向numpy rec数组添加一列数据。我在这里看到了很多答案,但它们似乎不适用于只包含一行的rec数组

假设我有一个rec数组
x

>>> x = np.rec.array([1, 2, 3])
>>> print(x)
rec.array((1, 2, 3), 
      dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8')])
如果我尝试使用普通的
append\u fields
方法添加列

>>> np.lib.recfunctions.append_fields(x, 'f3', 4, dtypes='<i8', 
usemask=False, asrecarray=True)
结果表明,对于只有一行的rec数组,
len(x)
不起作用,而
x.size
起作用。如果我改用
np.hstack()
,我会得到
TypeError:invalid type promotion
,如果我尝试
np.c
,我会得到一个不想要的结果

>>> np.c_[x, 4]
array([[(1, 2, 3), (4, 4, 4)]], 
  dtype=(numpy.record, [('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8')]))
np.c_x[x,4] 数组([(1,2,3),(4,4,4)],
dtype=(numpy.record,[('f0','创建初始数组,使其具有形状(1,);注意额外的括号:

In [17]: x = np.rec.array([[1, 2, 3]])
(如果
x
是您无法控制的输入,您可以在
append\u fields()
中使用
x=np.至少\u 1d(x)

然后确保
append_fields
中给出的值也是长度为1的序列:

In [18]: np.lib.recfunctions.append_fields(x, 'f3', [4], dtypes='<i8', 
    ...: usemask=False, asrecarray=True)
Out[18]: 
rec.array([(1, 2, 3, 4)], 
          dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8')])

[18]中的
:np.lib.recfunctions.append_字段(x,'f3',[4],dtypes=”下面是一种在没有recfunctions的情况下执行作业的方法:

In [64]: x = np.rec.array((1, 2, 3))
In [65]: y=np.zeros(x.shape, dtype=x.dtype.descr+[('f3','<i4')])
In [66]: y
Out[66]: 
array((0, 0, 0, 0), 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4')])
In [67]: for name in x.dtype.names: y[name] = x[name]
In [68]: y['f3']=4
In [69]: y
Out[69]: 
array((1, 2, 3, 4), 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4')])
[64]中的
:x=np.rec.array((1,2,3))

在[65]中:y=np.zero(x.shape,dtype=x.dtype.descr+[('f3','问题不在于
x
的维度,而在于
4
rf.append\u字段(x,'f3',[4],usemack=False)
@hpaulj您真的尝试过这个吗?这没有什么区别。我仍然得到一个
类型错误
。如果numpy函数的源代码调用
len(x)
len(x),那么更改
4
就没有意义了
抛出一个错误,然后就这么简单。对不起,我的
x
(1,)
x=np.rec.array([(1,2,3)])
,我的总体印象是,
recfunctions
有缺陷,并且没有积极开发。我不止一次建议直接使用结构化数组。我同意@hpaulj;可能是一个缺陷,
append\u字段
不能使用“标量”重新数组(即具有形状的数组
()
).谢谢,
np.至少_1d()
成功了。我想这会在将来派上用场的。
In [18]: np.lib.recfunctions.append_fields(x, 'f3', [4], dtypes='<i8', 
    ...: usemask=False, asrecarray=True)
Out[18]: 
rec.array([(1, 2, 3, 4)], 
          dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8')])
In [64]: x = np.rec.array((1, 2, 3))
In [65]: y=np.zeros(x.shape, dtype=x.dtype.descr+[('f3','<i4')])
In [66]: y
Out[66]: 
array((0, 0, 0, 0), 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4')])
In [67]: for name in x.dtype.names: y[name] = x[name]
In [68]: y['f3']=4
In [69]: y
Out[69]: 
array((1, 2, 3, 4), 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4')])