在 numpy 数组中找到很大的差异
Find big differences in numpy array
我有一个 csv 文件,其中包含来自两个 LED 测量的数据。文件中有一些错误会在图表中产生巨大的火花。我想找到发生这种情况的地方。
我有这段代码可以生成两个我绘制的数组。
x625 = np.array(df['LED Group 625'].dropna(axis=0, how='all'))
x940 = np.array(df['LED Group 940'].dropna(axis=0, how='all'))
由于您还没有发布任何数据,我将提供一些人工数据的答案。
因此,在将 pandas 列转换为 numpy
数组后,您可以执行以下操作:
import numpy as np
# some random data. 100 lines and 1 column
x625 = np.random.rand(100,1)
# Assume that the maximum value in `x625` is a spark.
spark = x625.max()
# Find where these spark are in the `x625`
np.where(x625==spark)
#(array([64]), array([0]))
以上表示等于spark
的值位于第0列的第64行。
同样,您可以使用np.where(x625 > any_number_here)
如果您需要创建布尔掩码而不是位置,请使用:
boolean_mask = (x625==spark)
# verify
np.where(boolean_mask)
# (array([64]), array([0]))
编辑 1
您可以使用 numpy.diff()
将所有元素的元素差异放入列表(变量)中。
diffs = np.diff(x625.ravel())
这将在索引 0 中包含 element1-element0 的结果。
如果diffs
中的值在特定索引位置大,则在该位置发生火花。
我有一个 csv 文件,其中包含来自两个 LED 测量的数据。文件中有一些错误会在图表中产生巨大的火花。我想找到发生这种情况的地方。
我有这段代码可以生成两个我绘制的数组。
x625 = np.array(df['LED Group 625'].dropna(axis=0, how='all'))
x940 = np.array(df['LED Group 940'].dropna(axis=0, how='all'))
由于您还没有发布任何数据,我将提供一些人工数据的答案。
因此,在将 pandas 列转换为 numpy
数组后,您可以执行以下操作:
import numpy as np
# some random data. 100 lines and 1 column
x625 = np.random.rand(100,1)
# Assume that the maximum value in `x625` is a spark.
spark = x625.max()
# Find where these spark are in the `x625`
np.where(x625==spark)
#(array([64]), array([0]))
以上表示等于spark
的值位于第0列的第64行。
同样,您可以使用np.where(x625 > any_number_here)
如果您需要创建布尔掩码而不是位置,请使用:
boolean_mask = (x625==spark)
# verify
np.where(boolean_mask)
# (array([64]), array([0]))
编辑 1
您可以使用 numpy.diff()
将所有元素的元素差异放入列表(变量)中。
diffs = np.diff(x625.ravel())
这将在索引 0 中包含 element1-element0 的结果。
如果diffs
中的值在特定索引位置大,则在该位置发生火花。