Python 带有dtype float的Numpy n元组数组

Python 带有dtype float的Numpy n元组数组,python,arrays,numpy,floating-point,tuples,Python,Arrays,Numpy,Floating Point,Tuples,我需要一个表达式,它将给我一个8元组float数组。目前,我通过以下途径获得了8元组数组: E = np.zeros((n,m), dtype='8i') #8-tuple 但是,当我通过以下方式附加索引时: E[i,j][0] = 1000.2 #etc. 我得到了一个dtypeint的元组数组: [1000 0 0 0 0 0 0 0] 似乎我需要在zeros命令中使用dtype来设置n元组和float值。有人知道这是怎么做到的吗 E=np.zero((n,

我需要一个表达式,它将给我一个8元组
float
数组。目前,我通过以下途径获得了8元组数组:

E = np.zeros((n,m), dtype='8i') #8-tuple
但是,当我通过以下方式附加索引时:

E[i,j][0] = 1000.2 #etc.
我得到了一个dtype
int
的元组数组:

[1000   0   0   0   0   0   0   0]
似乎我需要在
zeros
命令中使用
dtype
来设置n元组和
float
值。有人知道这是怎么做到的吗

E=np.zero((n,m),dtype='8f

试试:


E=np.zero((n,m),dtype='8f')#8元组
如果数组是整数
dtype
,则分配的值将被截断:

In [169]: x=np.array([0,1,2])
In [170]: x
Out[170]: array([0, 1, 2])
In [173]: x[0] = 1.234
In [174]: x
Out[174]: array([1, 1, 2])
数组必须具有浮点数据类型才能保存浮点值

只需将
i
(整数)更改为
f
(浮点)即可生成浮点数组:

In [166]: E = np.zeros((2,3), dtype='8f')
In [167]: E.shape
Out[167]: (2, 3, 8)
In [168]: E.dtype
Out[168]: dtype('float32')
此“8f”数据类型不常见。字符串实际上转换为:

In [175]: np.dtype('8f')
Out[175]: dtype(('<f4', (8,)))

您对“n-tuple”的用法不清楚。虽然
shape
是一个元组,但数字数组不使用元组表示法。这是为结构化阵列保留的

In [180]: np.zeros((3,), dtype='f,f,f,f')
Out[180]: 
array([(0., 0., 0., 0.), (0., 0., 0., 0.), (0., 0., 0., 0.)],
      dtype=[('f0', '<f4'), ('f1', '<f4'), ('f2', '<f4'), ('f3', '<f4')])
In [181]: _.shape
Out[181]: (3,)
也可以在字段中放置“数组”:

In [183]: np.zeros((3,), dtype=[('f0','f',(4,))])
Out[183]: 
array([([0., 0., 0., 0.],), ([0., 0., 0., 0.],), ([0., 0., 0., 0.],)],
      dtype=[('f0', '<f4', (4,))])
In [184]: _['f0']
Out[184]: 
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]], dtype=float32)
dtype
符号可能会令人困惑


除非您有意创建结构化数组,否则最好不要使用“8f”符号

In [189]: np.array([0,1,2,3],dtype='4i')
TypeError: object of type 'int' has no len()

In [190]: np.array([[0,1,2,3]],dtype='4i')
TypeError: object of type 'int' has no len()

In [191]: np.array([(0,1,2,3)],dtype='4i')     # requires [(...)]
Out[191]: array([[0, 1, 2, 3]], dtype=int32)
如果没有4,我可以简单地写:

In [193]: np.array([[0,1,2,3]], dtype='i')
Out[193]: array([[0, 1, 2, 3]], dtype=int32)
In [194]: np.array([0,1,2,3], dtype='i')
Out[194]: array([0, 1, 2, 3], dtype=int32)
In [195]: np.array([[0,1,2,3]])
Out[195]: array([[0, 1, 2, 3]])

我错过什么了吗?为什么不直接做
E=np.zeros((n,m,8),dtype=float)
?@FHTMitchell,它实际上在我的窗口中产生了更干净的输出!数据似乎相同;有什么区别吗?我不这么认为,但我从来没有像你那样使用过它。您在哪里找到
dtype='8i'
语法?我在
numpy
文档中找不到它,我怀疑这是一个遗留问题。
E.shape
E.dtype
是什么?它是3d数组还是带有复合数据类型的2d?在Python中,元组标记为
()
,而不是
[]
numpy
使用类似列表的表示法,除非显示
结构化数组的元素。你想要一个3D数组还是一个2D结构数组?考虑向你的代码添加一个解释,为什么它比OPS的代码更好/不同?如果可能的话也显示输出。哇,这里的描述太棒了。非常感谢@hpaulj
In [185]: np.zeros((3,), dtype='4f,i')
Out[185]: 
array([([0., 0., 0., 0.], 0), ([0., 0., 0., 0.], 0),
       ([0., 0., 0., 0.], 0)], dtype=[('f0', '<f4', (4,)), ('f1', '<i4')])
In [189]: np.array([0,1,2,3],dtype='4i')
TypeError: object of type 'int' has no len()

In [190]: np.array([[0,1,2,3]],dtype='4i')
TypeError: object of type 'int' has no len()

In [191]: np.array([(0,1,2,3)],dtype='4i')     # requires [(...)]
Out[191]: array([[0, 1, 2, 3]], dtype=int32)
In [193]: np.array([[0,1,2,3]], dtype='i')
Out[193]: array([[0, 1, 2, 3]], dtype=int32)
In [194]: np.array([0,1,2,3], dtype='i')
Out[194]: array([0, 1, 2, 3], dtype=int32)
In [195]: np.array([[0,1,2,3]])
Out[195]: array([[0, 1, 2, 3]])