awo*_*xho 8 python arrays sorting numpy
我有一个名为v的dtype('float64')数组:
array([[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02],
[ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00],
[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02]])
Run Code Online (Sandbox Code Playgroud)
...我通过使用np.loadtxt命令从文件中获取的.我想在第一列的值之后对其进行排序,而不会混淆将数字列在同一行上的结构.使用v.sort(axis = 0)给我:
array([[ 1.33360000e+05, 8.75886500e+06, 6.19200000e+00],
[ 4.33350000e+05, 8.75886500e+06, 3.45765000e+02],
[ 9.33350000e+05, 8.75886500e+06, 6.76650000e+02]])
Run Code Online (Sandbox Code Playgroud)
...即将第三列中最小数量的第一列放在第一行等等.我宁愿想要这样的东西......
array([[ 1.33360000e+05, 8.75886500e+06, 6.76650000e+02],
[ 4.33350000e+05, 8.75886500e+06, 6.19200000e+00],
[ 9.33350000e+05, 8.75886500e+06, 3.45765000e+02]])
Run Code Online (Sandbox Code Playgroud)
......每条线的元素没有相对移动.
Sve*_*ach 13
尝试
v[v[:,0].argsort()]
Run Code Online (Sandbox Code Playgroud)
(v作为阵列). v[:,0]是第一列,并.argsort()返回将第一列排序的索引.然后使用高级索引将此排序应用于整个数组.请注意,您将获得该阵列的sorte副本.
我知道对数组进行排序的唯一方法是使用记录dtype:
v.dtype = [("x", float), ("y", float), ("z", float)]
v.shape = v.size
v.sort(order="x")
Run Code Online (Sandbox Code Playgroud)
另外
尝试
import numpy as np
order = v[:, 0].argsort()
sorted = np.take(v, order, 0)
Run Code Online (Sandbox Code Playgroud)
'order'具有第一行的顺序.然后'np.take'将列按相应的顺序排列.
请参阅'np.take'的帮助
help(np.take)
Run Code Online (Sandbox Code Playgroud)
take(a,indices,axis = None,out = None,mode ='raise')沿轴取一个数组中的元素.
Run Code Online (Sandbox Code Playgroud)This function does the same thing as "fancy" indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. Parameters ---------- a : array_like The source array. indices : array_like The indices of the values to extract. axis : int, optional The axis over which to select values. By default, the flattened input array is used. out : ndarray, optional If provided, the result will be placed in this array. It should be of the appropriate shape and dtype. mode : {'raise', 'wrap', 'clip'}, optional Specifies how out-of-bounds indices will behave. * 'raise' -- raise an error (default) * 'wrap' -- wrap around * 'clip' -- clip to the range 'clip' mode means that all indices that are too large are由追踪该轴的最后一个元素的索引替换.请注意,这会禁用带负数的索引.
Run Code Online (Sandbox Code Playgroud)Returns ------- subarray : ndarray The returned array has the same type as `a`. See Also -------- ndarray.take : equivalent method Examples -------- >>> a = [4, 3, 5, 7, 6, 8] >>> indices = [0, 1, 4] >>> np.take(a, indices) array([4, 3, 6]) In this example if `a` is an ndarray, "fancy" indexing can be used. >>> a = np.array(a) >>> a[indices] array([4, 3, 6])
| 归档时间: |
|
| 查看次数: |
15928 次 |
| 最近记录: |