【问题标题】:numpy unique strange behaviournumpy 独特的奇怪行为
【发布时间】:2013-10-02 12:44:20
【问题描述】:

根据官方 numpy.unique 文档 (http://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html) return_index=True 应该允许我恢复数组中第一次出现的元素。但是,这不适用于这个简单的示例:

import numpy as np
a = np.array([10,20,30,40,50,60,70,80,3,2,4,3,2,5,2,1,999,1000])
a = np.append(a,np.repeat(999,10000))
u, indices = np.unique(a, return_index=True)
print indices[13], u[13] #according to unique documentation indices[13] should be 16 (i.e. first occurrence of 999 = u[13]), but it is not

这会导致:

[mvogelsberger@itc021 ~]$ python test.py 
6685 999

显然,6685 不是数组 a 中第一次出现 999 的索引。有人可以澄清吗?我可能误解了文档...

谢谢! 标记

【问题讨论】:

  • 您的代码为我打印16 999
  • 好的,这很有趣。哪个 numpy/Python 版本?
  • Python 2.7.5 + Numpy 1.7.1
  • 好的,我使用了 Numpy 1.6.0,Python 2.7.1。刚刚更改了 numpy 版本并得到了正确的结果。所以这一定是 Numpy 中的一些错误
  • 查看thisthis numpy 错误报告。这一切都归结为在需要返回索引时不使用稳定排序(合并排序)。

标签: python arrays numpy scipy numerical


【解决方案1】:

正如你在 cmets 中猜到的,这种行为确实是a bug that was fixed in Numpy v1.7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多