与 Python 中的相邻值进行比较
Comparing with adjacent values in Python
我想将每行的最小值与附近的元素进行比较。例如,第一行的最小值出现在 Pe[0,1] 处。我想将此值与 Pe[0,0]、Pe[0,2] 和 Pe[1,1] 进行比较,并找出这三个值中的最小值。其他行也类似。如何编码?
import numpy as np
Pe=np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
Pe_min=Pe.argmin(axis=1)
你可以通过遍历你的行来做到这一点,但这有点乏味,我相信有更聪明的方法:
import numpy as np
a = np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
b = np.zeros((a.shape[0], 2))
for row_n, row in enumerate(a):
# Get row min
b[row_n][0] = np.min(row)
# Get surroundings min
i = np.argmin(row)
near = []
if row_n > 0:
near.append(a[row_n-1][i])
if row_n+1 < b.shape[0]:
near.append(a[row_n+1][i])
if i > 0:
near.append(a[row_n][i-1])
if i+1 < b.shape[1]:
near.append(a[row_n][i+1])
b[row_n][1] = min(near)
print(b)
# array([[0.4630001 , 0.03976944],
# [0.03976944, 0.09043881],
# [0.20944696, 0.40305156]])
您可以通过在上一个和下一个 rows/columns:
import numpy as np
a = np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
r = a.copy()
r[:,:-1] = np.minimum(r[:,:-1],a[:,1:]) # min with next columns
r[:,1:] = np.minimum(r[:,1:],a[:,:-1]) # min with previous columns
r[:-1,:] = np.minimum(r[:-1,:],a[1:,:]) # min with next rows
r[1:,:] = np.minimum(r[1:,:],a[:-1,:]) # min with previous rows
print(r)
[[0.09043881 0.03976944 0.4630001 ]
[0.03976944 0.03976944 0.03976944]
[0.09043881 0.03976944 0.20944696]]
我想将每行的最小值与附近的元素进行比较。例如,第一行的最小值出现在 Pe[0,1] 处。我想将此值与 Pe[0,0]、Pe[0,2] 和 Pe[1,1] 进行比较,并找出这三个值中的最小值。其他行也类似。如何编码?
import numpy as np
Pe=np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
Pe_min=Pe.argmin(axis=1)
你可以通过遍历你的行来做到这一点,但这有点乏味,我相信有更聪明的方法:
import numpy as np
a = np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
b = np.zeros((a.shape[0], 2))
for row_n, row in enumerate(a):
# Get row min
b[row_n][0] = np.min(row)
# Get surroundings min
i = np.argmin(row)
near = []
if row_n > 0:
near.append(a[row_n-1][i])
if row_n+1 < b.shape[0]:
near.append(a[row_n+1][i])
if i > 0:
near.append(a[row_n][i-1])
if i+1 < b.shape[1]:
near.append(a[row_n][i+1])
b[row_n][1] = min(near)
print(b)
# array([[0.4630001 , 0.03976944],
# [0.03976944, 0.09043881],
# [0.20944696, 0.40305156]])
您可以通过在上一个和下一个 rows/columns:
import numpy as np
a = np.array([[0.97300493, 0.4630001 , 0.66754101],
[0.09043881, 0.03976944, 0.64823791],
[0.9530546 , 0.40305156, 0.20944696]])
r = a.copy()
r[:,:-1] = np.minimum(r[:,:-1],a[:,1:]) # min with next columns
r[:,1:] = np.minimum(r[:,1:],a[:,:-1]) # min with previous columns
r[:-1,:] = np.minimum(r[:-1,:],a[1:,:]) # min with next rows
r[1:,:] = np.minimum(r[1:,:],a[:-1,:]) # min with previous rows
print(r)
[[0.09043881 0.03976944 0.4630001 ]
[0.03976944 0.03976944 0.03976944]
[0.09043881 0.03976944 0.20944696]]