How to (simply) build an integer and a floating point array

I just wanted to create a numpy array of size (N, m) that has only the first column of an integer and the rest by default is a float. Thus, when initializing to zero, these should be the 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 made return me some sub-elements of the tuple when trying to create such a structured array.

+6
source share
3 answers

You can 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) 

Note that when doing arithmetic you get the correct behavior.

 >>> 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 can use 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 perform arithmetic in all values, you will have to perform the operation on each field separately, for example.

 >>> arr['x'] += 2 >>> arr['y'] += 2 
+13
source

Although I can think of many reasons why you should not want to do this in the first place, I should not be judged, and I hate it when people try to reduce the value of my own β€œfast”, dirty hacks.

The rationale is to use dtype=object . Since everything in Python is an object, you can combine numeric types while maintaining uniformity within the 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]] 
+2
source

Read this in the numpy documentation, which indicates that all members must be of the same type

The main NumPy object is a homogeneous multidimensional array. This is a table of elements (usually numbers), all the same, indexed by a tuple of integer positive numbers.

0
source

All Articles