gpt4 book ai didi

python - 如何从已经确定的峰/谷指数组中获取极值? [Python]

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:15 25 4
gpt4 key购买 nike

如果我在扼杀这个问题,我很抱歉,但本质上我有 3 个数组。一个数组是信号数据,以及一组峰值索引和一组谷值索引。然而问题是,有时在出现谷值索引之前,连续的峰值会有多个索引。我的目标是,对于这样的情况,我只想要最极端事件的索引。

对于这个特定问题,您可以假设我已经有一种选择峰值的方法,我只需要找到一种方法从已选择的峰值候选中选择最佳峰值。例如,该函数需要将这些索引数组作为参数,否则它有点错过了我的特定问题的要点。

例如,假设我的信号数组是 [1,2,0,1,0,-1,-2,0,2,1]

那么我的峰值索引数组是[0,1,3,8,9]我的山谷索引数组是 [5,4]

理想情况下,我希望峰数组的输出为 [1,8],谷数组的输出为 [6]

我想我想以某种方式转置索引,替换为实际值,合并它们,但是当峰值组转到谷组时使用某种分隔符来分隔组。然后也许对组进行 argmax 以获得最佳索引。

计算速度也是这里的一个因素,所以也许 numpy 中的东西会是最好的?

编辑:这是我希望它起作用的示例数据

arr = np.array([7441.5, 7445.12, 7446.17, 7441.81, 7441.56, 7441.2, 7440.33, 7446.93, 7448.07, 7442.92, 7448.42, 7450.12, 7460.35, 7456.22, 7450.71, 7449.74, 7445.41, 7447.37, 7444.9, 7442.76, 7440.11, 7445.36, 7440.48, 7443.32, 7443.03, 7443.97, 7445.42, 7444.48, 7442.95, 7439.68, 7441.68, 7443.33, 7444.03, 7447.52, 7443.99, 7443.47, 7444.29, 7448.01, 7447.5, 7451.22, 7450.66, 7451.64, 7450.49, 7449.06, 7447.65, 7451.92, 7450.0, 7451.57, 7450.64, 7450.0, 7448.27, 7448.64, 7448.19, 7445.09, 7442.06, 7443.49, 7440.62, 7438.81, 7438.92, 7443.25, 7443.49, 7445.81, 7440.87, 7442.09, 7441.48, 7443.05, 7445.15, 7443.78, 7442.85, 7444.33, 7443.58, 7446.96, 7454.49, 7453.25, 7453.93, 7452.53, 7448.32, 7448.84, 7448.66, 7450.12, 7448.78, 7447.28, 7447.81, 7447.54, 7449.03, 7453.83, 7457.14, 7457.3, 7456.27, 7456.87, 7459.0, 7462.51, 7463.2, 7464.8, 7462.19, 7458.84, 7461.06, 7462.22, 7458.29, 7456.01, 7456.39, 7452.45, 7451.59, 7450.89, 7448.07, 7447.12, 7448.3, 7448.51, 7451.03, 7449.42, 7451.51, 7451.01, 7450.64, 7451.77, 7451.62, 7456.78, 7455.0, 7456.56, 7456.02, 7456.87, 7457.06, 7456.12, 7454.63, 7454.1, 7456.45, 7457.54, 7458.9, 7462.79, 7464.2, 7464.9, 7465.66, 7466.22, 7463.78, 7467.25, 7468.45, 7470.34, 7472.76, 7469.48, 7468.66, 7461.69, 7459.6, 7455.01, 7453.74, 7457.08, 7454.99, 7455.0, 7454.07, 7451.0, 7447.45, 7449.14, 7447.6, 7448.79, 7456.93, 7452.14, 7451.43, 7451.71, 7450.16, 7456.18, 7453.77, 7455.0, 7455.73, 7461.42, 7464.22, 7464.39, 7468.93, 7464.26, 7460.81, 7459.01, 7455.22, 7455.86, 7454.82, 7452.06, 7448.3, 7448.0, 7447.06, 7447.66, 7449.25, 7449.99, 7450.78, 7451.0, 7449.13, 7447.86, 7446.52, 7445.18, 7447.52, 7445.63, 7445.53, 7444.61, 7444.1, 7443.06, 7442.24, 7449.86, 7451.68, 7449.78, 7456.82, 7452.73, 7450.21, 7451.17, 7447.99, 7447.58, 7456.91, 7454.96, 7455.0, 7452.01, 7446.36, 7450.3, 7450.66, 7457.03, 7452.5, 7456.81, 7454.02, 7455.25, 7450.81, 7447.67, 7450.42, 7453.24, 7451.37, 7447.56, 7443.17, 7444.71, 7449.08, 7447.15, 7446.78, 7444.62, 7449.17, 7447.11, 7447.43, 7446.39, 7445.45, 7446.13, 7448.75, 7446.45, 7447.36, 7446.71, 7450.9, 7448.62, 7453.63, 7451.32, 7452.14, 7451.62, 7451.27, 7448.85, 7447.09, 7447.95, 7447.59, 7446.91, 7446.23, 7450.8, 7459.14, 7455.11, 7450.01, 7450.68, 7453.6, 7456.19, 7456.5, 7456.12, 7449.64, 7454.07, 7453.06, 7451.14, 7450.35, 7452.72, 7454.74, 7456.4, 7455.68, 7461.65, 7465.55, 7464.14, 7467.1, 7469.03, 7468.09, 7462.62, 7461.85, 7465.76, 7467.41, 7471.29, 7494.03, 7482.29, 7507.32, 7512.99, 7518.08, 7514.59, 7499.95, 7502.13, 7502.36, 7492.58, 7484.07, 7490.81, 7485.87, 7492.0, 7503.42, 7508.07, 7518.99, 7537.78, 7526.03, 7540.8, 7565.91, 7575.0, 7548.14, 7566.46, 7587.75, 7569.83, 7590.92, 7586.58, 7594.67, 7573.29, 7577.17, 7583.92, 7588.55, 7586.7, 7579.3, 7575.32, 7568.09, 7579.52, 7588.03, 7583.27, 7584.99, 7590.74, 7600.96, 7597.08, 7593.53, 7604.15, 7611.99, 7613.49, 7633.72, 7620.0, 7624.71, 7618.0, 7611.25, 7618.86, 7613.47, 7605.01, 7600.88, 7607.51, 7604.82, 7610.15, 7611.83, 7608.05, 7597.32, 7600.03, 7602.1, 7598.03, 7592.9, 7601.34, 7598.72, 7589.16, 7592.57, 7586.38, 7587.19, 7594.99, 7595.58, 7594.28, 7592.7, 7594.52, 7588.63, 7592.0, 7590.53, 7593.77, 7593.68, 7587.04, 7586.54, 7584.22, 7585.25, 7583.78, 7577.43, 7574.7, 7568.03, 7581.1, 7588.0, 7591.91, 7596.84, 7601.0, 7604.74, 7605.64, 7599.75, 7595.06, 7606.26, 7615.0, 7610.27, 7613.05, 7610.5, 7608.57, 7614.09, 7602.72, 7600.02, 7605.85, 7613.8, 7605.11, 7607.28, 7609.91, 7608.84, 7610.44, 7611.0, 7627.0, 7625.52, 7630.95, 7631.58, 7622.78, 7631.74, 7630.12, 7636.2, 7641.72, 7641.22, 7631.24, 7634.83, 7629.6, 7628.88, 7622.34, 7623.94, 7621.13, 7621.12, 7622.66, 7624.69, 7620.02, 7621.7, 7616.28, 7619.33, 7617.79, 7618.99, 7626.87, 7631.58, 7634.12, 7636.67, 7632.5, 7629.6, 7625.02, 7623.06, 7619.04, 7633.73, 7631.13, 7629.81, 7624.33, 7629.22, 7635.73, 7632.38, 7627.79, 7612.69, 7618.51, 7617.99, 7622.68, 7617.61, 7616.49, 7614.0, 7618.02, 7624.99, 7624.11, 7623.68, 7618.75, 7620.05, 7619.89, 7618.89, 7625.29, 7624.36, 7625.39, 7623.69, 7623.75, 7629.52, 7630.17, 7629.06, 7628.95, 7631.44, 7633.58, 7635.21, 7633.25, 7631.77, 7630.02, 7625.27, 7625.07, 7627.5, 7628.34, 7629.85, 7632.71, 7633.0, 7633.06, 7638.81, 7644.12, 7660.0, 7655.18, 7664.29, 7670.01, 7667.35, 7668.54, 7682.48, 7679.2, 7669.93, 7679.79, 7676.21, 7668.34, 7671.45, 7678.32, 7686.23, 7687.47, 7681.56, 7670.71, 7681.12, 7675.63, 7665.06, 7653.43, 7653.23, 7658.87, 7664.59, 7668.87, 7664.52, 7663.67, 7669.31, 7666.76, 7663.56, 7659.05, 7661.13, 7660.02, 7663.35, 7657.97, 7654.0, 7646.29, 7646.19, 7646.0, 7650.0, 7643.65, 7652.91, 7658.65, 7655.71, 7662.92, 7667.63, 7670.65, 7666.03, 7659.09, 7660.0, 7664.31, 7662.66, 7663.63, 7662.33, 7665.96, 7670.49, 7676.31, 7665.48, 7663.99, 7658.61, 7664.22, 7658.9])
peaks = np.array([1, 2, 7, 8, 10, 11, 12, 13, 14, 15, 17, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 72, 73, 74, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 90, 91, 92, 93, 94, 95, 96, 97, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 143, 152, 157, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 194, 200, 201, 202, 207, 208, 209, 210, 211, 215, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 194, 278, 279, 280, 281, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 280, 293, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 343, 344, 318, 319, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 377, 379, 382, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 411, 412, 419, 420, 421, 422, 423, 424, 428, 429, 430, 432, 433, 434, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 420, 421, 422, 423, 428, 433, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 500, 501, 502, 503, 504, 505, 506, 523, 533])
valleys = np.array([5, 6, 19, 20, 22, 23, 24, 28, 29, 30, 31, 32, 34, 35, 36, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 76, 77, 78, 80, 81, 82, 83, 84, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 123, 141, 142, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 77, 78, 79, 80, 81, 82, 83, 84, 85, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 147, 148, 149, 150, 151, 154, 155, 156, 172, 173, 174, 175, 176, 177, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 198, 199, 204, 213, 217, 218, 219, 221, 222, 223, 225, 226, 227, 228, 229, 213, 214, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 286, 287, 288, 289, 242, 243, 244, 245, 246, 247, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 309, 310, 311, 312, 313, 314, 315, 316, 342, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 374, 375, 383, 384, 385, 387, 388, 436, 437, 438, 440, 441, 442, 443, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 385, 387, 388, 389, 390, 391, 392, 415, 416, 417, 418, 426, 427, 431, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 512, 513, 514, 515, 516, 517, 518, 519, 520, 525, 526, 528, 530, 535, 536, 537, 538])

最佳答案

import numpy as np

def get_values_from_peak_valley(arr, peak_indecies, valley_indecies):

peak_vals = arr[peak_indecies]
valley_vals = arr[valley_indecies]

# https://stackoverflow.com/a/17568803/12164878
max_peak_indecies = np.argwhere(peak_vals == peak_vals.max()).flatten().tolist()
arr_peak_indecies = peak_indecies[max_peak_indecies]

max_valley_indecies = np.argwhere(valley_vals == valley_vals.min()).flatten().tolist()
arr_valley_indecies = valley_indecies[max_valley_indecies]

return (arr_peak_indecies, arr_valley_indecies)


arr = np.array([1,2,0,1,0,-1,-2,0,2,1])
peak_indecies = np.array([0,1,3,8,9]) # want [1,8]
valley_indecies = np.array([5,6]) # want [6]

print(get_values_from_peak_valley(arr, peak_indecies, valley_indecies))
# (array([1, 8]), array([6]))

我可以说很多关于需要这种类型的函数说明了程序的性质(这对我来说是一种代码味道),但我很欣赏在某些情况下这很有用。对 flatten().tolist() 业务不是特别满意,也许有人有更好的方法来做到这一点,但是 argmax只返回第一个索引。如果您同意的话,argmax 会干净得多。

关于python - 如何从已经确定的峰/谷指数组中获取极值? [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58559585/

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