Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy排序wierd行为_Python_Python 2.7_Numpy - Fatal编程技术网

Python numpy排序wierd行为

Python numpy排序wierd行为,python,python-2.7,numpy,Python,Python 2.7,Numpy,我正在看我先前问的一个问题的答案。它们工作得很好,但有一个例子,我有问题 b ['Aug-09' 'Aug-09' 'Aug-09' ..., 'Jan-13' 'Jan-13' 'Jan-13'] b.shape (83761,) b.dtype |S6 bi, idxb = np.unique(b, return_index=True) months = bi[np.argsort(idxb)] months ndarray: ['Feb-10' 'Aug-10' 'Nov-10' 'Oct

我正在看我先前问的一个问题的答案。它们工作得很好,但有一个例子,我有问题

b
['Aug-09' 'Aug-09' 'Aug-09' ..., 'Jan-13' 'Jan-13' 'Jan-13']
b.shape
(83761,)
b.dtype
|S6
bi, idxb = np.unique(b, return_index=True)
months = bi[np.argsort(idxb)]
months
ndarray: ['Feb-10' 'Aug-10' 'Nov-10' 'Oct-12' 'Oct-11' 'Jul-10' 'Feb-12' 'Sep-11'\n  'Jan-10' 'Apr-10' 'May-10' 'Sep-09' 'Mar-11' 'Jun-12' 'Jul-12' 'Dec-09'\n 'Aug-09' 'Nov-12' 'Dec-12' 'Apr-12' 'Jun-11' 'Jan-11' 'Jul-11' 'Sep-10'\n 'Jan-12' 'Dec-10' 'Oct-09' 'Nov-11' 'Oct-10' 'Mar-12' 'Jan-13' 'Nov-09'\n 'May-11' 'Mar-10' 'Jun-10' 'Dec-11' 'May-12' 'Feb-11' 'Aug-11' 'Sep-12'\n 'Apr-11' 'Aug-12']
为什么月份从2月10日开始,而不是从09年8月开始?通过较小的数据集,我得到了预期的行为,即从2009年8月开始的月份。2月10日,我得到了上一个问题的所有答案


这很有效

months = []
for bi in b:
    if bi not in months:
        months.append(bi) 

这是我的数据集。你自己试试看

import numpy as np
f=open('test.txt','r')
res = []
for line in f.readlines():
   res.append(line.strip())

a = np.array(res)
_, idx = np.unique(a, return_index=True)
print a[np.sort(idx)]
更新:

我相信问题其实是这样的。你正在运行哪个版本的Numpy

我重现了你的问题,因为我测试的Numpy的Ubuntu安装版本是1.6.1,而bug是在1.6.2及以上版本修复的

升级Numpy,再试一次,它在我的Ubuntu机器上对我有效


在这些方面:

bi, idxb = np.unique(b, return_index=True)
months = bi[np.argsort(idxb)]
有两个错误:

  • 您希望实际使用原始数组上的排序索引,
    b[…]
  • 您需要排序的索引,而不是对索引进行排序的索引,因此请使用
    sort
    not
    argsort
  • 这应该起作用:

    bi, idxb = np.unique(b, return_index=True)
    months = b[np.sort(idxb)]
    

    是的,它使用您的数据集并在Mac OS 10.6 64位上运行python 2.7和numpy 1.7

    Python 2.7.3 (default, Oct 23 2012, 13:06:50) 
    
    IPython 0.13.1 -- An enhanced Interactive Python.
    
    In [1]: import numpy as np
    
    In [2]: np.__version__
    Out[2]: '1.7.0'
    
    In [3]: from platform import architecture
    
    In [4]: architecture()
    Out[4]: ('64bit', '')
    
    In [5]: f = open('test.txt','r')
    
    In [6]: lines = np.array([line.strip() for line in f.readlines()])
    
    In [7]: _, ilines = np.unique(lines, return_index = True)
    
    In [8]: months = lines[np.sort(ilines)]
    
    In [9]: months
    Out[9]: 
    array(['Aug-09', 'Sep-09', 'Oct-09', 'Nov-09', 'Dec-09', 'Jan-10',
           'Feb-10', 'Mar-10', 'Apr-10', 'May-10', 'Jun-10', 'Jul-10',
           'Aug-10', 'Sep-10', 'Oct-10', 'Nov-10', 'Dec-10', 'Jan-11',
           'Feb-11', 'Mar-11', 'Apr-11', 'May-11', 'Jun-11', 'Jul-11',
           'Aug-11', 'Sep-11', 'Oct-11', 'Nov-11', 'Dec-11', 'Jan-12',
           'Feb-12', 'Mar-12', 'Apr-12', 'May-12', 'Jun-12', 'Jul-12',
           'Aug-12', 'Sep-12', 'Oct-12', 'Nov-12', 'Dec-12', 'Jan-13'], 
          dtype='|S6')
    

    好的,我终于可以在Ubuntu 64位上重现你的问题了:

    Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
    
    IPython 0.12.1 -- An enhanced Interactive Python.
    
    In [1]: import numpy as np
    
    In [2]: np.__version__
    Out[2]: '1.6.1'
    
    In [3]: from platform import architecture
    
    In [4]: architecture()
    Out[4]: ('64bit', 'ELF')
    
    In [5]: f = open('test.txt','r')
    
    In [6]: lines = np.array([line.strip() for line in f.readlines()])
    
    In [7]: _, ilines = np.unique(lines, return_index=True)
    
    In [8]: months = lines[np.sort(ilines)]
    
    In [9]: months
    Out[9]: 
    array(['Feb-10', 'Aug-10', 'Nov-10', 'Oct-12', 'Oct-11', 'Jul-10',
           'Feb-12', 'Sep-11', 'Jan-10', 'Apr-10', 'May-10', 'Sep-09',
           'Mar-11', 'Jun-12', 'Jul-12', 'Dec-09', 'Aug-09', 'Nov-12',
           'Dec-12', 'Apr-12', 'Jun-11', 'Jan-11', 'Jul-11', 'Sep-10',
           'Jan-12', 'Dec-10', 'Oct-09', 'Nov-11', 'Oct-10', 'Mar-12',
           'Jan-13', 'Nov-09', 'May-11', 'Mar-10', 'Jun-10', 'Dec-11',
           'May-12', 'Feb-11', 'Aug-11', 'Sep-12', 'Apr-11', 'Aug-12'], 
          dtype='|S6')
    

    Numpy升级后在Ubuntu上工作:

    Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
    
    IPython 0.12.1 -- An enhanced Interactive Python.
    
    In [1]: import numpy as np
    
    In [2]: np.__version__
    Out[2]: '1.7.0'
    
    In [3]: f = open('test.txt','r')
    
    In [4]: lines = np.array([line.strip() for line in f.readlines()])
    
    In [5]: _, ilines = np.unique(lines, return_index=True)
    
    In [6]: months = lines[np.sort(ilines)]
    
    In [7]: months
    Out[7]: 
    array(['Aug-09', 'Sep-09', 'Oct-09', 'Nov-09', 'Dec-09', 'Jan-10',
           'Feb-10', 'Mar-10', 'Apr-10', 'May-10', 'Jun-10', 'Jul-10',
           'Aug-10', 'Sep-10', 'Oct-10', 'Nov-10', 'Dec-10', 'Jan-11',
           'Feb-11', 'Mar-11', 'Apr-11', 'May-11', 'Jun-11', 'Jul-11',
           'Aug-11', 'Sep-11', 'Oct-11', 'Nov-11', 'Dec-11', 'Jan-12',
           'Feb-12', 'Mar-12', 'Apr-12', 'May-12', 'Jun-12', 'Jul-12',
           'Aug-12', 'Sep-12', 'Oct-12', 'Nov-12', 'Dec-12', 'Jan-13'], 
          dtype='|S6')
    

    可能是按字符串散列排序?这是一个在1.6.2版中修复的Numpy错误,请参阅我编辑的答案。不,我需要按原始顺序排序。在本例中,第一个项目是Aug-09,因此在唯一列表中,该项目应排在第一位,并保留顺序2009年8月、2009年8月、2009年9月、09年9月、09年9月、09年8月、09年10月、09年8月、09年9月、09年9月、09年9月、09年9月、09年8月、09年9月、09年8月、09年9月、09年9月、09年9月、09年8月、09年9月、09年9月、09年9月、09年9月、09年9月、09年9月、09年9月、09年9月、09、09年9月、09、09年9月、09、09、09年9月、09、09、09、09年9月ug-09''2009年10月''2009年8月''2009年8月']@Bizzo09啊哈。在索引上使用
    sort
    ,而不是
    argsort
    。再次查看编辑,希望最后一个:PYes应该可以工作。但是在我的数据集上,它不工作。我不知道为什么。你可以从链接下载它。编辑。好的,等一下,让我试试,我在两台计算机上尝试了你的代码,两次都成功了2009年10月10日8月10日11月10日10月12日10月11日7月12日9月11日1月10日5月10日9月11日6月12日7月12日12月12日12月12日12月12日12月12日12月12日12月12日4月12日6月11日11月11日7月11日9月10日1月12日12日12日10月10日10日10月12日3月12日1月13日11月11日5月11日3月11日10日3月11日10日10日12日3月10日10日12日12日12日12日11月10日12日12日12日11月10日12日11月10日11日11日12日11日11月11日11日11日11日11月11日12日11月11日11日11日ug-11''2012年9月''2011年4月''2012年8月']