【问题标题】:iterating over lists finding standard deviation of each point遍历列表找到每个点的标准偏差
【发布时间】:2013-07-20 00:40:42
【问题描述】:

我有这段代码应该找到 A 中每个数字的标准偏差,其中 A 是由 7 个值组成的列表列表。

def sigma(A):
    diff = 0
    positives = [b for b in A if b >= 0]
    if positives:
        mean = sum(positives) / len(positives)
        for i in positives:  
            diff = ((sum([abs(i - mean)**2 for i in positives]))/(len(positives)))**(0.5)
            return diff
    else:
        return 0

    G = map(sigma, zip(*A))
    print G

这正确地给了我第一个 7 个数字列表的标准偏差,但 map(sigma, zip(*A)) 不应该让它遍历所有列表吗?我也尝试过[sigma(A) for col in xrange(len(rows[0]))],但这也没有用。理想情况下,标准偏差也将保存为七人列表。任何帮助表示赞赏。

更新: 这是我现在拥有的代码;

def sigma(A):
    diff = 0
    positives = [b for b in A if b >= 0]
    if positives:
        mean = sum(positives) / len(positives)
        diff += ((sum([abs(i - mean)**2 for i in positives]))/(len(positives)))**(0.5)
        for i in positives:
            if (abs(i - mean)) > (diff*3):
                return -9999.00
            else:
                return i

    else:
        return -9999.00

G = map(sigma, zip(*A))
print G

它完成了我想要它做的所有事情,但是当我以这种方式运行它时,它只输出第一行。如果将“return”语句替换为“print”并删除print G,则打印所有行的我想要的输出。如何将所有这些值存储在列表中?我假设这是这条线G = map(sigma, zip(*A)) 这就是问题所在。我尝试将其更改为G = map(sigma, A),但这只会给我第一列的数字。 有人有什么想法吗?

【问题讨论】:

  • 什么“所有列表”?简短的回答是“是”,但A 的结构很可能会打破这种局面。因此,您需要发布更多信息,具体回答“A 长什么样?”和“你得到什么错误?”
  • “但这也不起作用”。当然,因为软件只有一种故障模式,我们都知道这意味着什么。
  • A 看起来像 [[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], [-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], [0.040896, 0.018690, 0.005620, -9999.0, 0.038722, 0.018323, -9999.0], [0.039443, 0.017517, 0.003460, -9999.0, 0.035526, 0.011692, -9999.0], [-9999.0, 0.017963, 0.005264, -9999.0, 0.03788, 0.014316, -9999.0]] 所以“所有列表”将是 5 组 7 个数字。我没有收到错误,我只是没有得到我想要的所有信息(所有 5 次迭代)。

标签: python list loops python-2.x standard-deviation


【解决方案1】:

positives = [b for b in A if b >= 0] 不会做你认为的那样。 b 是 7 个元素的列表,7 个元素的列表怎么会大于 0?

numpy 让这一切变得简单:

import numpy as np
import numpy.ma as ma
A = [[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [0.040896, 0.018690, 0.005620, -9999.0, 0.038722, 0.018323, -9999.0],
    [0.039443, 0.017517, 0.003460, -9999.0, 0.035526, 0.011692, -9999.0],
    [-9999.0, 0.017963, 0.005264, -9999.0, 0.03788, 0.014316, -9999.0]]
A = np.array(A)

sigmas = []
for b in A:
    bmask=ma.masked_array(b,mask=np.greater_equal(b,0))
    b=b[bmask.mask]
    print b
    sigmas.append(np.std(b))

给予

[]
[]
[ 0.040896  0.01869   0.00562   0.038722  0.018323]
[ 0.039443  0.017517  0.00346   0.035526  0.011692]
[ 0.017963  0.005264  0.03788   0.014316]

>>> sigmas
[0.0, 0.0, 0.013412289355661845, 0.013828802328473713, 0.011917047544903896]

编辑:回应评论

>>> A=[[1,2,3,4,5,6,7],[2,-3,4,-3,2,1,-9]]
>>> [b for b in A if b>=0]
[[1, 2, 3, 4, 5, 6, 7], [2, -3, 4, -3, 2, 1, -9]]

Python 不会给你一个错误,但它不会比较b0 中的元素,它只是比较b,它被评估为布尔值。

在这里您可以清楚地看到正在发生的事情:

>>> bool(b)
True
>>> True >= 0
True

对于 A 中的每个 7 个数字列表 b,你只是在做 True >= 0,它始终是 True

edit2:我是个白痴,现在看到您正在尝试使用地图,而我正在谈论的问题将被避免。只需将G = map(sigma, zip(*A)) 更改为G = map(sigma, A)

edit3::您返回的是i,而不是diff。这是代码:

def sigma(A):
    positives = [b for b in A if b >= 0]
    if positives:
        mean = sum(positives) / len(positives)
        diff = ((sum([abs(i - mean)**2 for i in positives]))/(len(positives)))**(0.5)
        for i in positives:
            if (abs(i - mean)) > (diff*3):
                return -9999.00
        return diff
    else:
        return -9999.00

A = [[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [0.040896, 0.018690, 0.005620, -9999.0, 0.038722, 0.018323, -9999.0],
    [0.039443, 0.017517, 0.003460, -9999.0, 0.035526, 0.011692, -9999.0],
    [-9999.0, 0.017963, 0.005264, -9999.0, 0.03788, 0.014316, -9999.0]]

G = map(sigma, A)

给出:

>>> G
[-9999.0, -9999.0, 0.013412289355661845, 0.013828802328473713, 0.011917047544903896]

edit4:澄清问题

def sigma(A):
    positives = [b for b in A if b >= 0]
    sq_err=[]
    if positives:
        mean = sum(positives) / len(positives)
        diff = ((sum([abs(i - mean)**2 for i in positives]))/(len(positives)))**(0.5)
        for i in positives:
            if (abs(i - mean)) > (diff*3):
                sq_err.append(-9999.00)
            else:
                sq_err.append(i)
    else:
        return [-9999.00]
    return sq_err

A = [[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0],
    [0.040896, 0.018690, 0.005620, -9999.0, 0.038722, 0.018323, -9999.0],
    [0.039443, 0.017517, 0.003460, -9999.0, 0.035526, 0.011692, -9999.0],
    [-9999.0, 0.017963, 0.005264, -9999.0, 0.03788, 0.014316, -9999.0]]

G = map(sigma, A)

给予

>>> G
[[-9999.0], [-9999.0], [0.040896, 0.01869, 0.00562, 0.038722, 0.018323], [0.039443, 0.017517, 0.00346, 0.035526, 0.011692], [0.017963, 0.005264, 0.03788, 0.014316]]

【讨论】:

  • 所以我使用 positives = [b for b in A if b >= 0] 只给我 A 的每一行中的正数,它似乎正在工作。另外,如果可能的话,我想知道我的代码有什么问题,因为现在它似乎可以做我想做的一切,但是当我调用该函数时,我只能返回第一行
  • >>> A=[[1,2,3,4,5,6,7],[2,-3,4,-3,2,1,-9]]>>> [b for b in A if b>=0][[1, 2, 3, 4, 5, 6, 7], [2, -3, 4, -3, 2, 1, -9]]
  • print A= [[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], [-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], [0.040896, 0.018690, 0.005620, -9999.0, 0.038722, 0.018323, -9999.0], [0.039443, 0.017517, 0.003460, -9999.0, 0.035526, 0.011692, -9999.0], [-9999.0, 0.017963, 0.005264, -9999.0, 0.037885, 0.014316, -9999.0]] 但是当我打印正片=[0.040896, 0.039443] [0.018690, 0.017517, 0.017963] [0.005620, 0.003460, 0.005264] [0.038722, 0.035526, 0.037885] [0.018323, 0.011692, 0.014316] hmmm?
  • 没问题!谢谢你和我一起坚持这个哈哈。我确实尝试了 map(sigma, A) ,然后我只得到每行的第一个数字
  • 但是G还是7个数字的列表,应该是7个数字的5个列表
猜你喜欢
  • 2012-12-03
  • 2021-10-16
  • 2015-06-27
  • 2021-11-30
  • 2016-07-23
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多