排序和索引的问题

 1 '''
 2 1.查看列上最大索引的位置
 3     data.argmax(axis = 0)
 4 2.输出索引位置上的元素
 5     data[index,range(data.shape[1])]
 6     使用 range 输出几个元素
 7 3.对numpy 对象进行扩展
 8     a = np.array([4,5,6,2])
 9     np.tile(a,(2,3))
10 4.对数组按行排序,从小到大
11     a = np.array([[4,3,5],[1,7,6]])
12     np.sort(a,axis = 1)
13 5.对数组元素进行排序,返回索引下标
14     a = np.array([4,3,1,2])
15     j = np.argsort(a)
16     a[j]
17 '''
18 import numpy as np
19 data = np.array([
20     [4,5,6,8],
21     [7,4,2,8],
22     [9,5,4,2]
23 ])
24 data.argmax(axis = 0)
25 # array([2, 0, 0, 0], dtype=int64)
26 data.argmax(axis = 1)
27 # array([3, 3, 0], dtype=int64)
28 index = data.argmax(axis = 0)
29 # array([9, 5, 6, 8])
30 a = np.array([4,5,6,2])
31 np.tile(a,(2,3))
32 '''
33 array([[4, 5, 6, 2, 4, 5, 6, 2, 4, 5, 6, 2],
34        [4, 5, 6, 2, 4, 5, 6, 2, 4, 5, 6, 2]])
35 '''
36 a = np.array([[4,3,5],[1,7,6]])
37 '''
38 array([[4, 3, 5],
39        [1, 7, 6]])
40 '''
41 np.sort(a,axis = 1)
42 '''
43 array([[3, 4, 5],
44        [1, 6, 7]])
45 '''
46 np.sort(a,axis = 0)
47 '''
48 array([[1, 3, 5],
49        [4, 7, 6]])
50 '''
51 a = np.array([4,3,1,2])
52 j = np.argsort(a)
53 # array([1, 2, 3, 4])

2020-04-10

posted @ 2020-04-10 12:41  CodeYaSuo  阅读(225)  评论(0编辑  收藏  举报