structured-array

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

纵然是瞬间 提交于 2019-12-06 04:24:18
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: import numpy as np a = np.zeros(3, dtype={'names':['A','B','C'], 'formats':['int','int','float']}) for i in

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

痴心易碎 提交于 2019-12-05 19:54:50
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> TypeError: data type not understood So initializing with a dict doesn't, and the problem is the string

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

北战南征 提交于 2019-12-05 08:22:37
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) (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

Extract python 'native' values from numpy structured array

China☆狼群 提交于 2019-12-01 13:16:22
问题 I have a structured numpy array. The numpy structure matches the type google.protobuf.Timestamp . I need to extract the seconds int64 and the nanos int32 from each element of said structure and assign it to the real Timestamp structure. Below I list a script that does just that in a convenient way for anyone to test ( numpy and protobuf Python modules need to be installed). How do I get rid/circumvent the TypeError listed at the end and have the values out of the numpy structure in the

How to save a list of python dictionaries as an array of matlab structured arrays?

冷暖自知 提交于 2019-12-01 10:57:00
问题 I am trying to create a file to be read in a matlab enviroment. The structure in matlab looks like this trx(1) = x: [1×1500 double] y: [1×1500 double] a: [1×1500 double] b: [1×1500 double] theta: [1×1500 double] firstframe: 1 endframe: 1500 nframes: 1500 off: 0 trx(2) = x: [1×751 double] y: [1×751 double] a: [1×751 double] b: [1×751 double] theta: [1×751 double] firstframe: 750 endframe: 1500 nframes: 751 off: -749 So naturally I created a python dictionary with the required fields and create

Convert structured array with various numeric data types to regular array

…衆ロ難τιáo~ 提交于 2019-12-01 00:21:51
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". You can do it easily with Pandas: >>> import pandas as pd >>> pd.DataFrame(my_data).values array([[ 17. , 182.1000061], [ 19. , 175.6000061]], dtype=float32) Here's one way (assuming my_data is a one-dimensional

No binary operators for structured arrays in Numpy?

耗尽温柔 提交于 2019-11-30 19:21:40
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.) So, all goes well until we try to perform some basic math. I get errors for all of the following:

Numpy, Add Column to existing structured array

寵の児 提交于 2019-11-30 17:58:02
问题 I have a starting array as such: [(1, [-112.01268501699997, 40.64249414272372]) (2, [-111.86145708699996, 40.4945008710162])] where the first column is an int and the second is a tuple with floats in there. I need to add a string column called USNG. I then create a structured numpy array as such: dtype = numpy.dtype([('USNG', '|S100')]) x = numpy.empty(array.shape, dtype=dtype) I want to append the x numpy array to the existing one to add a new column so I can output some information to that