更新:
我相信问题实际上是这样的。您正在运行哪个版本的 Numpy?
http://projects.scipy.org/numpy/ticket/2063
我重现了您的问题,因为我测试的 Numpy 的 Ubuntu 安装是 1.6.1,并且该错误已修复为 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')