- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我认为最容易说明我的问题,一般情况很难解释。
假设我有一个矩阵
a with dimensions NxMxT,
人们可以将 T 视为时间维度(以使问题更容易)。设 (n,m) 为通过 NxM 的索引。我可能会称 (n,m) 为状态空间标识符。然后我需要找到 python/scipy 等价物
for each (n,m):
find a*(n,m) = min(a(n,m,:) s.t. a*(n,m) > a(n,m,T)
也就是说,为整个状态空间找到仍然高于最后一次(在时间维度中)观察值的最小状态空间值。
我的第一次尝试是先解决内层问题(找到比a[...,-1]高的a):
aHigherThanLast = a[ a > a[...,-1][...,newaxis] ]
然后我想为每个 (n,m) 找到所有这些中最小的一个。不幸的是,aHigherThanLast 现在包含所有这些值的一维数组,所以我不再有 (n,m) 对应关系。对此有什么更好的方法?
还有一个问题:状态空间是可变的,它也可以是 3 维或更多维(NxMxKx...),我无法对其进行硬编码。所以任何一种
for (n,m,t) in nditer(a):
不可行。
非常感谢!
/编辑:
a = array([[[[[[[[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.]]]],
[[[[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.]]]]],
[[[[[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.]]]],
[[[[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.],
[ 0., 2., 1.]]]]]]]])
# a.shape = (1L, 1L, 2L, 2L, 1L, 1L, 10L, 3L). so in this case, T = 3.
# expected output would be the sort of
# b.shape = (1L, 1L, 2L, 2L, 1L, 1L, 10L), which solves
b[a,b,c,d,e,f,g] > a[a,b,c,d,e,f,g,-1](b 高于最新观测值)
a中i中没有元素同时满足
-- a[a,b,c,d,e,f,g,t] > a[a,b,c,d,e,f,g,-1]
-- a[a,b,c,d,e,f,g,t] < b[a,b,c,d,e,f,g] (b 是大于的最小元素最新观察)
因此,假设前一个数组是一个简单的堆栈 if [0,2,1] 沿着最后的观察,我期望
b = ones((1,1,2,2,1,1,10))*2
但是, - 如果在一些 (a,b,c,d,e,f,g) 中,不仅有 {0,1,2} 的值,还有 {3},那么我仍然想要 2 (因为它是满足 i > 1 的 i = {2,3} 中的较小者。 - 如果在某些 (a,b,c,d,e,f,g) 中只有值 {0,1,3},我想要 3,因为 i = 3 将是满足 i 的最小数字> 1.
希望这能稍微澄清一下?
/编辑2:
非常感谢答案,它有效。如果我想要相反的东西,即较小的那些中最大的,我将如何调整它?我没有尝试通过那个复杂的索引逻辑,所以我(弱)尝试只改变前三行没有成功:
b = sort(a[...,:-1], axis=-1)
b = b[...,::-1]
mask = b < a[..., -1:]
index = argmax(mask, axis=-1)
indices = tuple([arange(j) for j in a.shape[:-1]])
indices = meshgrid(*indices, indexing='ij', sparse=True)
indices.append(index)
indices = tuple(indices)
a[indices]
此外,我的第二次尝试 [...,::-1][indices] 也没有成功。
最佳答案
我认为 E 先生的做法是正确的。您一定要先对没有最后时间值的数组进行排序:
b = np.sort(a[..., :-1], axis=-1)
理想情况下,您现在可以使用 `np.searchsorted
来查找大于最终值的第一项的位置,但不幸的是 np.searchsorted
仅适用于展平数组,所以我们必须做更多的工作,比如创建一个 bool 掩码,然后使用 np.argmax
找到第一个 True
:
mask = b > a[..., -1:]
index = np.argmax(mask, axis=-1)
你现在有了索引,要提取实际值,你需要做一些索引魔术:
indices = tuple([np.arange(j) for j in b.shape[:-1]])
indices = np.meshgrid(*indices, indexing='ij', sparse=True)
indices.append(index)
indices = tuple(indices)
现在你终于可以做到了:
>>> b[indices]
array([[[[[[[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]]],
[[[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]]]],
[[[[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]]],
[[[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]]]]]]])
>>> b[indices].shape
(1L, 1L, 2L, 2L, 1L, 1L, 10L)
要在较小的那些中获得最大的,您可以这样做:
mask = b >= a[..., -1:]
index = np.argmax(mask, axis=-1) - 1
即较小的项中最大的是相等或更大的项中最小项之前的项。第二种情况更清楚地表明,如果没有满足条件的项目,则此方法会给出垃圾结果。在第二种情况下,当发生这种情况时,您将获得索引的 -1
,因此您可以通过 np.any(index == -1)< 检查结果是否有效
.
如果第一种情况不能满足条件,你可以将索引设置为-1
mask = b > a[..., -1:]
wrong = np.all(~mask, axis=-1)
index = np.argmax(mask, axis=-1)
index[wrong] = -1
关于Python/Scipy : Find "bounded" min/max of a matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19930749/
快速且可能简单的 Lambda 问题: 我有一家有评论的餐厅。我想查询具有以下内容的那个: 最大(平均评分) 和 Max(ReviewCount) 和 Max(NewestReviewDate) 和
在尝试使用 C++17 折叠表达式时,我尝试实现 max sizeof ,其中结果是类型 sizeof 的最大值。我有一个使用变量和 lambda 的丑陋折叠版本,但我想不出一种使用折叠表达式和 st
我目前正在使用 C 并遇到了一些我觉得有趣的东西,但似乎在这里找不到任何类似的东西。 我正在为数组(大小 1000000)静态分配内存。我知道这相当大并且有可能引起问题。但是,使用 10^6 不会出现
我有一个具有 max-height 的 div 和其中的图像,应该使用 max-width:100% 和 max-height:100%。在 Chromium 中,这是可行的,但 Firefox 仅使
我有一个最大高度的 div 和里面的一个图像,它应该使用最大宽度:100% 和最大高度:100%。在 Chromium 中,这是可行的,但 Firefox 仅使用最大宽度而忽略最大高度。 div#ov
在一本在线 awk 手册中我找到了例子awk '{ if (NF > max) max = NF } END { print max }' 该程序打印任何输入行上的最大字段数。但我不明白 awk 如何
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在制作一个非循环图数据库。 表 Material (id_item,id_collection,...)主键(id_item,id_collection) (item可以是collection本身
我有以下两个表。 1.电影详情(电影ID、电影名称、评分、票数、年份) 2.电影类型(Movie-ID,Genre) 我正在使用以下查询来执行连接并获得每个评分最高的电影流派。 select Movi
我有一个查询,我想返回 idevent 中给定传感器 ID (sensorID) 范围内的最高 ID 值,但是查询没有返回最高值。 我运行查询时减去 max() 语句的结果: mysql> SELEC
SUM(MAX() + MAX()) 有正确的方法吗? 这是我一直在努力做的事情 SELECT SUM(MAX(account.BALANCE1) + MAX(account.BALANCE2))
这个问题类似于CSS media queries: max-width OR max-height , 但由于我的代表不够高,我无法在回复中添加评论(问题),我想在原始问题中添加。 与其他主题中的发帖
Jon Skeet今天报告(source): Math.Max(1f, float.NaN) == NaN new[] { 1f, float.NaN }.Max() == 1f 为什么? 编辑:双倍
这个问题已经有答案了: Java 8 stream's .min() and .max(): why does this compile? (5 个回答) 已关闭 7 年前。 我正在学习1z0-809
我在处理一些数据库记录时遇到了一些挑战。 我需要为特定列获取具有 MAX 值的行,并且这些记录必须介于两个时间戳值之间。 这是SQL查询 SELECT id, MAX(amount), created
我想在媒体查询中使用 AND 条件。我使用了下面的代码,但是没有用 @media screen and (max-width: 995px AND max-height: 700px) { } 最佳答
在编写 CSS 媒体查询时,有什么方法可以用“或”逻辑指定多个条件吗? 我正在尝试做这样的事情: /* This doesn't work */ @media screen and (max-widt
我对仅使用 max(list array) 和 np.max(list array) 之间的区别有疑问。 这里唯一的区别是 Python 返回代码所需的时间吗? 最佳答案 它们在边缘情况下可能不同,例
例如: a = [[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.
这个问题在这里已经有了答案: Java 8 stream's .min() and .max(): why does this compile? (5 个答案) 关闭 6 年前。 我正在学习 1z0
我是一名优秀的程序员,十分优秀!