- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
摘要:我有一个数据集,其收集方式使得维度最初不可用。我想获取本质上是一大块无差别的数据,并为其添加维度,以便可以对其进行查询、子集化等。这是以下问题的核心。
这是我拥有的 xarray 数据集:
<xarray.Dataset>
Dimensions: (chain: 1, draw: 2000, rows: 24000)
Coordinates:
* chain (chain) int64 0
* draw (draw) int64 0 1 2 3 4 5 6 7 ... 1993 1994 1995 1996 1997 1998 1999
* rows (rows) int64 0 1 2 3 4 5 6 ... 23994 23995 23996 23997 23998 23999
Data variables:
obs (chain, draw, rows) float64 4.304 3.985 4.612 ... 6.343 5.538 6.475
Attributes:
created_at: 2019-12-27T17:16:13.847972
inference_library: pymc3
inference_library_version: 3.8
此处的行
维度对应于我需要恢复到数据的许多子维度。特别是,24,000 行对应于 240 个条件的 100 个样本(这 100 个样本位于连续的 block 中)。这些条件是门
、输入
、生长介质
和od
的组合。
我希望得到这样的结果:
<xarray.Dataset>
Dimensions: (chain: 1, draw: 2000, gate: 1, input: 4, growth_medium: 3, sample: 100, rows: 24000)
Coordinates:
* chain (chain) int64 0
* draw (draw) int64 0 1 2 3 4 5 6 7 ... 1993 1994 1995 1996 1997 1998 1999
* rows *MultiIndex*
* gate (gate) int64 'AND'
* input (input) int64 '00', '01', '10', '11'
* growth_medium (growth_medium) 'standard', 'rich', 'slow'
* sample (sample) int64 0 1 2 3 4 5 6 7 ... 95 96 97 98 99
Data variables:
obs (chain, draw, gate, input, growth_medium, samples) float64 4.304 3.985 4.612 ... 6.343 5.538 6.475
Attributes:
created_at: 2019-12-27T17:16:13.847972
inference_library: pymc3
inference_library_version: 3.8
我有一个 pandas 数据框,它指定门、输入和生长介质的值 - 每行给出一组门、输入和生长介质的值,以及一个指定位置的索引(在 rows
)会出现相应的 100 个样本集。目的是该数据框是标记数据集的指南。
我查看了有关“ reshape 和重组数据”的 xarray 文档,但我不知道如何组合这些操作来完成我需要的操作。我怀疑我需要将它们与 GroupBy 结合起来,但我不知道如何做。谢谢!
后来:我有一个解决这个问题的方法,但是它太恶心了,我希望有人能解释我的错误有多大,以及还有什么更优雅的方法是可能的。
因此,首先,我将原始 Dataset
中的所有数据提取为原始 numpy 形式:
foo = qm.idata.posterior_predictive['obs'].squeeze('chain').values.T
foo.shape # (24000, 2000)
然后我根据需要重新塑造它:
bar = np.reshape(foo, (240, 100, 2000))
这大致给出了我想要的形状:有 240 种不同的实验条件,每种条件有 100 个变体,对于每个变体,我的数据集中都有 2000 个蒙特卡罗样本。
现在,我从 Pandas DataFrame
中提取有关 240 个实验条件的信息:
import pandas as pd
# qdf is the original dataframe with the experimental conditions and some
# extraneous information in other columns
new_df = qdf[['gate', 'input', 'output', 'media', 'od_lb', 'od_ub', 'temperature']]
idx = pd.MultiIndex.from_frame(new_df)
最后,我从 numpy 数组和 pandas MultiIndex
重新组装了一个 DataArray
:
xr.DataArray(bar, name='obs', dims=['regions', 'conditions', 'draws'],
coords={'regions': idx, 'conditions': range(100), 'draws': range(2000)})
生成的DataArray
具有这些坐标,如我所愿:
Coordinates:
* regions (regions) MultiIndex
- gate (regions) object 'AND' 'AND' 'AND' 'AND' ... 'AND' 'AND' 'AND'
- input (regions) object '00' '10' '10' '10' ... '01' '01' '11' '11'
- output (regions) object '0' '0' '0' '0' '0' ... '0' '0' '0' '1' '1'
- media (regions) object 'standard_media' ... 'high_osm_media_five_percent'
- od_lb (regions) float64 0.0 0.001 0.001 ... 0.0001 0.0051 0.0051
- od_ub (regions) float64 0.0001 0.0051 0.0051 2.0 ... 0.0003 2.0 2.0
- temperature (regions) int64 30 30 37 30 37 30 37 ... 37 30 37 30 37 30 37
* conditions (conditions) int64 0 1 2 3 4 5 6 7 ... 92 93 94 95 96 97 98 99
* draws (draws) int64 0 1 2 3 4 5 6 ... 1994 1995 1996 1997 1998 1999
不过,这非常可怕,而且我必须穿透 xarray
抽象的所有漂亮层才能达到这一点,这似乎是错误的。特别是因为这似乎并不是科学工作流程中一个不寻常的部分:获取相对原始的数据集以及需要与数据结合的元数据电子表格。那么我做错了什么?更优雅的解决方案是什么?
最佳答案
给定起始数据集,类似于:
<xarray.Dataset>
Dimensions: (draw: 2, row: 24)
Coordinates:
* draw (draw) int32 0 1
* row (row) int32 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
Data variables:
obs (draw, row) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
您可以连接多个纯 xarray 命令来分割维度(获取相同形状但使用多重索引的数据),甚至 reshape 数据集。要分割维度,可以使用以下代码:
multiindex_ds = ds.assign_coords(
dim_0=["a", "b", "c"], dim_1=[0,1], dim_2=range(4)
).stack(
dim=("dim_0", "dim_1", "dim_2")
).reset_index(
"row", drop=True
).rename(
row="dim"
)
multiindex_ds
其输出是:
<xarray.Dataset>
Dimensions: (dim: 24, draw: 2)
Coordinates:
* draw (draw) int32 0 1
* dim (dim) MultiIndex
- dim_0 (dim) object 'a' 'a' 'a' 'a' 'a' 'a' ... 'c' 'c' 'c' 'c' 'c' 'c'
- dim_1 (dim) int64 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
- dim_2 (dim) int64 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
Data variables:
obs (draw, dim) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
此外,多重索引可以被取消堆叠,从而有效地 reshape 数据集:
reshaped_ds = multiindex_ds.unstack("dim")
reshaped_ds
输出:
<xarray.Dataset>
Dimensions: (dim_0: 3, dim_1: 2, dim_2: 4, draw: 2)
Coordinates:
* draw (draw) int32 0 1
* dim_0 (dim_0) object 'a' 'b' 'c'
* dim_1 (dim_1) int64 0 1
* dim_2 (dim_2) int64 0 1 2 3
Data variables:
obs (draw, dim_0, dim_1, dim_2) int32 0 1 2 3 4 5 ... 42 43 44 45 46 47
我认为仅此并不能完全满足您的需求,因为您想将一个维度转换为二维,其中之一是多重索引。不过,所有的构建模块都在这里。
例如,您可以按照此步骤(包括取消堆叠)使用 regions
和 conditions
,然后按照此步骤(现在无需取消堆叠)来转换 regions
到多重索引。另一种选择是从一开始就使用所有维度,取消堆叠它们,然后再次堆叠它们,将条件
保留在最终多重索引之外。
答案结合了几个完全不相关的命令,要了解每个命令在做什么可能很困难。
分配坐标
第一步是创建新的尺寸和坐标并将其添加到数据集中。这是必要的,因为接下来的方法需要数据集中已经存在的尺寸和坐标。
在assign_coords
之后停止会产生以下数据集:
<xarray.Dataset>
Dimensions: (dim_0: 3, dim_1: 2, dim_2: 4, draw: 2, row: 24)
Coordinates:
* draw (draw) int32 0 1
* row (row) int32 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
* dim_0 (dim_0) <U1 'a' 'b' 'c'
* dim_1 (dim_1) int32 0 1
* dim_2 (dim_2) int32 0 1 2 3
Data variables:
obs (draw, row) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
堆栈
数据集现在包含 3 个维度,最多可添加 24 个元素,但是,由于数据当前相对于这 24 个元素是平坦的,我们必须将它们堆叠到单个 24 元素多重索引中,以使它们的形状兼容。
我发现 assign_coords
后跟 stack
是最自然的解决方案,但是,另一种可能性是生成一个多重索引,类似于上面的操作方式并直接调用 assign_coords
与多重索引,使堆栈不必要。
此步骤将所有 3 个新维度合并为一个维度:
<xarray.Dataset>
Dimensions: (dim: 24, draw: 2, row: 24)
Coordinates:
* draw (draw) int32 0 1
* row (row) int32 0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
* dim (dim) MultiIndex
- dim_0 (dim) object 'a' 'a' 'a' 'a' 'a' 'a' ... 'c' 'c' 'c' 'c' 'c' 'c'
- dim_1 (dim) int64 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
- dim_2 (dim) int64 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
Data variables:
obs (draw, row) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
请注意,根据需要,现在我们有 2 个维度,大小为 24。
重置索引
现在我们的最终维度作为坐标出现在数据集中,我们希望这个新坐标成为用于索引变量 obs 的坐标。 set_index
似乎是正确的选择,但是,我们的每个坐标都对自身进行索引(与 set_index
文档中的示例不同,其中 x
对 x
和 进行索引>a
坐标),这意味着在这种特殊情况下不能使用 set_index
。使用的方法是reset_index
,以删除坐标row
而不删除维度row
。
在下面的输出中可以看出,现在 row
是一个没有坐标的维度:
<xarray.Dataset>
Dimensions: (dim: 24, draw: 2, row: 24)
Coordinates:
* draw (draw) int32 0 1
* dim (dim) MultiIndex
- dim_0 (dim) object 'a' 'a' 'a' 'a' 'a' 'a' ... 'c' 'c' 'c' 'c' 'c' 'c'
- dim_1 (dim) int64 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
- dim_2 (dim) int64 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
Dimensions without coordinates: row
Data variables:
obs (draw, row) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
重命名
当前数据集几乎是最后一个,唯一的问题是 obs
变量仍然具有 row
维度,而不是所需的维度:dim
。它看起来并不是 rename
的预期用途,但它可以用来使 dim
到 absorb row
,产生所需的最终结果(上面称为 multiindex_ds
)。
这里,set_index
似乎是要选择的方法,但是,如果不是 rename(row="dim")
,而是 set_index(row=使用“dim”)
,多重索引被折叠成由元组组成的索引:
<xarray.Dataset>
Dimensions: (draw: 2, row: 24)
Coordinates:
* draw (draw) int32 0 1
* row (row) object ('a', 0, 0) ('a', 0, 1) ... ('c', 1, 2) ('c', 1, 3)
Data variables:
obs (draw, row) int32 0 1 2 3 4 5 6 7 8 ... 39 40 41 42 43 44 45 46 47
关于python - 如何分割/细化 xarray 数据集中的维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59504320/
我想做一个系统,用户可以上传和下载文件。系统将具有一个集中的地形,但在很大程度上依赖于节点将相关数据通过中心节点传输给其他节点我不希望对等端保存整个文件,而是希望它们保存整个数据集的一个压缩的加密部分
我正在 Riverpod Auth 流程样板应用程序中工作。 我想对所有异步功能甚至登录和注销使用通用加载屏幕。目前,如果 Appstate 加载我显示加载屏幕,我有 AppState 提供程序。它可
我有一个 functions.php 文件,其中包括以下功能: function head() { global $brand, $brandName, $logo, $slogan, $si
我有下一个 html 代码 ... 我想选择随机的 div 数组来向它们添加一个事件类,因为我使用这个 jquery 代码 function randOrder() { return
多年来,我创建并调整了一组NAnt脚本以执行完整的项目构建。主脚本采用一个应用程序端点(例如,一个Web应用程序项目),并从源代码控制中对其进行完整的构建。脚本已预先配置了与构建输出位置,源代码控制地
我希望我的 jQuery 插件在 $(window) 选择上调用时表现不同。如何检查 window 是否在集合中?到目前为止我的尝试: >>> $(window) == $(window) false
考虑到我们有 let existingSet = $(); 如何通过 jQuery 将 newElements 添加到该集合中? existingSet = existingSet.add(newEl
我需要在 priority_queue 中保存一个整数集合。但是我需要能够删除这些整数中的一个,即使它不是我容器的第一个元素。我无法使用 std::priority_queue。我选择使用一个集合来根
对于我的网站,我一直在尝试集中所有内容以便在移动设备上显示: http://m.bachatdeals.com 我在移动设备上打开网站后,内容下方有很多空间,我必须捏住 zoon 才能阅读,如何删除下
我计划为我的剑道验证器制定一些自定义规则,并希望在所有验证器之间共享。在我的验证器代码中,我有: rules: { bothorblank: function (input) {
这是我的函数,用于测试两个点 x 和 y 在 MAX_ITERATION 255 之后是否在 mandelbrot 集合中。如果不在,它应该返回 0,如果在,则返回 1。 int isMandelbr
致力于从移动设备扩展到桌面设备的简单网站布局。一切都按预期工作,但由于某种原因,我的 float div 没有集中放置。我已经完成了正常工作,但这次不适合我?有什么想法吗? 这是我的 CSS: /*
我的“div”元素有一个相对宽度,它不是绝对的,所以我不能使用精确的数字来集中。一个不错的解决方案是使用“display: inline-block”: body { text-align:
目前我拥有的所有类都处理它们自己的导入。使用一个典型的例子: [ImportMany] private Lazy[] someOfMyInterfaces { get; set; } public M
我有一个类定义: class Question: title = "" answer = "" def __init__(self, title, answer):
我正在尝试将一个对象 Point2D 插入到一个 Point2D 集合中,但我做不到,似乎该集合适用于 int 和 char 但不适用于对象。 我需要帮助来了解如何将对象插入到集合中???假设我想按
我的应用上有一些弹出窗口,它是全屏的,代码如下: content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
我们有一个多模块 Quarkus 项目,带有一个公共(public)库和多个应用程序。在通用的 lib 中,我们有各种缓存用于所有应用。 我们希望不必在每个应用程序的所有配置文件中配置保留和容量。 有
这个问题在这里已经有了答案: Nested facets in ggplot2 spanning groups (2 个回答) 去年关闭。 我在 ggplot 中创建了一个图表里面有两个变量 face
我无法集中v-radio-group。这是我得到的:
我是一名优秀的程序员,十分优秀!