NumPy的。将一个数组中的所有向量行与同一数组中的每个另一个向量进行比较

时间:2017-07-23 09:03:13

标签: python numpy

我有一个numpy数组如下:

a= array([[2, 3],
   [0, 2]])

并希望使用np.greater将每行中的“向量”与其他行进行比较,以便:

array([[False, False],  <--- [2,3] compared with [2,3]
     [True, True]],     <--- [2,3] compared with [0,2]
     [[False, False],   <--- [0,2] compared with [2,3] 
     [False, False]])   <--- [0,2] compared with [0,2]

但是,如果我尝试r=(np.greater.outer(a,a)),它会将a中的每个数字与a中的每个其他数字进行比较,以便:{/ p>

array([[[[False, False],  <--- 2 compared with a
     [ True, False]],     

    [[ True, False],      <--- 3 compared with a
     [ True,  True]]],


   [[[False, False],      <--- 0 compared with a
     [False, False]],

    [[False, False],      <--- 2 compared with a 
     [ True, False]]]], dtype=bool)

¿我怎样才能使外部比较行明智而不是元素呢?

1 个答案:

答案 0 :(得分:4)

试试这个:

let shareButton = ShareButton<LinkShareContent>()
let content = LinkShareContent(url: URL(string: "https://stackoverflow.com/a/45263496/1724845")!)
shareButton.content = content
shareButton.frame = CGRect(x: 50.0, y: 25.0, width: 200, height: 40)
view.addSubview(shareButton)

这与更明确的代码相同:

a[:,np.newaxis] > a

帽子提示https://stackoverflow.com/a/16500609/4323

详细信息:The use of Python numpy.newaxis