Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy n-tuple array with dtype float

I need an expression that will grant me an 8-tuple float array. Currently, I have the 8-tuple array via:

E = np.zeros((n,m), dtype='8i') #8-tuple

However, when I append an indices i,j via:

E[i,j][0] = 1000.2 #etc.

I get back a tuple array with dtype int:

[1000   0   0   0   0   0   0   0]

It appears I need a way of using the dtype within my zeros command to both set the n-tuple and the float value. Does anyone know how this is done?

like image 687
evambivalence Avatar asked Jul 12 '26 09:07

evambivalence


2 Answers

If an array is integer dtype, then assigned values will be truncated:

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])

The array has to have a float dtype to hold float values.

Simply changing the i (integer) to f (float) produces a float array:

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')

This '8f' dtype is not common. The string actually translates to:

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

But when used in np.zeros that 8 is treated as a dimension. Usually we specify all dimensions in the shape, as @FHTMitchell notes:

In [176]: E1 = np.zeros((2,3,8), dtype=np.float32)
In [177]: E1.shape
Out[177]: (2, 3, 8)
In [178]: E1.dtype
Out[178]: dtype('float32')

Your use of 'n-tuple' is unclear. While shape is a tuple, numeric arrays don't use tuple notation. That is reserved for structured arrays.

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,)

This is a 1d array with 3 elements. The dtype shows 4 fields. Each element, or record, is displayed as a tuple.

But fields are indexed by name, not number:

In [182]: Out[180]['f1']
Out[182]: array([0., 0., 0.], dtype=float32)

It is also possible to put 'arrays' within fields:

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)

Initially I thought the 8f notation would produce this sort of array. But apparently I have to either use the full notation with field name, or make a comma separated string:

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')])

dtype notation can be confusing, https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html


Unless you are intentionally trying to create a structured array, it is best to stay away from the '8f' notation.

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)

Without the 4, I can simply write:

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]])
like image 124
hpaulj Avatar answered Jul 13 '26 22:07

hpaulj


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

like image 33
skullgoblet1089 Avatar answered Jul 13 '26 23:07

skullgoblet1089



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!