Ago*_*ino 8 python arrays sorting numpy
我ndarray从一个文件中读取它,就像这样
my_data = np.genfromtxt(input_file, delimiter='\t', skip_header=0)
Run Code Online (Sandbox Code Playgroud)
示例输入(已解析)
[[ 2. 1. 2. 0.]
[ 2. 2. 100. 0.]
[ 2. 3. 100. 0.]
[ 3. 1. 2. 0.]
[ 3. 2. 4. 0.]
[ 3. 3. 6. 0.]
[ 4. 1. 2. 0.]
[ 4. 2. 4. 0.]
[ 4. 3. 6. 0.]]
Run Code Online (Sandbox Code Playgroud)
更长的示例输入(未解析).
前两列应该是int,而最后两列应该是float,但这就是我得到的.欢迎提出建议.
主要问题是,我正在尝试使用Numpy对其进行排序,以便行排序优先于第二列的数字,然后是第一列.
期望输出的示例
[[ 2. 1. 2. 0.]
[ 3. 1. 2. 0.]
[ 4. 1. 2. 0.]
[ 2. 2. 100. 0.]
[ 3. 2. 4. 0.]
[ 4. 2. 4. 0.]
[ 2. 3. 100. 0.]
[ 3. 3. 6. 0.]
[ 4. 3. 6. 0.]]
Run Code Online (Sandbox Code Playgroud)
我知道这个答案,它适用于对单个列上的行进行排序.
我尝试对第二列进行排序,因为第一列已经排序,但这还不够.有时,第一列也会重新排序,严重.
new_data = my_data[my_data[:, 1].argsort()]
print(new_data)
#output
[[ 2. 1. 2. 0.]
[ 4. 1. 2. 0.] #ouch
[ 3. 1. 2. 0.] #ouch
[ 2. 2. 100. 0.]
[ 3. 2. 4. 0.]
[ 4. 2. 4. 0.]
[ 2. 3. 100. 0.]
[ 3. 3. 6. 0.]
[ 4. 3. 6. 0.]]
Run Code Online (Sandbox Code Playgroud)
我也检查了这个问题
答案提到了
这里的问题是np.lexsort或np.sort不适用于dtype对象的数组.要解决该问题,您可以在创建order_list之前对rows_list进行排序:
import operator
rows_list.sort(key=operator.itemgetter(0,1,2))
Run Code Online (Sandbox Code Playgroud)
但是我的类型函数中没有key参数.在我的情况下,合并字段不是替代方案.sortndarray
另外,我没有标题,所以,如果我尝试使用order参数排序,我会收到错误.
ValueError: Cannot specify order when the array has no fields.
Run Code Online (Sandbox Code Playgroud)
我宁愿排序或者至少获得相同类型的结果ndarray.然后我想将它保存到文件中.
如何在不弄乱数据类型的情况下执行此操作?
Jim*_*Jim 24
您np.lexsort可以同时根据多个列进行排序。您想要排序的列需要反向传递。这意味着np.lexsort((col_b,col_a))首先按 col_a 排序,然后按 col_b 排序:
my_data = np.array([[ 2., 1., 2., 0.],
[ 2., 2., 100., 0.],
[ 2., 3., 100., 0.],
[ 3., 1., 2., 0.],
[ 3., 2., 4., 0.],
[ 3., 3., 6., 0.],
[ 4., 1., 2., 0.],
[ 4., 2., 4., 0.],
[ 4., 3., 6., 0.]])
ind = np.lexsort((my_data[:,0],my_data[:,1]))
my_data[ind]
Run Code Online (Sandbox Code Playgroud)
结果:
array([[ 2., 1., 2., 0.],
[ 3., 1., 2., 0.],
[ 4., 1., 2., 0.],
[ 2., 2., 100., 0.],
[ 3., 2., 4., 0.],
[ 4., 2., 4., 0.],
[ 2., 3., 100., 0.],
[ 3., 3., 6., 0.],
[ 4., 3., 6., 0.]])
Run Code Online (Sandbox Code Playgroud)
如果您知道第一列已经排序,您可以使用:
ind = my_data[:,1].argsort(kind='stable')
my_data[ind]
Run Code Online (Sandbox Code Playgroud)
这可确保保留相同项目的顺序。通常使用的快速排序算法虽然速度更快,但并不能做到这一点。
>>> a = np.array([[1,30,200], [2,20,300], [3,10,100]])
>>> a
array([[ 1, 30, 200],
[ 2, 20, 300],
[ 3, 10, 100]])
>>> a[a[:,2].argsort()] #sort by the 3rd column ascending
array([[ 3, 10, 100],
[ 1, 30, 200],
[ 2, 20, 300]])
>>> a[a[:,2].argsort()][::-1] #sort by the 3rd column descending
array([[ 2, 20, 300],
[ 1, 30, 200],
[ 3, 10, 100]])
>>> a[a[:,1].argsort()] #sort by the 2nd column ascending
array([[ 3, 10, 100],
[ 2, 20, 300],
[ 1, 30, 200]])
Run Code Online (Sandbox Code Playgroud)
要解释这里发生了什么:argsort()传回一个包含其父级的整数序列的数组:https:
//docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html
>>> x = np.array([15, 30, 4, 80, 6])
>>> np.argsort(x)
array([2, 4, 0, 1, 3])
Run Code Online (Sandbox Code Playgroud)
>>> a = np.array([[2,30,200], [1,30,200], [1,10,200]])
>>> a
array([[ 2, 30, 200],
[ 1, 30, 200],
[ 1, 10, 200]])
>>> a[np.lexsort((a[:,2], a[:,1],a[:,0]))]
array([[ 1, 10, 200],
[ 1, 30, 200],
[ 2, 30, 200]])
>>> a[np.lexsort((a[:,2], a[:,1],a[:,0]))][::-1] #reverse
array([[ 2 30 200]
[ 1 30 200]
[ 1 10 200]])
Run Code Online (Sandbox Code Playgroud)
import numpy as np
my_data = [[ 2., 1., 2., 0.],
[ 2., 2., 100., 0.],
[ 2., 3., 100., 0.],
[ 3., 1., 2., 0.],
[ 3., 2., 4., 0.],
[ 3., 3., 6., 0.],
[ 4., 1., 2., 0.],
[ 4., 2., 4., 0.],
[ 4., 3., 6., 0.]]
my_data = np.array(my_data)
r = np.core.records.fromarrays([my_data[:,1],my_data[:,0]],names='a,b')
my_data = my_data[r.argsort()]
print(my_data)
Run Code Online (Sandbox Code Playgroud)
[[ 2. 1. 2. 0.]
[ 3. 1. 2. 0.]
[ 4. 1. 2. 0.]
[ 2. 2. 100. 0.]
[ 3. 2. 4. 0.]
[ 4. 2. 4. 0.]
[ 2. 3. 100. 0.]
[ 3. 3. 6. 0.]
[ 4. 3. 6. 0.]]
Run Code Online (Sandbox Code Playgroud)
导入让Numpy猜测类型并进行适当排序:
import numpy as np
# let numpy guess the type with dtype=None
my_data = np.genfromtxt(infile, dtype=None, names=["a", "b", "c", "d"])
# access columns by name
print(my_data["b"]) # column 1
# sort column 1 and column 0
my_data.sort(order=["b", "a"])
# save specifying required format (tab separated values)
np.savetxt("sorted.tsv", my_data, fmt="%d\t%d\t%.6f\t%.6f"
Run Code Online (Sandbox Code Playgroud)
或者,指定输入格式并排序到新数组:
import numpy as np
# tell numpy the first 2 columns are int and the last 2 are floats
my_data = np.genfromtxt(infile, dtype=[('a', '<i8'), ('b', '<i8'), ('x', '<f8'), ('d', '<f8')])
# access columns by name
print(my_data["b"]) # column 1
# get the indices to sort the array using lexsort
# the last element of the tuple (column 1) is used as the primary key
ind = np.lexsort((my_data["a"], my_data["b"]))
# create a new, sorted array
sorted_data = my_data[ind]
# save specifying required format (tab separated values)
np.savetxt("sorted.tsv", sorted_data, fmt="%d\t%d\t%.6f\t%.6f")
Run Code Online (Sandbox Code Playgroud)
输出:
2 1 2.000000 0.000000
3 1 2.000000 0.000000
4 1 2.000000 0.000000
2 2 100.000000 0.000000
3 2 4.000000 0.000000
4 2 4.000000 0.000000
2 3 100.000000 0.000000
3 3 6.000000 0.000000
4 3 6.000000 0.000000
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12112 次 |
| 最近记录: |