8

I would simply like to create a numpy array of size(N,m) that has just the first column made of integer, and the rest by default float. So that, if initialized to zero it should be results:

array([[ 0,  0.,  0.,  0.,  0.],
       [ 0,  0.,  0.,  0.,  0.],
       [ 0,  0.,  0.,  0.,  0.],
       [ 0,  0.,  0.,  0.,  0.],
       [ 0,  0.,  0.,  0.,  0.]])

All the attempts I have made return me some tuple sub-elements when trying to create such a structured array.

3
  • Out of curiosity, why would you need to that? Commented Oct 22, 2016 at 11:32
  • imagine you have to store some mixed data, where 1 field is integer (i.e. a discretized state), and the others are real. Commented Oct 24, 2016 at 10:17
  • 1
    But what are you doing with this data? When is 1 != 1.0 problematic? Commented Oct 24, 2016 at 21:01

3 Answers 3

18

You could use an array with dtype = object:

>>> arr = np.ndarray((10,4),dtype = object)
>>> arr[:,0] = int(10)
>>> arr[:,1:] = float(10)
>>> arr
array([[10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0],
       [10, 10.0, 10.0, 10.0]], dtype=object)

Notice that you get the right behavior when doing arithmetic.

>>> arr / 3
array([[3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333],
       [3, 3.33333333333, 3.33333333333, 3.33333333333]], dtype=object)

Or you could use a numpy.recarray:

>>> import numpy as np
>>> arr = np.recarray(10,dtype=[('x',int),('y',float,4)])
>>> arr[:] = 0
>>> arr
rec.array([(0, array([ 0.,  0.,  0.,  0.])), (0, array([ 0.,  0.,  0.,  0.])),
           (0, array([ 0.,  0.,  0.,  0.])), (0, array([ 0.,  0.,  0.,  0.])),
           (0, array([ 0.,  0.,  0.,  0.])), (0, array([ 0.,  0.,  0.,  0.])),
           (0, array([ 0.,  0.,  0.,  0.])), (0, array([ 0.,  0.,  0.,  0.])),
           (0, array([ 0.,  0.,  0.,  0.])), (0, array([ 0.,  0.,  0.,  0.]))], 
  dtype=[('x', '<i4'), ('y', '<f8', (4,))])
>>> arr['x']
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> arr['y']
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

If you need to do arithmetic on all the values, you'll have to perform the operation on each field seperately, e.g.

>>> arr['x'] += 2
>>> arr['y'] += 2
Sign up to request clarification or add additional context in comments.

1 Comment

Although note that dtype=object makes the array as slow (and memory-intensive) as normal Python array.
2

Although I can think of lots of reasons why you shouldn't be wanting to do this in the first place, it's not for me to judge, and I hate when people try to diminish the value of my own quick'n'dirty hacks.

The rationale is to use dtype=object. Since everything in Python is an object, you can mixed numeric types while still preserving homogeneity inside an array. I suggest the following, but you can obviously adapt to your needs:

import numpy

rows = 5
a = numpy.zeros((rows,5)).astype(object)
a[:,0] = a[:,0].astype(int)
print a

[[0  0.0  0.0  0.0  0.0]
 [0  0.0  0.0  0.0  0.0]
 [0  0.0  0.0  0.0  0.0]
 [0  0.0  0.0  0.0  0.0]
 [0  0.0  0.0  0.0  0.0]]

Comments

1

Read this in the numpy documentation, which indicates all the members have to be of same type

NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.

Comments

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.