Stosunkowo nowa biblioteka xray [1] ma Dataset
i DataArray
struktur, które robią dokładnie to, o co prosisz.
Tu jest moje zdanie na temat problemu, napisany jako ipython sesji:
>>> import numpy as np
>>> import xray
>>> ## Prepare data:
>>> #
>>> point = {'x': np.array(-0.47652306228698005),
... 'y': np.array([[-0.41809043],
... [ 0.48407823]])}
>>> points = 10 * [point]
>>> ## Convert to Xray DataArrays:
>>> #
>>> list_x = [p['x'] for p in points]
>>> list_y = [p['y'] for p in points]
>>> da_x = xray.DataArray(list_x, [('x', range(len(list_x)))])
>>> da_y = xray.DataArray(list_y, [
... ('x', range(len(list_y))),
... ('y0', range(2)),
... ('y1', [0]),
... ])
Są to dwa DataArray
przypadki my zbudowane tak daleko:
>>> print(da_x)
<xray.DataArray (x: 10)>
array([-0.47652306, -0.47652306, -0.47652306, -0.47652306, -0.47652306,
-0.47652306, -0.47652306, -0.47652306, -0.47652306, -0.47652306])
Coordinates:
* x (x) int32 0 1 2 3 4 5 6 7 8 9
>>> print(da_y.T) ## Transposed, to save lines.
<xray.DataArray (y1: 1, y0: 2, x: 10)>
array([[[-0.41809043, -0.41809043, -0.41809043, -0.41809043, -0.41809043,
-0.41809043, -0.41809043, -0.41809043, -0.41809043, -0.41809043],
[ 0.48407823, 0.48407823, 0.48407823, 0.48407823, 0.48407823,
0.48407823, 0.48407823, 0.48407823, 0.48407823, 0.48407823]]])
Coordinates:
* x (x) int32 0 1 2 3 4 5 6 7 8 9
* y0 (y0) int32 0 1
* y1 (y1) int32 0
Możemy teraz Merge te dwa DataArray
w ich wspólnym wymiarze x
do jednego wymiaru: DataSet
:
I wreszcie możemy przejść, a dane zagregowane sposób Poszukiwany
>>> ds['X'].sum()
<xray.DataArray 'X'()>
array(-4.765230622869801)
>>> ds['Y'].sum()
<xray.DataArray 'Y'()>
array(0.659878)
>>> ds['Y'].sum(axis=1)
<xray.DataArray 'Y' (x: 10, y1: 1)>
array([[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878],
[ 0.0659878]])
Coordinates:
* x (x) int32 0 1 2 3 4 5 6 7 8 9
* y1 (y1) int32 0
>>> np.all(ds['Y'].sum(axis=1) == ds['Y'].sum(dim='y0'))
True
>>>> ds['X'].sum(dim='y0')
Traceback (most recent call last):
ValueError: 'y0' not found in array dimensions ('x',)
[1] biblioteka do obsługi danych n-wymiarowej z etykietami, jak pandy czy 2D: http://xray.readthedocs.org/en/stable/data-structures.html#dataset
Czy zapoznałeś się z [panelem] (http://pandas.pydata.org/pandas-docs/dev/dsintro.html#panel) datastructure? Nie jestem pewien, czy to pomaga w przypadku użycia ... –
Czy możemy uzyskać próbki danych dla Twojego problemu? – BKay
Z pewnością dodałem trochę wyżej. To pomaga? A może chcesz coś więcej? –