在 numpy 一维数组中查找拐点和固定点
Find Inflection and Stationary points in a numpy 1d-array
假设我有以下 numpy 数组:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([11.53333333, 11.86666667, 11.1, 10.66666667, 11.2, 11.3,
11.06666667, 12.06666667, 11.8, 13.03333333, 12.4,
12.33333333, 12.53333333, 13.33333333, 12.43333333, 13., 13.2,
13.76666667, 14.96666667, 19.16666667, 25.1, 32.,
83.33333333, 103.76666667, 110.7, 118.63333333, 129.26666667,
139.06666667, 150.3, 161.53333333, 171.16666667, 184.56666667,
196.6, 210.26666667, 221.63333333, 231.3, 244.16666667,
253.5, 254.66666667, 255., 255., 255., 255.,
255., 255., 255., 255., 255., 255.,
255., 255., 255., 255., 255., 255.,
255., 255., 255., 255., 255.])
plt.plot(x)
plt.show()
这是绘制的输出:
如何轻松获得此图中的转折点?例如,第一个转折点在 x=20 处,另一个转折点在
x=37.
有没有可能把所有的转折点按降序排列,然后我就可以抓取最有特色的3个转折点?
更新:
我有兴趣获得 Inflection Points as well as the Stationary points。 numpy / scipy?
中有简单的解决方案吗
有很多可能的答案 -- 取决于您的实际需要。一种想法是通过采用移动平均线或样条曲线或其他方法来平滑数据,然后采用二阶导数并寻找何时改变符号。这会找到近似的 "inflection points" 或 "turning points"——从字面上看,它会在凹面发生变化时找到。
参见:
- How to smooth a curve in the right way?
- Derivative of an array in python?
我也在找这个答案。这是在 python:
中找到拐点的一种方法
How to find the inflection point in a noisy curve?
拐点为[x0,y0]
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter
def generate_fake_data():
"""Generate data that looks like an example given."""
xs = np.arange(0, 25, 0.05)
ys = - 20 * 1./(1 + np.exp(-(xs - 5.)/0.3))
m = xs > 7.
ys[m] = -20.*np.exp(-(xs - 7.)[m] / 5.)
# add noise
ys += np.random.normal(0, 0.2, xs.size)
return xs, ys
def main():
xs, ys = generate_fake_data()
# smooth out noise
smoothed = gaussian_filter(ys, 3.)
# find the point where the signal goes above the background noise
# level (assumed to be zero here).
base = 0.
std = (ys[xs < 3] - base).std()
m = smoothed < (base - 3. * std)
x0 = xs[m][0]
y0 = ys[m][0]
plt.plot(xs, ys, '.')
plt.plot(xs, smoothed, '-')
plt.plot(x0, y0, 'o')
plt.show()
if __name__ == '__main__':
main()
Example output of how to find inflection point in python
假设我有以下 numpy 数组:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([11.53333333, 11.86666667, 11.1, 10.66666667, 11.2, 11.3,
11.06666667, 12.06666667, 11.8, 13.03333333, 12.4,
12.33333333, 12.53333333, 13.33333333, 12.43333333, 13., 13.2,
13.76666667, 14.96666667, 19.16666667, 25.1, 32.,
83.33333333, 103.76666667, 110.7, 118.63333333, 129.26666667,
139.06666667, 150.3, 161.53333333, 171.16666667, 184.56666667,
196.6, 210.26666667, 221.63333333, 231.3, 244.16666667,
253.5, 254.66666667, 255., 255., 255., 255.,
255., 255., 255., 255., 255., 255.,
255., 255., 255., 255., 255., 255.,
255., 255., 255., 255., 255.])
plt.plot(x)
plt.show()
这是绘制的输出:
如何轻松获得此图中的转折点?例如,第一个转折点在 x=20 处,另一个转折点在 x=37.
有没有可能把所有的转折点按降序排列,然后我就可以抓取最有特色的3个转折点?
更新: 我有兴趣获得 Inflection Points as well as the Stationary points。 numpy / scipy?
中有简单的解决方案吗有很多可能的答案 -- 取决于您的实际需要。一种想法是通过采用移动平均线或样条曲线或其他方法来平滑数据,然后采用二阶导数并寻找何时改变符号。这会找到近似的 "inflection points" 或 "turning points"——从字面上看,它会在凹面发生变化时找到。
参见:
- How to smooth a curve in the right way?
- Derivative of an array in python?
我也在找这个答案。这是在 python:
中找到拐点的一种方法How to find the inflection point in a noisy curve?
拐点为[x0,y0]
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter
def generate_fake_data():
"""Generate data that looks like an example given."""
xs = np.arange(0, 25, 0.05)
ys = - 20 * 1./(1 + np.exp(-(xs - 5.)/0.3))
m = xs > 7.
ys[m] = -20.*np.exp(-(xs - 7.)[m] / 5.)
# add noise
ys += np.random.normal(0, 0.2, xs.size)
return xs, ys
def main():
xs, ys = generate_fake_data()
# smooth out noise
smoothed = gaussian_filter(ys, 3.)
# find the point where the signal goes above the background noise
# level (assumed to be zero here).
base = 0.
std = (ys[xs < 3] - base).std()
m = smoothed < (base - 3. * std)
x0 = xs[m][0]
y0 = ys[m][0]
plt.plot(xs, ys, '.')
plt.plot(xs, smoothed, '-')
plt.plot(x0, y0, 'o')
plt.show()
if __name__ == '__main__':
main()
Example output of how to find inflection point in python