[英]Python 3.x Finding saddle points in a matrix
This is my matrix :这是我的矩阵:
1 1 2 5 6 1
5 6 8 5 6 7
10 12 10 12 11 11
8 10 5 6 8 9
6 5 10 12 15 19
and I would like to find it's saddle points .我想找到它的鞍点。
COORDINATES of Saddle points should be:鞍点的坐标应该是:
2 0
2 2
0 4
So my question is.所以我的问题是。 Can someone show me, how to make this in Python?
有人可以告诉我,如何用 Python 做这个吗? :)
:)
Here is a Python approach that assembles lists of the indices of all row/column mins and maxs and then uses set operations to find their intersections:这是一种 Python 方法,它组装所有行/列最小值和最大值的索引列表,然后使用集合操作来查找它们的交集:
def allSaddles(matrix):
rowmins = []
rowmaxs = []
colmins = []
colmaxs = []
for i,row in enumerate(matrix):
m = min(row)
M = max(row)
for j,x in enumerate(row):
if x == m: rowmins.append((i,j))
if x == M: rowmaxs.append((i,j))
t = [list(column) for column in zip(*matrix)] #transpose of matrix
for j,col in enumerate(t):
m = min(col)
M = max(col)
for i,x in enumerate(col):
if x == m: colmins.append((i,j))
if x == M: colmaxs.append((i,j))
return (set(rowmins) & set(colmaxs)) | (set(rowmaxs) & set(colmins))
M = [[1,1,2,5,6,1],
[5,6,8,5,6,7],
[10,12,10,12,11,11],
[8,10,5,6,8,9],
[6,5,10,12,15,19]]
print(allSaddles(M))
Output: {(0, 4), (2, 0), (2, 2)}
输出:
{(0, 4), (2, 0), (2, 2)}
here is my saddle point finder in python这是我在 python 中的鞍点查找器
import numpy as np
def is_saddle(row, column, *args):
index = 0
size = row * column
matrix = np.arange(size).reshape(row, column)
for i in range(row):
for j in range(column):
matrix[i][j] = args[0][index]
index = index + 1
print(matrix)
for item_row in matrix:
column_number = 0
min_item = np.amin(item_row)
for i in range(len(item_row)):
if min_item == item_row[i]:
row_number = i
reversed_matrix = matrix.transpose()
max_item = np.amax(reversed_matrix[row_number])
if max_item == min_item:
print("saddle point found : {}".format(min_item))
return matrix
row = input("enter row")
column = input("enter column")
matrix = input("enter the matrix items")
is_saddle(row, column, matrix)
input sample: row = 2, column = 3, matrix = (1,2,3,4,5,6)
输入样本:行 = 2,列 = 3,矩阵 = (1,2,3,4,5,6)
Here is a different way to make it.这是一种不同的制作方法。 Note that the input matrix is a
np.ndarray
:请注意,输入矩阵是
np.ndarray
:
def saddles(mat : np.ndarray) -> list:
"""
returns the list of all saddle points of the input matrix
"""
(N, M) = mat.shape
jMax = np.argmax(mat, axis = 1) # index of col for max in each row
iMin = np.argmin(mat, axis = 0) # index of row for min in each col
IJMax = [(i,jMax[i]) for i in range(N)] # list of indexes of max of each row
IJMin = [(iMin[j],j) for j in range(M)] # list of indexes of min of each col
maxRowMinCol = list(set(IJMax) & set(IJMin)) # max of row, min of col
iMax = np.argmax(mat, axis = 0) # index of row for max in each col
jMin = np.argmin(mat, axis = 1) # index of col for min in each row
IJMax = [(iMax[j],j) for j in range(M)] # list of indexes of max of each col
IJMin = [(i,jMin[i]) for i in range(N)] # list of indexes of min of each row
minRowMaxCol = list(set(IJMax) & set(IJMin)) # min of row, max of col
return maxRowMinCol + minRowMaxCol
I have a different approach, looking for saddle points which are local, based on a few auxiliary 2d arrays:我有一种不同的方法,基于一些辅助二维数组寻找本地的鞍点:
def find_saddle(arr2d):
xdiff = np.diff(arr2d, axis=0) # Derivative along 1st dim
ydiff = np.diff(arr2d, axis=1) # Derivative along 2nd dim
# Saddle points are not in the "boundary" elements
xdiff1 = xdiff[1: , 1:-1] # Shift matrix skipping 1st row
xdiff2 = xdiff[ :-1, 1:-1] # Cut the last row
ydiff1 = ydiff[1:-1, 1: ] # Same, for columns
ydiff2 = ydiff[1:-1, :-1]
# A saddle points is a local maximum/minimum in one dimension (xdiff1*xdiff2 < 0) and in the other dimension (ydiff1*ydiff2 < 0),
# but only in the combination max1&min2 or min1&max2 (xdiff1*ydiff1 < 0)
ind = np.where((xdiff1*xdiff2 < 0) & (ydiff1*ydiff2 < 0) &
(xdiff1*ydiff1 < 0))
saddle_points = []
if len(ind) > 0:
for j, x in enumerate(ind[0]):
saddle_points.append([x + 1, ind[1][j] + 1])
return saddle_points
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.