【问题标题】:How to detect the peak Values using Python SciPy, getting index error"arrays used as indices must be of integer (or boolean) type"如何使用 Python SciPy 检测峰值,出现索引错误“用作索引的数组必须是整数(或布尔)类型”
【发布时间】:2022-12-13 09:28:51
【问题描述】:

我有速度数据,因为我需要检测阈值大于 20 且谷值大于 0 的值。我使用此代码进行峰值检测,但出现索引错误

import numpy as np
from scipy.signal import find_peaks, find_peaks_cwt
import matplotlib.pyplot as plt
import pandas as pd
import sys
np.set_printoptions(threshold=sys.maxsize)
zero_locs = np.where(x==0)
search_lims = np.append(zero_locs, len(x)) # limits for search area

diff_x = np.diff(x)
diff_x_mapped = diff_x > 0
peak_locs = []
x = np.array([1, 9, 18, 24, 26, 5, 26, 25, 26, 16, 20, 16, 23, 5, 1, 27, 
22, 26, 27, 26, 25, 24, 25, 26, 3, 25, 26, 24, 23, 12, 22, 11, 15, 24, 11, 
26, 26, 26, 24, 25, 24, 24, 22, 22, 22, 23, 24])

for i in range(len(search_lims)-1):
    peak_loc = search_lims[i] + np.where(diff_x_mapped[search_lims[i]:search_lims[i+1]]==0)[0][0]
    if x[peak_loc] > 20:
        peak_locs.append(peak_loc)
fig= plt.figure(figsize=(10,4))
plt.plot(x)
plt.plot(np.array(peak_locs), x[np.array(peak_locs)], "x", color = 'r')

我尝试使用峰值检测算法,它不检测峰值高于 20 的峰值我需要检测 x 值为 0 且峰值为 20 的峰值 expected output: the marked peaks has to be detected 通过运行上面的脚本我得到这个错误

IndexError: arrays used as indices must be of integer (or boolean) type

如何摆脱这个错误任何建议感谢问候

【问题讨论】:

  • 显示完整的回溯。您/我们需要知道错误发生在哪里。你不能在不知道的情况下修复它!也就是说,如果问题出在x[np.array(peak_locs)],那么您需要检查np.array(peak_locs)。那是一个有效的索引数组吗?

标签: python pandas scipy peak-detection


【解决方案1】:

你没有发现峰。 也就是说,len(peak_locs) 为零。

所以你最终得到了这个数组,它的类型默认为 float:

>>> np.array(peak_locs)
array([], dtype=float64)

要解决这个问题? 寻找更多的高峰!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多