あなたは正しいトラックにあります。あなたはリスト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)
:あなたはあなたの平均とstd計算のうち、リストオブジェクトを作成する必要はありません
- 関数呼び出しみましょうあなたがプラグインあなた
samplePopulation
と、任意の数の値を手で入れずに手動で値を変更することなく、標準偏差を入力する
- リスト内包表記はループの1行で、多かれ少なかれ、あなたの中に必要なフィルタリングを行うこともできます。
数値は既にnumpy配列として保存されていますか? – James