gpt4 book ai didi

python - 在 pandas DataFrame 列中存储多维数组

转载 作者:太空狗 更新时间:2023-10-29 17:15:43 25 4
gpt4 key购买 nike

我希望使用 pandas 作为主要的 Trace(来自 MCMC 的参数空间中的一系列点)对象。

我有一个 string->array 的字典列表,我想将其存储在 pandas 中。字典中的键总是相同的,对于每个键,numpy 数组的形状总是相同的,但不同键的形状可能不同,并且可能具有不同的维数。

我一直在使用 self.append(dict_list, ignore_index = True) 这似乎适用于 1d 值,但对于 nd>1 值 pandas 将值存储为不允许的对象用于漂亮的绘图和其他漂亮的东西。关于如何获得更好的行为有什么建议吗?

示例数据

point = {'x': array(-0.47652306228698005),
'y': array([[-0.41809043],
[ 0.48407823]])}

points = 10 * [ point]

我希望能够做类似的事情

df = DataFrame(points)

df = DataFrame()
df.append(points, ignore_index=True)

>> df['x'][1].shape
()
>> df['y'][1].shape
(2,1)

最佳答案

相对较新的库 xray[1] 有 DatasetDataArray 结构,可以完全满足您的要求。

这是我对你的问题的看法,写成一个 IPython session :

>>> 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]),
... ])

这是我们目前构建的两个 DataArray 实例:

>>> 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

我们现在可以将这两个 DataArray 在它们共同的 x 维度上合并到一个 DataSet 中:

>>> ds = xray.Dataset({'X':da_x, 'Y':da_y})
>>> print(ds)
<xray.Dataset>
Dimensions: (x: 10, y0: 2, y1: 1)
Coordinates:
* x (x) int32 0 1 2 3 4 5 6 7 8 9
* y0 (y0) int32 0 1
* y1 (y1) int32 0
Data variables:
X (x) float64 -0.4765 -0.4765 -0.4765 -0.4765 -0.4765 -0.4765 -0.4765 ...
Y (x, y0, y1) float64 -0.4181 0.4841 -0.4181 0.4841 -0.4181 0.4841 -0.4181 ...

我们最终可以按照您想要的方式访问和聚合数据:

>>> 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] 一个用于处理带标签的 N 维数据的库,就像 pandas 处理 2D 一样:http://xray.readthedocs.org/en/stable/data-structures.html#dataset

关于python - 在 pandas DataFrame 列中存储多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15806414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com