我创建了矩阵x:
import numpy as np
np.random.seed(0)
x = np.random.randint(10, size=(5, 5))
x=array([[5, 0, 3, 3, 7],
[9, 3, 5, 2, 4],
[7, 6, 8, 8, 1],
[6, 7, 7, 8, 1],
[5, 9, 8, 9, 4]])局部最大值索引应如下所示:
array([[0, 1, 2, 2, 4, 4],
[4, 0, 2, 3, 1, 3]])我已经尝试了像下面的代码这样的方法来寻找局部最大值,但我越来越困惑,甚至还没有接近结果。
for i in range(0,5):
A12=np.r_[ x[1:-1][i] > x[:-2][i] , False]
result12= np.where(A12 == False)
result12=np.asarray(result12)
A22=np.r_[ x[i][1:-1][i] < x[2:][i] , True]
result22= np.where(A22 == True)
result22=np.asarray(result22)
print(np.intersect1d(result12, result22))发布于 2020-12-07 00:31:21
在here上有一种方法。如果你检查一下,我相信那会对你有帮助的。我想到的也是你可以把你的二维数组变成一个图像。如下所示:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(0)
x = np.random.randint(10, size=(5, 5))
plt.matshow(x)之后,您可以检查颜色以找到一致性。你也可以试试这个:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
np.random.seed(0)
a = np.random.randint(10, size=(5, 5))
a += a[::-1,:]
fig = plt.figure()
ax2 = fig.add_subplot(122)
# 'nearest' interpolation - faithful but blocky
ax2.imshow(a, interpolation='nearest', cmap=cm.Greys_r)
plt.show()输出将是:like this
https://stackoverflow.com/questions/65170184
复制相似问题