我创建了矩阵 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))
如果您检查它,我相信会对您有所帮助。我想到的是你也可以将你的二维数组变成图像。像这样:
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()