生成一个三维的数据
x = np.arange(24, 0, -1).reshape((2, 3, 4))
对于数据进行简单的排序,有
x_sort = np.sort(x, axis = 0)
axis表示维度,从0 到 -1分别对应括号的顺序
如果要获得排序对应的坐标
x_index = np.argsort(x, axis = 0)
如果想要利用这个坐标进行排序(sort的作用):
先生成其他维度对应的坐标:
arg_1 = np.arange(0, x.shape[1])
arg_2 = np.arange(0, x.shape[2])
相应的增加维度
arg_2 = np.expand_dims(arg_2, 0).repeat(x.shape[1], axis=0)
arg_2 = np.expand_dims(arg_2, 0).repeat(x.shape[0], axis=0)
arg_1 = np.expand_dims(arg_1, -1).repeat(x.shape[2], axis=-1)
arg_1 = np.expand_dims(arg_1, 0).repeat(x.shape[0], axis=0)
于是有:
x_sort = x[x_index, arg_1, arg_2]
1466

被折叠的 条评论
为什么被折叠?



