假设我在NumPy中有一个包含连续可微函数评估的数组,我想找到局部最小值.没有噪音,所以每个点的值低于其所有邻居的值都符合我的局部最小值的标准.
我有以下列表理解,适用于二维数组,忽略边界上的潜在最小值:
import numpy as N
def local_minima(array2d):
local_minima = [ index
for index in N.ndindex(array2d.shape)
if index[0] > 0
if index[1] > 0
if index[0] < array2d.shape[0] - 1
if index[1] < array2d.shape[1] - 1
if array2d[index] < array2d[index[0] - 1, index[1] - 1]
if array2d[index] < array2d[index[0] - 1, index[1]]
if array2d[index] < array2d[index[0] - 1, index[1] + 1]
if array2d[index] < array2d[index[0], index[1] - 1]
if array2d[index] < array2d[index[0], index[1] + 1]
if array2d[index] < array2d[index[0] + …Run Code Online (Sandbox Code Playgroud) 我有两个数组x,y如下:
x = np.array([6, 3, 5, 2, 1, 4, 9, 7, 8])
y = np.array([2, 1, 3, 5, 3, 9, 8, 10, 7])
Run Code Online (Sandbox Code Playgroud)
我发现当地最小值和最大值的索引如下:
sortId = np.argsort(x)
x = x[sortId]
y = y[sortId]
minm = np.array([])
maxm = np.array([])
while i < y.size-1:
while(y[i+1] >= y[i]):
i = i + 1
maxm = np.insert(maxm, 0, i)
i++
while(y[i+1] <= y[i]):
i = i + 1
minm = np.insert(minm, 0, i)
i++
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?答案应该是索引minima = [2, 5, …
我想有一个函数可以检测数组中局部最大值/最小值的位置(即使有一组局部最大值/最小值).例:
鉴于阵列
test03 = np.array([2,2,10,4,4,4,5,6,7,2,6,5,5,7,7,1,1])
Run Code Online (Sandbox Code Playgroud)
我想有一个输出像:
set of 2 local minima => array[0]:array[1]
set of 3 local minima => array[3]:array[5]
local minima, i = 9
set of 2 local minima => array[11]:array[12]
set of 2 local minima => array[15]:array[16]
Run Code Online (Sandbox Code Playgroud)
从示例中可以看出,不仅检测到奇异值,还检测局部最大值/最小值.
我知道在这个问题中有很多好的答案和想法,但是他们都没有完成所描述的工作:他们中的一些人只是忽略了数组的极端点而忽略了局部最小值/最大值的集合.
在提出这个问题之前,我自己编写了一个函数,它完全按照上面的描述进行(函数在这个问题的最后:local_min(a).通过我做的测试,它可以正常工作).
问题:但是,我也确信这不是使用Python的最佳方式.我可以使用内置函数,API,库等吗?还有其他功能建议吗?一行指令?一个完整的矢量解决方案?
def local_min(a):
candidate_min=0
for i in range(len(a)):
# Controlling the first left element
if i==0 and len(a)>=1:
# If the first element is a singular local minima
if a[0]<a[1]:
print("local minima, i …Run Code Online (Sandbox Code Playgroud) 
操作员用于检查光谱,知道每个峰的位置和宽度,并判断光谱所属的部分.以新的方式,图像由相机捕获到屏幕.并且必须以编程方式计算每个波段的宽度.
旧系统:分光镜 - >人眼新系统:分光镜 - >摄像头 - >程序
计算每个波段宽度的好方法是什么,给出它们近似的X轴位置; 鉴于此任务过去通过眼睛完美地执行,现在必须由程序执行?
对不起,如果我缺乏细节,但他们很少.
生成上一个图表的程序列表; 我希望它是相关的:
import Image
from scipy import *
from scipy.optimize import leastsq
# Load the picture with PIL, process if needed
pic = asarray(Image.open("spectrum.jpg"))
# Average the pixel values along vertical axis
pic_avg = pic.mean(axis=2)
projection = pic_avg.sum(axis=0)
# Set the min value to zero for a nice fit
projection /= projection.mean()
projection -= projection.min()
#print projection
# Fit function, two gaussians, adjust as needed
def …Run Code Online (Sandbox Code Playgroud) 我是信号处理的新手,在这个问题中,我想问一下如何为感兴趣的频率F周围的每个频段获取能量.我找到了一个公式,但我不知道如何在Python中实现它.这是公式和我的傅里叶变换图:

x = np.linspace(0,5,100)
y = np.sin(2*np.pi*x)
## fourier transform
f = np.fft.fft(y)
## sample frequencies
freq = np.fft.fftfreq(len(y), d=x[1]-x[0])
plt.plot(freq, abs(f)**2) ## will show a peak at a frequency of 1 as it should.
Run Code Online (Sandbox Code Playgroud)

我希望找到一些高斯平滑数据中的峰值.我已经研究了一些可用的峰值检测方法,但它们需要一个输入范围来搜索,我希望它比这更加自动化.这些方法也适用于非平滑数据.由于我的数据已经过平滑,我需要一种更简单的方法来检索峰值.我的原始数据和平滑数据如下图所示.
从本质上讲,是否有一种pythonic方法从平滑数据数组中检索最大值,以便像数组一样
a = [1,2,3,4,5,4,3,2,1,2,3,2,1,2,3,4,5,6,5,4,3,2,1]
Run Code Online (Sandbox Code Playgroud)
会回来:
r = [5,3,6]
Run Code Online (Sandbox Code Playgroud) 在Python中,如何计算直方图的峰值?
我尝试了这个:
import numpy as np
from scipy.signal import argrelextrema
data = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 1, 2, 3, 4,
5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9,
12,
15, 16, 17, 18, 19, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24,]
h = np.histogram(data, bins=[0, 5, 10, 15, 20, 25])
hData = h[0]
peaks = argrelextrema(hData, np.greater)
Run Code Online (Sandbox Code Playgroud)
但是结果是:
(array([3]),) …Run Code Online (Sandbox Code Playgroud) 所以我一直在互联网上查找,但我无法找到如何在没有 for 循环 + if 语句的情况下做到这一点。假设我有这个作为我的数组:
import numpy as np
a = np.array([361, 362, 363, 364, 365, 0, 1, 2, 366, 367])
Run Code Online (Sandbox Code Playgroud)
我想找出第一个最高值(无论将来是否有另一个更高的值),在本例中为365. 这就是我在不使用 numpy 的情况下执行此操作的方法:
import numpy as np
a = np.array([361, 362, 363, 364, 365, 0, 1, 2, 366, 367])
for element in range(a.shape[0]-1):
if a[element+1] < a[element]:
first_max = a[element]
break
print(first_max)
# 365
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 numpy 函数来做到这一点?
我想从数组或列表中找到局部最小值。通过以下代码,我可以找到局部最大值。我知道存在相关问题,但我仍然想知道,是否存在任何逻辑可以使用相同的代码来查找局部最小值。
代码:
import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks
x = electrocardiogram()[2000:4000]
peaks, _ = find_peaks(x, height=0)
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.plot(np.zeros_like(x), "--", color="gray")
plt.show()
Run Code Online (Sandbox Code Playgroud) 给定大量波动值,如何确定所有局部最小值?不使用numpy。局部最小值是指列表中作为函数波谷的所有值。
List_y = [23, 8, -7, 57, 87, 6]
Run Code Online (Sandbox Code Playgroud)
我想:
New_list = [-7, 6]
Run Code Online (Sandbox Code Playgroud) python ×11
numpy ×6
arrays ×4
algorithm ×1
derivative ×1
distribution ×1
kinect ×1
list ×1
physics ×1
python-3.x ×1
scipy ×1