3

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?

5
  • 1
    Am I missing something? Why not just do E = np.zeros((n, m, 8), dtype=float)? Commented Jan 23, 2018 at 13:12
  • @FHTMitchell that actually produces a much cleaner output in my window! The data appears the same; is there any difference? Commented Jan 23, 2018 at 14:11
  • I don't think so but then I've never used it the way you do. Where did you find the dtype='8i' syntax? I can't find it in the numpy documentation, I suspect it's a legacy thing. Commented Jan 23, 2018 at 16:09
  • What's the E.shape and E.dtype? Is it a 3d array, or 2d with compound dtype? Commented Jan 23, 2018 at 17:37
  • In Python a tuple is marked with (), not []. numpy uses a list like notation except when display elements of a structured array. Do you want a 3d array or a 2d structured array? Commented Jan 23, 2018 at 17:45

3 Answers 3

1

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Whoa, amazing description here. Thanks a ton @hpaulj
1

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

Comments

0

Try:

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

1 Comment

consider adding an explanation to your code, and why is it better/different than the OPs' code. Also show output if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.