structured-array

Access line by line to a numpy structured array

℡╲_俬逩灬. 提交于 2019-12-12 03:44:52
问题 I am trying to access to a structured array line by line by iterating on the values of one field of it but even if the value iterate well, the slice of the array doesn't change. Here is my SWE : import numpy as np dt=np.dtype([('name',np.unicode,80),('x',np.float),('y',np.float)]) a=np.array( [('a',0.,0.),('b',0.,0.),('c',0.,0.) ],dtype=dt) for n in a['name']: print n,a['name'==n] gives me : a (u'a', 0.0, 0.0) b (u'a', 0.0, 0.0) c (u'a', 0.0, 0.0) At each iteration, I always have the same

How to build a numpy structured array with 2 int columns and 3 float columns?

China☆狼群 提交于 2019-12-11 14:52:46
问题 I have data which is a list of 5-tuples. The first two are integer indices i, j and the next three are floats xyz . data = [(1, 2, 3.141, 1.414, 2.718), (3, 4, 1.111, 2.222, 3.333), (0, 0, 0.000, 0.000, 0.000)] I have heard that I can do something like dt = [('ij', 'int', 2), ('xyz', 'float', 3)] struct_array = np.array(data, dtype=dt) so I can access the last three columns of the array as a 2D float array. For example to get r = sqrt(x^2 + y^2 + z^2) I should be able to say r = np.sqrt((

Filter numpy structured array based on multiple values

不羁的心 提交于 2019-12-11 12:23:57
问题 I have a numpy structured array. : myArray = np.array([(1, 1, 1, u'Zone3', 9.223), (2, 1, 0, u'Zone2', 17.589), (3, 1, 1, u'Zone2', 26.95), (4, 0, 1, u'Zone1', 19.367), (5, 1, 1, u'Zone1', 4.395)], dtype=[('ID', '<i4'), ('Flag1', '<i4'), ('Flag2', '<i4'), ('ZoneName', '<U5'), ('Value', '<f8')]) I would like to sum the values from the "Value" column when multiple criteria are met. If I want Flag1 and Flag2 to ==1 i can use: sumResult = (sum(myArray[((myArray["Flag1"] == 1) & (myArray["Flag2"]

ndarray to structured_array and float to int

有些话、适合烂在心里 提交于 2019-12-11 08:35:23
问题 The problem I encounter is that, by using ndarray.view(np.dtype) to get a structured array from a classic ndarray seems to miscompute the float to int conversion. Example talks better: In [12]: B Out[12]: array([[ 1.00000000e+00, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.43600000e+01, 0.00000000e+00], [ 1.00000000e+00, 2.00000000e+00, 7.10000000e+00, 1.10000000e+00, 4.43600000e+01, 1.32110000e+02], [ 1.00000000e+00, 3.00000000e+00, 9.70000000e+00, 2.10000000e+00, 4.43600000e+01, 2

Memory-friendly way to add a field to a structured ndarray — without duplicating data?

为君一笑 提交于 2019-12-11 05:47:49
问题 To add a field to a structured numpy array, it is quite simply to create a new array with a new dtype, copy over the old fields, and add the new field. However, I need to do this for an array that takes a lot of memory, and I would rather not duplicate all of it. Both my own implementation and the (slow) implementation in numpy.lib.recfunctions.append_fields duplicate memory. Is there a way to add a field to a structured ndarray , without duplicating memory? That means, either a way that

NumPy recfunctions join_by TypeError

送分小仙女□ 提交于 2019-12-11 01:22:46
问题 I encounter a TypeError when I attempt to join a 'uint16' field to a structured array in NumPy 1.11 or 1.12 (Python 3.5). import numpy as np from numpy.lib import recfunctions as rfn foo = np.array([(1,)], dtype=[('key', int)]) bar = np.array([(1,np.array([1,2,3]))], dtype=[('key', int), ('value', 'uint16', 3)]) rfn.join_by('key', foo, bar) This is the error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/user/anaconda3/lib/python3.5/site-packages/numpy/lib

TypeError when appending fields to a structured array of size ONE

家住魔仙堡 提交于 2019-12-10 18:28:45
问题 I'm getting a run-time error when trying to append field(s) to a structured array of size ONE. I've written a simple example below: import numpy as np import numpy.lib.recfunctions as rcfuncs dtype_ = np.dtype( { 'names': ["field_a","field_b","field_c"] , 'formats': ['S32', 'i4', 'f8']} ) data_ = [("1",17, 123.45)] numpy_array = np.array(data_, dtype_) # append 2 fields numpy_array = rcfuncs.append_fields( numpy_array,["field_d","field_e"],data=[ "1","3" ] ) # append 1 field fails :( numpy

Add and access object-type field of a numpy structured array

旧街凉风 提交于 2019-12-08 02:34:25
问题 I am using numpy 1.16.2. In brief, I am wondering how to add an object-type field to a structured array. The standard way via the recfunctions module throws an error and I suppose there is a reason for this. Therefore, I wonder whether there is anything wrong with my workaround. Furthermore, I would like to understand why this workaround is necessary and whether I need to use extra caution when accessing the newly created array. Now here come the details: I have a numpy structured array:

How can I mask elements of a record array in Numpy?

倖福魔咒の 提交于 2019-12-07 02:01:10
问题 I understand how to create a masked array, and I would like to use masking in a record array so that I can access this data using named attributes. The masking seems to be "lost" when I create a record array from a masked array: >>> data = np.ma.array(np.ma.zeros(30, dtype=[('date', '|O4'), ('price', '<f8')]),mask=[i<10 for i in range(30)]) >>> data masked_array(data = [(--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (--, --) (0, 0.0) (0, 0.0) (0, 0.0) (0, 0.0

numpy: how to fill multiple fields in a structured array at once

半城伤御伤魂 提交于 2019-12-07 01:42:22
问题 Very simple question: I have a structured array with multiple columns and I'd like to fill only some of them (but more than one) with another preexisting array. This is what I'm trying: strc = np.zeros(4, dtype=[('x', int), ('y', int), ('z', int)]) x = np.array([2, 3]) strc[['x', 'y']][0] = x This gives me this future warning: main :1: FutureWarning: Numpy has detected that you (may be) writing to an array returned by numpy.diagonal or by selecting multiple fields in a record array. This code