recarray

how to set dtype for nested numpy ndarray?

谁说我不能喝 提交于 2020-01-15 11:19:09
问题 I am working on the following data structure, from which I am trying to create a ndarray contains all the data: instrument filter response ----------------------------------------------------- spire 250um array of response ... ... ... where the array of response is: linenumber wavelangth throughput ----------------------------------------------------- 0 1.894740e+06 0.000e+00 1 2.000000e+06 1.000e-02 2 2.026320e+06 3.799e-02 ... .... .... So, I hope I can turn the data to one ndarray, by

Python numpy recarray: Can one obtain a view into different fields using pointer arithmetic?

你。 提交于 2019-12-24 04:03:45
问题 I have a numpy structured array of the following form: x = np.array([(1,2,3)]*2, [('t', np.int16), ('x', np.int8), ('y', np.int8)]) I now want to generate views into this array that team up 't' with either 'x' or 'y' . The usual syntax creates a copy: v_copy = x[['t', 'y']] v_copy #array([(1, 3), (1, 3)], # dtype=[('t', '<i2'), ('y', '|i1')]) v_copy.base is None #True This is not unexpected, since picking two fields is "fancy indexing", at which point numpy gives up and makes a copy. Since my

numpy recarray append_fields: can't append numpy array of datetimes

只谈情不闲聊 提交于 2019-12-20 01:40:36
问题 I have a recarray containing various fields and I want to append an array of datetime objects on to it. However, it seems like the append_fields function in numpy.lib.recfunctions won't let me add an array of objects. Here's some example code: import numpy as np import datetime import numpy.lib.recfunctions as recfun dtype= np.dtype([('WIND_WAVE_HGHT', '<f4'), ('WIND_WAVE_PERD', '<f4')]) obs = np.array([(0.1,10.0),(0.2,11.0),(0.3,12.0)], dtype=dtype) dates = np.array([datetime.datetime(2001,1

Convert structured array to regular NumPy array

◇◆丶佛笑我妖孽 提交于 2019-12-17 03:04:23
问题 The answer will be very obvious I think, but I don't see it at the moment. How can I convert a record array back to a regular ndarray? Suppose I have following simple structured array: x = np.array([(1.0, 4.0,), (2.0, -1.0)], dtype=[('f0', '<f8'), ('f1', '<f8')]) then I want to convert it to: array([[ 1., 4.], [ 2., -1.]]) I tried asarray and astype , but that didn't work. UPDATE (solved: float32 (f4) instead of float64 (f8)) OK, I tried the solution of Robert ( x.view(np.float64).reshape(x

How to convert a subset of numpy recarray to continuous array?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:06:46
问题 I have a recarray that comes from reading a csv file. I am interested in converting a subset of columns to a continuous float array. I'd like to avoid converting them to list or stacking them one by one. I tried the suggestions in https://stackoverflow.com/a/11792956 and https://stackoverflow.com/a/7842620 but I get ValueError: new type not compatible with array. Here is my code: a = np.recfromcsv(r"myfile.csv") #a has many columns of type int, float or string. I want to extract those called

Python Numpy Structured Array (recarray) assigning values into slices

血红的双手。 提交于 2019-12-10 13:07:17
问题 The following example shows what I want to do: >>> test rec.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)], dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')]) >>> test[['ifAction', 'ifDocu']][0] (0, 0) >>> test[['ifAction', 'ifDocu']][0] = (1,1) >>> test[['ifAction', 'ifDocu']][0] (0, 0) So, I want to assign the values (1,1) to test[['ifAction', 'ifDocu']][0] . (Eventually, I want to do something like test

Join Recarrays by attributes in Python

爷,独闯天下 提交于 2019-12-08 06:38:13
问题 I am trying to join recarrys in python such that the same value joins to many elements. The following code works when it is a 1:1 ratio, but when I am trying to do many:1, it only joins one instance: import numpy as np import matplotlib # First data structure sex = np.array(['M', 'F', 'M', 'F', 'M', 'F']) causes = np.array(['c1', 'c1', 'c2', 'c2', 'c3', 'c3']) data1 = np.core.records.fromarrays([sex, causes], names='sex, causes') # Second data structure causes2 = np.array(['c1', 'c2', 'c3'])

Join Recarrays by attributes in Python

旧时模样 提交于 2019-12-08 04:03:24
I am trying to join recarrys in python such that the same value joins to many elements. The following code works when it is a 1:1 ratio, but when I am trying to do many:1, it only joins one instance: import numpy as np import matplotlib # First data structure sex = np.array(['M', 'F', 'M', 'F', 'M', 'F']) causes = np.array(['c1', 'c1', 'c2', 'c2', 'c3', 'c3']) data1 = np.core.records.fromarrays([sex, causes], names='sex, causes') # Second data structure causes2 = np.array(['c1', 'c2', 'c3']) analyst = np.array(['p1', 'p2', 'p3']) data2 = np.core.records.fromarrays([causes2, analyst], names=

numpy recarray indexing based on intersection with external array

倖福魔咒の 提交于 2019-12-07 23:40:08
问题 I'm trying to subset the records in a numpy.recarray based on the common values between one of the recarrays fields and an external array. For example, a = np.array([(10, 'Bob', 145.7), (20, 'Sue', 112.3), (10, 'Jim', 130.5)], dtype=[('id', 'i4'), ('name', 'S10'), ('weight', 'f8')]) a = a.view(np.recarray) b = np.array([10,30]) I want to take the intersection of a.id and b to determine what records to pull from the recarray, so that I get back: (10, 'Bob', 145.7) (10, 'Jim', 130.5) Naively, I

numpy recarray indexing based on intersection with external array

ε祈祈猫儿з 提交于 2019-12-06 05:58:51
I'm trying to subset the records in a numpy.recarray based on the common values between one of the recarrays fields and an external array. For example, a = np.array([(10, 'Bob', 145.7), (20, 'Sue', 112.3), (10, 'Jim', 130.5)], dtype=[('id', 'i4'), ('name', 'S10'), ('weight', 'f8')]) a = a.view(np.recarray) b = np.array([10,30]) I want to take the intersection of a.id and b to determine what records to pull from the recarray, so that I get back: (10, 'Bob', 145.7) (10, 'Jim', 130.5) Naively, I tried: common = np.intersect1d(a.id, b) subset = a[common] but of course that doesn't work because