繁体   English   中英

在Python的子列表中找到第n个元素的方差

[英]Finding the variance of nth element in sublist in python

我正在寻找使用python数组中每个子数组的第一个元素的方差。

数据看起来像这样...

INPUT = [[x1, y1, z1], [x2, y2, z2], [x3, y3, z3], ... [xn, yn, zn]]

# Code magic

OUTPUT = [x_var, y_var, z_var]

有什么pythonic方法可以做到这一点吗?

我目前正在这种方式。 请不要判断得太苛刻。

INPUT = [[1,2,3], [4,5,6], [7,8,9]]

OUTPUTx = [item[0] for item in INPUT]
OUTPUTy = [item[1] for item in INPUT]
OUTPUTz = [item[2] for item in INPUT]

OUTPUTx = numpy.var(OUTPUTx)
OUTPUTy = numpy.var(OUTPUTy)
OUTPUTz = numpy.var(OUTPUTz)

OUTPUT = numpy.zeros(3)

OUTPUT[0] = OUTPUTx
OUTPUT[1] = OUTPUTy
OUTPUT[2] = OUTPUTz

您可以从文档中在var函数中指定轴:

axis:无或整数或整数元组,可选的一个或多个计算方差的轴。 默认值是计算展平数组的方差。

码:

import numpy as np

INPUT = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(np.var(INPUT, axis=0))

产量

[6. 6. 6.]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM