计算移动值范围内的均值和标准差
Calculating mean and standard deviation across a moving range of values
BLUF:(使用 Python 3.0)以 0.25 为增量,我想计算并存储一系列值的 mean/std,以便稍后绘制它们或进行进一步分析。
计算 mean/std 很容易,但我无法完全正确地使用算法在值的范围内正确迭代。
数据:https://www.dropbox.com/s/y78pynq9onyw9iu/Data.csv?dl=0
到目前为止,我拥有的是标准化的玩具数据,看起来像散弹枪爆炸,其中一个目标区域在黑线之间被隔离,增量为 0.25:
import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
import seaborn as sns
Data=pd.read_csv("Data.csv")
g = sns.jointplot(x="x", y="y", data=Data)
bottom_lim = 0
top_lim = 0.25
temp = Data.loc[(Data.y>=bottom_lim)&(Data.y<top_lim)]
g.ax_joint.axhline(top_lim, c='k', lw=2)
g.ax_joint.axhline(bottom_lim, c='k', lw=2)
# we have to create a secondary y-axis to the joint-plot, otherwise the kde
might be very small compared to the scale of the original y-axis
ax_joint_2 = g.ax_joint.twinx()
sns.kdeplot(temp.x, shade=True, color='red', ax=ax_joint_2, legend=False)
ax_joint_2.spines['right'].set_visible(False)
ax_joint_2.spines['top'].set_visible(False)
ax_joint_2.yaxis.set_visible(False)
# calculating the StdDev of the y-axis band above
S = temp.std()
M = temp.mean()
print("StdDev", S)
print("Mean", M)
现在我要做的是计算 mean/std(再次在下方):
S = temp.std()
M = temp.mean()
但是在循环中执行此操作以覆盖从 0 到 8 的 "y" 变量的整个范围。我想将这些结果保存在一种格式中,以便稍后可以绘制它们或进一步操作它们(列表、数组等)。
一个简单的 while
循环就可以完成我们想要的:
bottom_lim, top_lim = 0, 0.25
g = sns.jointplot(x="x", y="y", data=data)
while bottom_lim < 7.75 and top_lim < 8:
temp = data.loc[(data.y>=bottom_lim)&(data.y<top_lim)]
g.ax_joint.axhline(top_lim, c='g', lw=2)
g.ax_joint.axhline(bottom_lim, c='g', lw=2)
ax_joint_2 = g.ax_joint.twinx()
sns.kdeplot(temp.x, shade=True, color='green', ax=ax_joint_2, legend=False)
ax_joint_2.spines['right'].set_visible(False)
ax_joint_2.spines['top'].set_visible(False)
ax_joint_2.yaxis.set_visible(False)
# calculating the StdDev of the band above
S = temp.std()
M = temp.mean()
print("StdDev", S)
print("Mean", M)
bottom_lim+=0.25
top_lim+=0.25
我们将不得不调整 top/bottom 的限制以解决丢失的数据,因为没有数据的切片会引发错误,但是当我 运行 此代码的上限和下限低于 2效果很好。
但如果有更优雅的方式,我一直在寻找reduce/reuse/recycle。
BLUF:(使用 Python 3.0)以 0.25 为增量,我想计算并存储一系列值的 mean/std,以便稍后绘制它们或进行进一步分析。
计算 mean/std 很容易,但我无法完全正确地使用算法在值的范围内正确迭代。
数据:https://www.dropbox.com/s/y78pynq9onyw9iu/Data.csv?dl=0
到目前为止,我拥有的是标准化的玩具数据,看起来像散弹枪爆炸,其中一个目标区域在黑线之间被隔离,增量为 0.25:
import csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
import seaborn as sns
Data=pd.read_csv("Data.csv")
g = sns.jointplot(x="x", y="y", data=Data)
bottom_lim = 0
top_lim = 0.25
temp = Data.loc[(Data.y>=bottom_lim)&(Data.y<top_lim)]
g.ax_joint.axhline(top_lim, c='k', lw=2)
g.ax_joint.axhline(bottom_lim, c='k', lw=2)
# we have to create a secondary y-axis to the joint-plot, otherwise the kde
might be very small compared to the scale of the original y-axis
ax_joint_2 = g.ax_joint.twinx()
sns.kdeplot(temp.x, shade=True, color='red', ax=ax_joint_2, legend=False)
ax_joint_2.spines['right'].set_visible(False)
ax_joint_2.spines['top'].set_visible(False)
ax_joint_2.yaxis.set_visible(False)
# calculating the StdDev of the y-axis band above
S = temp.std()
M = temp.mean()
print("StdDev", S)
print("Mean", M)
现在我要做的是计算 mean/std(再次在下方):
S = temp.std()
M = temp.mean()
但是在循环中执行此操作以覆盖从 0 到 8 的 "y" 变量的整个范围。我想将这些结果保存在一种格式中,以便稍后可以绘制它们或进一步操作它们(列表、数组等)。
一个简单的 while
循环就可以完成我们想要的:
bottom_lim, top_lim = 0, 0.25
g = sns.jointplot(x="x", y="y", data=data)
while bottom_lim < 7.75 and top_lim < 8:
temp = data.loc[(data.y>=bottom_lim)&(data.y<top_lim)]
g.ax_joint.axhline(top_lim, c='g', lw=2)
g.ax_joint.axhline(bottom_lim, c='g', lw=2)
ax_joint_2 = g.ax_joint.twinx()
sns.kdeplot(temp.x, shade=True, color='green', ax=ax_joint_2, legend=False)
ax_joint_2.spines['right'].set_visible(False)
ax_joint_2.spines['top'].set_visible(False)
ax_joint_2.yaxis.set_visible(False)
# calculating the StdDev of the band above
S = temp.std()
M = temp.mean()
print("StdDev", S)
print("Mean", M)
bottom_lim+=0.25
top_lim+=0.25
我们将不得不调整 top/bottom 的限制以解决丢失的数据,因为没有数据的切片会引发错误,但是当我 运行 此代码的上限和下限低于 2效果很好。
但如果有更优雅的方式,我一直在寻找reduce/reuse/recycle。