按行比较数组以推广 argmax 函数
Comparing array row-wise so as to generalize argmax function
使用Python,考虑包含二维数据的数组X
:
X = np.array([x0,y0], ..., [xn,yn])
和三个一维数组 Y_A, Y_B, Y_C
,长度与 X
相同,包含数字。最后考虑 3 个空数组 A,B,C
。如何根据以下伪代码填充这些空数组A,B,C
?
伪代码:
for each i in range(X):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]
store X[i] to array A
else if Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]
store X[i] to array B
else store X[i] to array C
我的努力无效:
for each i in range(len(X)):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]:
A = Y_A
if Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]:
B = Y_B
else:
C = Y_C
也许可以试试这样:
import numpy as np
X = np.random.random((20, 2))
Y_A = np.random.random((20))
Y_B = np.random.random((20))
Y_C = np.random.random((20))
A, B, C = [], [], []
for i in range(X.shape[0]):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]:
A.append(X[i])
elif Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]:
B.append(X[i])
else:
C.append(X[i])
A = np.array(A)
B = np.array(B)
C = np.array(C)
当然,如果它们的长度与 X
相同,您也可以创建空的 numpy 数组并在循环时填充它们。
使用Python,考虑包含二维数据的数组X
:
X = np.array([x0,y0], ..., [xn,yn])
和三个一维数组 Y_A, Y_B, Y_C
,长度与 X
相同,包含数字。最后考虑 3 个空数组 A,B,C
。如何根据以下伪代码填充这些空数组A,B,C
?
伪代码:
for each i in range(X):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]
store X[i] to array A
else if Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]
store X[i] to array B
else store X[i] to array C
我的努力无效:
for each i in range(len(X)):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]:
A = Y_A
if Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]:
B = Y_B
else:
C = Y_C
也许可以试试这样:
import numpy as np
X = np.random.random((20, 2))
Y_A = np.random.random((20))
Y_B = np.random.random((20))
Y_C = np.random.random((20))
A, B, C = [], [], []
for i in range(X.shape[0]):
if Y_A[i] > Y_B[i] and Y_A[i] > Y_C[i]:
A.append(X[i])
elif Y_B[i] > Y_A[i] and Y_B[i] > Y_C[i]:
B.append(X[i])
else:
C.append(X[i])
A = np.array(A)
B = np.array(B)
C = np.array(C)
当然,如果它们的长度与 X
相同,您也可以创建空的 numpy 数组并在循环时填充它们。