【问题标题】:python numpy - unable to compare 2 arrayspython numpy - 无法比较2个数组
【发布时间】:2019-07-24 11:55:33
【问题描述】:

我有 2 个数组如下:

x = array(['2019-02-28', '2019-03-01'], dtype=object)
z = array(['2019-02-28', '2019-03-02', '2019-03-01'], dtype=object)

我正在尝试使用 np.where 来确定 2 个矩阵在哪个索引上对齐。

我在做 i = np.where (z == x) 但它不起作用,结果我得到一个空数组。看起来它正在比较整个数组是否等于另一个整个数组,而我正在寻找匹配值并希望在 2 之间获得匹配结果。我应该怎么做?

谢谢

问候

编辑:预期结果是肯定的 [True, False, False]

【问题讨论】:

  • 确切的预期输出是什么?
  • 我假设预期的输出将是 [True, False, False] 或只是 [1]
  • 是的[对,错,错]

标签: python arrays numpy


【解决方案1】:

where 结果仅与它搜索的布尔值一样好。如果参数没有任何 True 值,where 返回空:

In [308]: x = np.array(['2019-02-28', '2019-03-01'], dtype=object) 
     ...: z = np.array(['2019-02-28', '2019-03-02', '2019-03-01'], dtype=object)                             
In [309]: x==z                                                                                               
/usr/local/bin/ipython3:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
  #!/usr/bin/python3
Out[309]: False

如果您不关心订单:

In [311]: np.isin(z,x)                                                                                       
Out[311]: array([ True, False,  True])

或修剪z:

In [312]: x==z[:2]                                                                                           
Out[312]: array([ True, False])

要扩展x,你可以先使用np.pad,或者使用itertools.zip_longest

In [353]: list(itertools.zip_longest(x,z))                                                                   
Out[353]: 
[('2019-02-28', '2019-02-28'),
 ('2019-03-01', '2019-03-02'),
 (None, '2019-03-01')]
In [354]: [i==j for i,j in itertools.zip_longest(x,z)]                                                       
Out[354]: [True, False, False]

zip_longest 接受其他填充值,如果这样可以更好地进行比较。

【讨论】:

    【解决方案2】:

    这是你需要的吗:

    print([i for i, (x, y) in enumerate(zip(x, z)) if x == y])
    

    【讨论】:

      【解决方案3】:

      由于两个数组的大小不同,比较两个大小中的最小值。

      编辑: 我只是重读了问题和 cmets。

      result= np.zeros( max(x.size, z.size), dtype=bool) # result size of the biggest array.
      size = min(x.size, z.size)
      result[:size] = z[:size] == x[:size] # Comparison at smallest size.
      
      result
      # array([ True, False, False])
      

      这给出了评论要求的布尔掩码。

      原答案

      import numpy as np
      x = np.array(['2019-02-28', '2019-03-01'], dtype=object)
      z = np.array(['2019-02-28', '2019-03-02', '2019-03-01'], dtype=object)
      
      size = min(x.size, z.size)    
      np.where(z[:size]==x[:size])  # Select the common range
      # (array([0], dtype=int64),)
      

      在我的机器上,这比 @U10-Forward 的 dtype=object 列表理解要慢,但如果 numpy 选择 dtype 'Unicode 10',则速度更快。

      x = np.array(['2019-02-28', '2019-03-01'])
      z = np.array(['2019-02-28', '2019-03-02', '2019-03-01'])
      

      【讨论】:

        猜你喜欢
        • 2017-01-18
        • 1970-01-01
        • 2018-12-03
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 2015-07-04
        相关资源
        最近更新 更多