0

我正在从 3 个值的差异中产生一个平均值,并希望将它放在一个列表中

我想要平均的列表示例如下所示:

...
[6.0, 270.0, -55.845848680633168],
[6.0, 315.0, -47.572000492889323],
[6.5, 0.0, -47.806802767243724],
[6.5, 45.0, -48.511643275159528],
[6.5, 90.0, -45.002053150122123],
[6.5, 135.0, -51.034656702050455],
[6.5, 180.0, -53.266356523649002],
[6.5, 225.0, -47.872632929518339],
[6.5, 270.0, -52.09662072002746],
[6.5, 315.0, -48.563996448937075]]

前 2 列匹配最多有 3 行(这 2 列是极坐标),在这种情况下,我想取第 3 个元素之间的差异,将其平均并附加点的极坐标和平均结果到一个新列表中

for a in avg_data:
    comparison = []
    for b in avg_data:
        if a[0] == b[0] and a[1] == b[1]:
            comparison.append(b[2])

    print comparison    
    z = 0   # reset z to 0, z does not need set now in if len(comp) == 1

    if len(comparison) == 2: # if there are only 2 elements, compare them
        z += -(comparison[0]) + comparison[1]
    if len(comparison) == 3: # if all 3 elements are there, compare all 3
        z += -(comparison[0]) + comparison[1]
        z += -(comparison[0]) + comparison[2]
        z += -(comparison[1]) + comparison[2]
        z = z/3 #average the variation

    avg_variation.append([a[0], a[1], z]) #append the polar coordinates and the averaged variation to a list

此代码将正确的数据输出到列表中,但每次遇到匹配的极坐标时都会输出它,所以我最终会得到重复的行。

为了阻止这种情况,我尝试实现一个 if 语句以在 avg_variation 列表中查找匹配的极坐标,然后再次执行平均

if a[0] not in avg_variation and a[1] not in avg_variation:

这不起作用,我收到错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我不认为任何或全部是我正在寻找的,因为我只想检查前两列而不是第三列与已经附加的值。有人知道如何使我的 if 语句更好吗?

为了更清楚我的实际问题是什么:

我的代码在嵌套列表中搜索第一个 2 个元素匹配的列表,对第三个元素执行计算,然后将它们附加到一个新列表中。我的问题是,如果有 2 或 3 行第一个 2 元素匹配它会将结果附加到新列表 2 或 3 次,我希望它只执行一次

编辑:对不起,我最初的问题是误导我的代码的目的。

4

3 回答 3

3

IIUC,我认为更简单的方法是

import numpy as np
from itertools import combinations
from collections import defaultdict

def average_difference(seq):
    return np.mean([j-i for i,j in combinations(seq, 2)]) if len(seq) > 1 else 0

def average_over_xy(seq, fn_to_apply):
    d = defaultdict(list)
    for x,y,z in seq:
        d[x,y].append(z)

    outlist = [[x,y,fn_to_apply(z)] for (x,y),z in sorted(d.items())]
    return outlist

它遍历所有行,创建一个字典,其中 x,y 坐标是元素的键和值列表,然后将该字典转换为列表的排序列表,在z. 例如,我们可以使用平均有符号和有序差异,就像在您的代码中一样:

产生

>>> seq = [[1, 2, 30], [1, 2, 40], [1, 2, 50], [1, 3, 4], [1, 3, 6], [2, 10, 5]] 
>>> average_over_xy(seq, average_difference)
[[1, 2, 13.333333333333334], [1, 3, 2.0], [2, 10, 0]]

请注意,您定义它的方式(我在上面已经匹配),答案取决于给出元素的顺序,即

>>> average_over_xy([[1,2,3],[1,2,4]], average_difference)
[[1, 2, 1.0]]
>>> average_over_xy([[1,2,4],[1,2,3]], average_difference)
[[1, 2, -1.0]]

如果你愿意,你可以使用

def average_difference_sorted(seq):
    return average_difference(sorted(seq))

相反,或者使用标准偏差或任何你喜欢的。(你没有提到你的用例,所以我假设你已经按照你想要的顺序得到了列表,你知道陷阱,你真的需要average_difference)。

我们可以做一些numpy基于更快的技巧,以及推广它的方法,但是使用 adefaultdict来累积值是一种方便的模式,而且它通常足够快。

于 2013-04-20T17:46:25.020 回答
1

这是一个可能的解决方案:

l=[[6.0, 270.0, -55.845848680633168],
[6.0, 315.0, -47.572000492889323],
[6.5, 0.0, -47.806802767243724],
[6.0, 180.0, -53.266356523649002],
[6.0, 225.0, -47.872632929518339],
[6.0, 270.0, -52.09662072002746],
[6.0, 315.0, -48.563996448937075]]

# First, we change the structure so that the pair of coordinates
# becomes a tuple which can be used as dictionary key
l=[[(c1, c2), val] for c1, c2, val in l]

# We build a dictionary coord:[...list of values...]
d={}
for coord, val in l:
    d.setdefault(coord,[]).append(val)

# Here, I compute the mean of each list of values.
# Apply your own function !

means = [[coord[0], coord[1], sum(vals)/len(vals)] for coord, vals in d.items()]

print means
于 2013-04-20T18:25:15.420 回答
0

您没有提供所有必要的信息来确保这一点,但我相信您的错误是由对 numpy 数组执行逻辑操作引起的。请参阅对具有类似错误的问题的答案。

如果没有更多信息,很难复制您的问题的上下文来尝试它,但也许在if语句中更具体的布尔运算会有所帮助。

于 2013-04-20T17:39:57.400 回答