gpt4 book ai didi

python - Numpy:当前行除以前一行

转载 作者:行者123 更新时间:2023-12-01 01:12:11 24 4
gpt4 key购买 nike

在我的实验中,我有以下格式的三种不同的时间序列数据,具有不同的特征,其中第一列是时间戳,第二列是值。

0.086206438,10
0.086425551,12
0.089227066,20
0.089262508,24
0.089744425,30
0.090036815,40
0.090054172,28
0.090377569,28
0.090514071,28
0.090762872,28
0.090912691,27

为了重现性,我分享了我正在使用的三个时间序列数据 here

从第 2 列,我想读取当前行并将其与前一行的值进行比较。如果更大的话,我会继续比较。如果当前值小于前一行的值,我想将当前值(较小)除以前一个值(较大)。让我说清楚。例如,在我提供的上述示例记录中,第七行 (28) 小于第六行 (40) 中的值 - 因此将为 (28/40=0.7)。

这是我的示例代码。

import numpy as np
import pandas as pd
import csv
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
from statsmodels.graphics.tsaplots import plot_acf, acf


protocols = {}


types = {"data1": "data1.csv", "data2": "data2.csv", "data3": "data3.csv"}

for protname, fname in types.items():
col_time = []
col_window = []
with open(fname, mode='r', encoding='utf-8-sig') as f:
reader = csv.reader(f, delimiter=",")
for i in reader:
col_time.append(float(i[0]))
col_window.append(int(i[1]))
col_time, col_window = np.array(col_time), np.array(col_window)
diff_time = np.diff(col_time)
diff_window = np.diff(col_window)
diff_time = diff_time[diff_window > 0]
diff_window = diff_window[diff_window > 0] # To keep only the increased values
protocols[protname] = {
"col_time": col_time,
"col_window": col_window,
"diff_time": diff_time,
"diff_window": diff_window,
}


# Plot the quotient values
rt = np.exp(np.diff(np.log(col_window)))

for protname, fname in types.items():
col_time, col_window = protocols[protname]["col_time"], protocols[protname]["col_window"]
rt = np.exp(np.diff(np.log(col_window)))
plt.plot(np.diff(col_time), rt, ".", markersize=4, label=protname, alpha=0.1)
plt.ylim(0, 1.0001)
plt.xlim(0, 0.003)
plt.title(protname)
plt.xlabel("time")
plt.ylabel("difference")
plt.legend()
plt.show()

这给了我以下情节

enter image description here

enter image description here enter image description here

但是,当我这样做时

rt = np.exp(np.diff(np.log(col_window)))

它将每个当前行除以前一行,这不是我想要的。正如我在问题中用上面的示例所解释的那样,仅当当前行值小于前一个值时,我才想将第 2 列的当前行值除以第 2 列的前一个值。最后,根据时间戳差异绘制商(上面代码中的 col_time)。我怎样才能解决这个问题?

最佳答案

除非您特别需要 csv 模块,否则我建议使用 numpy method loadtxt加载您的文件,即

col_time,col_window = np.loadtxt(fname,delimiter=',').T

这一行负责处理 for 循环的前 8 行。请注意,转置操作 (.T) 是将原始数据形状(N 行乘 2 列)转换为 2 所必需的 行乘 N 列形状,被解压缩为 col_timecol_window。另请注意,loadtxt 自动将数据加载到 numpy.array 对象中。

至于你的实际问题,我会使用切片和屏蔽:

trailing_window = col_window[:-1] # "past" values at a given index
leading_window = col_window[1:] # "current values at a given index
decreasing_mask = leading_window < trailing_window
quotient = leading_window[decreasing_mask] / trailing_window[decreasing_mask]
quotient_times = col_time[decreasing_mask]

然后可以根据quotient绘制quotient_times

另一种方法是使用 numpy method where获取掩码为 True 的索引:

trailing_window = col_window[:-1] # "past" values at a given index
leading_window = col_window[1:] # "current values at a given index
decreasing_inds = np.where(leading_window < trailing_window)[0]
quotient = leading_window[decreasing_inds] / trailing_window[decreasing_inds]
quotient_times = col_time[decreasing_inds]

请记住,上述所有代码仍然发生在第一个 for 循环中,但现在 rt 在循环内计算为 quotient.因此,在计算 quotient_times 后,进行绘图(也在第一个循环内):

# Next line opens a new figure window and then clears it
figure(); clf()
# Updated plotting call with the syntax from the answer
plt.plot(quotient_times,quotient,'.',ms=4,label=protname,alpha=0.1)
plt.ylim(0, 1.0001)
plt.xlim(0, 0.003)
plt.title(protname)
plt.xlabel("time")
plt.ylabel("quotient")
plt.legend()
# You may not need this `plt.show()` line
plt.show()
# To save the figure, one option would be the following:
# plt.savefig(protname+'.png')

请注意,您可能需要将 plt.show() 行从循环中取出。

给你整理一下,

import numpy as np
import matplotlib.pyplot as plt

protocols = {}

types = {"data1": "data1.csv", "data2": "data2.csv", "data3": "data3.csv"}

for protname, fname in types.items():
col_time,col_window = np.loadtxt(fname,delimiter=',').T
trailing_window = col_window[:-1] # "past" values at a given index
leading_window = col_window[1:] # "current values at a given index
decreasing_inds = np.where(leading_window < trailing_window)[0]
quotient = leading_window[decreasing_inds] /
trailing_window[decreasing_inds]
quotient_times = col_time[decreasing_inds]
# Still save the values in case computation needs to happen later
# in the script
protocols[protname] = {
"col_time": col_time,
"col_window": col_window,
"quotient_times": quotient_times,
"quotient": quotient,
}
# Next line opens a new figure window and then clears it
plt.figure(); plt.clf()
plt.plot(quotient_times,quotient, ".", markersize=4, label=protname, alpha=0.1)
plt.ylim(0, 1.0001)
plt.xlim(0, 0.003)
plt.title(protname)
plt.xlabel("time")
plt.ylabel("quotient")
plt.legend()
# To save the figure, one option would be the following:
# plt.savefig(protname+'.png')
# This may still be unnecessary, especially if called as a script
# (just save the plots to `png`).
plt.show()

关于python - Numpy:当前行除以前一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com