structured-array

numpy structured array sorting by multiple columns

时间秒杀一切 提交于 2021-01-29 15:20:14
问题 A minimal numpy structured array generator: import numpy as np index = np.arange(4) A = np.stack((np.sin(index), np.cos(index)),axis=1) B = np.eye(4).astype(int) C = np.array([1, 0, 1, 0], dtype=bool) goodies = [(a, b, c, d) for a, b, c, d in zip(index, A, B, C)] dt = [('index', 'int'), ('two_floats', 'float', 2), ('four_ints', 'int', 4), ('and_a_bool', 'bool')] s = np.array(goodies, dtype=dt) generates the minimal numpy structured array: array([(0, [ 0. , 1. ], [1, 0, 0, 0], True), (1, [ 0

Convert a slice of a structured array to regular NumPy array in NumPy 1.14

江枫思渺然 提交于 2020-01-14 03:14:08
问题 Note 1: None of the answers given to this question work in my case. Note 2: The solution must work in NumPy 1.14. Assume I have the following structured array: arr = np.array([(105.0, 34.0, 145.0, 217.0)], dtype=[('a', 'f4'), ('b', 'f4'), ('c', 'f4'), ('d', 'f4')]) . Now I'm slicing into the structured data type like so: arr2 = arr[['a', 'b']] And now I'm trying to convert that slice into a regular array: out = arr2[0].view((np.float32, 2)) which results in ValueError: Changing the dtype of a

No binary operators for structured arrays in Numpy?

南笙酒味 提交于 2020-01-11 02:11:07
问题 Okay, so after going through the tutorials on numpy's structured arrays I am able to create some simple examples: from numpy import array, ones names=['scalar', '1d-array', '2d-array'] formats=['float64', '(3,)float64', '(2,2)float64'] my_dtype = dict(names=names, formats=formats) struct_array1 = ones(1, dtype=my_dtype) struct_array2 = array([(42., [0., 1., 2.], [[5., 6.],[4., 3.]])], dtype=my_dtype) (My intended use case would have more than three entries and would use very long 1d-arrays.)

Truly recursive `tolist()` for NumPy structured arrays

情到浓时终转凉″ 提交于 2020-01-04 14:06:32
问题 From what I understand, the recommended way to convert a NumPy array into a native Python list is to use ndarray.tolist. Alas, this doesn't seem to work recursively when using structured arrays. Indeed, some ndarray objects are being referenced in the resulting list, unconverted: >>> dtype = numpy.dtype([('position', numpy.int32, 3)]) >>> values = [([1, 2, 3],)] >>> a = numpy.array(values, dtype=dtype) >>> a.tolist() [(array([1, 2, 3], dtype=int32),)] I did write a simple function to

Convert structured array with various numeric data types to regular array

随声附和 提交于 2019-12-30 07:07:13
问题 Suppose I have a NumPy structured array with various numeric datatypes. As a basic example, my_data = np.array( [(17, 182.1), (19, 175.6)], dtype='i2,f4') How can I cast this into a regular NumPy array of floats? From this answer, I know I could use np.array(my_data.tolist()) but apparently it is slow since you "convert an efficiently packed NumPy array to a regular Python list". 回答1: You can do it easily with Pandas: >>> import pandas as pd >>> pd.DataFrame(my_data).values array([[ 17. , 182

Is the mask of a structured array supposed to be structured itself?

老子叫甜甜 提交于 2019-12-24 18:07:50
问题 I was looking into numpy issue 2972 and several related problems. It turns out that all those problems are related to the situation where the array itself is structured, but its mask is not: In [38]: R = numpy.zeros(10, dtype=[("A", "<f2"), ("B", "<f4")]) In [39]: Rm = numpy.ma.masked_where(R["A"]<5, R) In [41]: Rm.dtype Out[41]: dtype([('A', '<f2'), ('B', '<f4')]) In [42]: Rm.mask.dtype Out[42]: dtype('bool') # Now, both `__getitem__` and `__repr__` will result in errors — see issue #2972 If

How to modify one column of a selected row from a numpy structured array

假装没事ソ 提交于 2019-12-24 17:19:35
问题 I am looking for an easy way to modify one field of a numpy structured array of a selected line of it. 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) b=a.copy() a[a['name']=='a']['x']=1 print a==b # return [ True True True] In this example, the a==b results should return [False True True] .Actually, I would like to selected the line of my array from the 'name' field and modify

Numpy structured arrays: string type not understood when specifying dtype with a dict

℡╲_俬逩灬. 提交于 2019-12-22 09:24:30
问题 Here's what happens if I initialize a struct array with the same field names and types in different ways: >>> a = np.zeros(2, dtype=[('x','int64'),('y','a')]) >>> a array([(0L, ''), (0L, '')], dtype=[('x', '<i8'), ('y', 'S')]) So initializing with list of tuples works fine. >>> mdtype = dict(names=['x','y'],formats=['int64','a']) >>> mdtype {'names': ['x', 'y'], 'formats': ['int64', 'a']} >>> a = np.zeros(2,dtype=mdtype) Traceback (most recent call last): File "<stdin>", line 1, in <module>

Constructing np.array with overlapping fields in dtype

不问归期 提交于 2019-12-13 16:53:31
问题 I have a dtype as follows: pose_dtype = np.dtype([('x', np.float64), ('y', np.float64), ('theta', np.float64)]) Right now, I can write: pose = np.array((1, 2, np.pi), dtype=pose_dtype) I'd like to add an xy field to make this easier to work with. I can do this with: pose_dtype = np.dtype(dict( names=['x', 'y', 'theta', 'xy'], formats=[np.float64, np.float64, np.float64, (np.float64, 2)], offsets=[0, 8, 16, 0] )) However, now I can no longer construct the array using my previous method, and

How do I fill multiple named fields using structured data

点点圈 提交于 2019-12-12 13:26:20
问题 I would like to take the information from some fields and just write them into another variable using a list. import numpy as np var1 = np.array([(1,2,3,4),(11,22,33,44),(111,222,333,444)], dtype=([('field1', 'int32'),('field2','int32'),('field3','int32'),('field4','int32')])) var2 = np.empty((1), dtype = ([('field1', 'int32'),('field2','int32'),('field5','int32'),('field6','int32')])) myList = ['field1', 'field2'] I want to write the values from the 1st and 2nd fields and 1st row to var2. I