我有一个数字列表,其中包含这些数字的样本均值和SD。现在我正在尝试找出mean+-SD,mean +-2SD和mean +-3SD的数字。例如,在mean+-SD部分,我编写了如下代码:
ND1 = [np.mean(l)+np.std(l,ddof=1)]
ND2 = [np.mean(l)-np.std(l,ddof=1)]
m=sorted(l)
print(m)
ND68 = []
if ND2 > m and m< ND1:
ND68.append(m<ND2 and m>ND1)
print (ND68)我的问题是: 1.数字可以通过列表和排列来计算吗?如果是这样,我做错了哪一部分。或者有一些包我可以用来解决这个问题。
发布于 2016-09-08 07:31:41
这可能会有帮助。我们将使用numpy来获取您要查找的值。在我的示例中,我创建了一个正态分布数组,然后使用布尔切片返回+/- 1、2或3标准差之外的元素。
import numpy as np
# create a random normally distributed integer array
my_array = np.random.normal(loc=30, scale=10, size=100).astype(int)
# find the mean and standard dev
my_mean = my_array.mean()
my_std = my_array.std()
# find numbers outside of 1, 2, and 3 standard dev
# the portion inside the square brackets returns an
# array of True and False values. Slicing my_array
# with the boolean array return only the values that
# are True
out_std_1 = my_array[np.abs(my_array-my_mean) > my_std]
out_std_2 = my_array[np.abs(my_array-my_mean) > 2*my_std]
out_std_3 = my_array[np.abs(my_array-my_mean) > 3*my_std]发布于 2016-09-08 07:42:08
在这一点上,你是在正确的轨道上。你知道列表l的均值和标准差,不过我还是把它叫做samplePopulation。
由于您希望对几个标准差间隔执行此操作,因此我建议创建一个小函数。你可以多次调用它,而不需要做太多的额外工作。此外,我还将使用list comprehension,它只是一行中的一个for循环。
import numpy as np
def filter_by_n_std_devs(samplePopulation, numStdDevs):
# you mostly got this part right, no need to put them in lists though
mean = np.mean(samplePopulation) # no brackets needed here
std = np.std(samplePopulation) # or here
band = numStdDevs * std
# this is the list comprehension
filteredPop = [x for x in samplePopulation if x < mean - band or x > mean + band]
return filteredPop
# now call your function with however many std devs you want
filteredPopulation = filter_by_n_std_devs(samplePopulation, 1)
print(filteredPopulation)以下是列表理解的翻译(基于您对append的使用,看起来您可能不知道这些是什么,否则请随意忽略)。
# remember that you provide the variable samplePopulation
# the above list comprehension
filteredPop = [x for x in samplePopulation if x < mean - band or x > mean + band]
# is equivalent to this:
filteredPop = []
for num in samplePopulation:
if x < mean - band or x > mean + band:
filteredPop.append(num)因此,简单回顾一下:
calculations
https://stackoverflow.com/questions/39380316
复制相似问题