python - Smooth Data and Find Maximum -
i have dataset (see below) of 2 variables, x , y. want find value of x, maximum in y occur. current approach x gives me maximum y. not ideal data quite noisy, perform sort of smoothing first, , find max.
so far, have tried use r smooth data npreg
(kernel regression) np
package obtain curve:
but i'm not sure how find max.
i solution following in python:
1) smooth data (doesn't kernel regression)
2) find value of x max in y occurs using smoothed data
x y -20 0.006561733 -19 -4.48e-08 -18 -4.48e-08 -17 -4.48e-08 -16 0.003281305 -15 0.00164063 -14 0.003280565 -13 0.003282537 -12 -4.48e-08 -11 0.003281286 -10 0.004921239 -9 0.00491897 -8 -1.52e-06 -7 0.004925867 -6 -1.27e-06 -5 0.009839438 -4 0.001643726 -3 -4.48e-08 -2 2.09e-06 -1 -0.001640027 0 0.006559627 1 0.001636958 2 2.36e-06 3 0.003281469 4 0.011481469 5 0.004922279 6 0.018044207 7 0.011483134 8 0.014765087 9 0.008201379 10 0.00492497 11 0.006560482 12 0.009844796 13 0.011483199 14 0.008202129 15 0.001641621 16 0.004921645 17 0.006563377 18 0.006561068 19 0.008201004
i'd run gaussian filter on data smooth:
# first, make function linearly interpolate data f = scipy.interpolate.interp1d(x,y) # resample 1000 samples xx = np.linspace(-20,19, 1000) # compute function on finer interval yy = f(xx) # make gaussian window window = scipy.signal.gaussian(200, 60) # convolve arrays smoothed = scipy.signal.convolve(yy, window/window.sum(), mode='same') # maximum xx[np.argmax(smoothed)]
here's smoothed result:
the max occurs @ 6.93.
there whole bunch of other window functions , filtering options in scipy.signal
. see documentation more.
Comments
Post a Comment