Python: how to get local maximum values โ€‹โ€‹from a 1D array or list

I am new to Python and I have a problem that I could not solve with the answers found ... I hope someone can help: I โ€‹โ€‹need to get a list of all the local maxima of the dataset that is imported from the csv file. Values โ€‹โ€‹range from 0 to 0.5 or so.

I just need to get a list of these local maxima of one row of data ("Werte", array or "N", list) in order to make statistics on them.

Here is what I got:

import numpy as np from numpy import * N = [] file = open('C:/Auswertung/PEE/PEE_L_1_O_130702-1.1.csv', 'r') Probe = file.readline() # lese Inhalt zeilenweise in Listen Header = file.readline() data = file.readlines() for row in data: columns = row.split(";") # Trenne Zeilen bei ';' N.append(float(columns[1])) Werte = np.array([N]) # one try here: only gives me a set of 1s... c = (diff(sign(diff(Werte))) < 0).nonzero()[0] + 1 # local max print(c) 

Is there anyone who could help me find the right way to do this? Thank you very much!

+6
source share
2 answers

I think you're looking for argrelmax , from scipy.signal . It gives you the relative maximum indices of the 1d array.

 from scipy.signal import argrelmax t=linspace(-4,40,1000) y=sin(t) argrelmax(y)[0] 

with the result

 [126 269 412 554 697 840 982] 

to get the values โ€‹โ€‹use

 y[argrelmax(y)[0]] 

EDIT:

note that it does not take into account local maxima at the extremes of your domain.

+6
source

You are on the right track. The only thing you need is to slice the Werke array. but I think finding a local max can be simplified to:

  werte[1:-1][(diff(werte)[:-1]>0)*(diff(werte)[1:]<0)] 

@Jamine was absolutely right, & instead of * makes him read better.

+2
source

Source: https://habr.com/ru/post/954180/


All Articles