我有2个np数组,其中包含区间[0,1]中的值。 我想创建第三个数组,包含最多的#34;自信"值,意味着采用元素,数组中的数字更接近1或0.请考虑以下示例:
[0.7,0.12,1,0.5]
[0.1,0.99,0.001,0.49]
所以我构造的数组将是:
[0.1,0.99,1,0.49]
答案 0 :(得分:4)
你可以试试这个:
c=np.array([a[i] if min(1-a[i],a[i])<min(1-b[i],b[i]) else b[i] for i in range(len(a))])
结果是:
array([ 0.1 , 0.99, 1. , 0.49])
答案 1 :(得分:4)
import numpy as np
A = np.array([0.7,0.12,1,0.5])
B = np.array([0.1,0.99,0.001,0.49])
maxi = np.maximum(A,B)
mini = np.minimum(A,B)
# Find where the maximum is closer to 1 than the minimum is to 0
idx = 1-maxi < mini
maxi*idx + mini*~idx
返回
array([ 0.1 , 0.99, 1. , 0.49])
答案 2 :(得分:2)
说明你的信心的另一种方式&#34;衡量是要问两个数字中的哪一个远离0.5
。也就是说,两个数字x
中的哪一个产生最大的abs(0.5 - x)
。以下解决方案使用原始数组作为列构建2D数组c
。然后我们构造并应用基于abs(0.5 - c)
的布尔掩码:
import numpy as np
a = np.array([0.7,0.12,1,0.5])
b = np.array([0.1,0.99,0.001,0.49])
# Combine
c = np.concatenate((a, b)).reshape((2, len(a))).T
# Create mask
b_or_a = np.asarray(np.argmax(np.abs((0.5 - c)), axis=1), dtype=bool)
mask = np.zeros(c.shape, dtype=bool)
mask[:, 0] = ~b_or_a
mask[:, 1] = b_or_a
# Applt mask
d = c[mask]
print(d) # [ 0.1 0.99 1. 0.49]