GVKun编程网logo

对具有多个维度的numpy.argsort进行不变排序(在多维分析中如果对多个维度进行限定)

31

对于想了解对具有多个维度的numpy.argsort进行不变排序的读者,本文将提供新的信息,我们将详细介绍在多维分析中如果对多个维度进行限定,并且为您提供关于API:numpy.argsort、Arg

对于想了解对具有多个维度的numpy.argsort进行不变排序的读者,本文将提供新的信息,我们将详细介绍在多维分析中如果对多个维度进行限定,并且为您提供关于API: numpy.argsort、Argsort.argsort - 它在做什么?、Argsort无法与numba结合使用、arrays – Perl:基于特定列对具有多个列的2D数组进行排序的有价值信息。

本文目录一览:

对具有多个维度的numpy.argsort进行不变排序(在多维分析中如果对多个维度进行限定)

对具有多个维度的numpy.argsort进行不变排序(在多维分析中如果对多个维度进行限定)

numpy.argsort
docs状态

返回:
index_array:ndarray,int沿指定轴对a进行排序的索引数组。如果a是一维的,则a[index_array]产生排序的a。

我如何应用numpy.argsort多维数组的结果以返回已排序的数组?(不只是一维或二维数组;它可以是一个N维数组,其中N仅在运行时才知道)

>>> import numpy as np>>> np.random.seed(123)>>> A = np.random.randn(3,2)>>> Aarray([[-1.0856306 ,  0.99734545],       [ 0.2829785 , -1.50629471],       [-0.57860025,  1.65143654]])>>> i=np.argsort(A,axis=-1)>>> A[i]array([[[-1.0856306 ,  0.99734545],        [ 0.2829785 , -1.50629471]],       [[ 0.2829785 , -1.50629471],        [-1.0856306 ,  0.99734545]],       [[-1.0856306 ,  0.99734545],        [ 0.2829785 , -1.50629471]]])

对我来说,这不仅仅是使用的问题sort();我有另一个数组B,我想B使用np.argsort(A)沿适当轴的结果进行排序。考虑以下示例:

>>> A = np.array([[3,2,1],[4,0,6]])>>> B = np.array([[3,1,4],[1,5,9]])>>> i = np.argsort(A,axis=-1)>>> BsortA = ???             # should result in [[4,1,3],[5,1,9]]# so that corresponding elements of B and sort(A) stay together

似乎此功能已经是numpy中的增强请求。

答案1

小编典典

该numpy的问题#8708具有take_along_axis的样本实现,做什么,我需要;
我不确定大型阵列是否有效,但似乎可行。

def take_along_axis(arr, ind, axis):    """    ... here means a "pack" of dimensions, possibly empty    arr: array_like of shape (A..., M, B...)        source array    ind: array_like of shape (A..., K..., B...)        indices to take along each 1d slice of `arr`    axis: int        index of the axis with dimension M    out: array_like of shape (A..., K..., B...)        out[a..., k..., b...] = arr[a..., inds[a..., k..., b...], b...]    """    if axis < 0:       if axis >= -arr.ndim:           axis += arr.ndim       else:           raise IndexError(''axis out of range'')    ind_shape = (1,) * ind.ndim    ins_ndim = ind.ndim - (arr.ndim - 1)   #inserted dimensions    dest_dims = list(range(axis)) + [None] + list(range(axis+ins_ndim, ind.ndim))    # could also call np.ix_ here with some dummy arguments, then throw those results away    inds = []    for dim, n in zip(dest_dims, arr.shape):        if dim is None:            inds.append(ind)        else:            ind_shape_dim = ind_shape[:dim] + (-1,) + ind_shape[dim+1:]            inds.append(np.arange(n).reshape(ind_shape_dim))    return arr[tuple(inds)]

产生

>>> A = np.array([[3,2,1],[4,0,6]])>>> B = np.array([[3,1,4],[1,5,9]])>>> i = A.argsort(axis=-1)>>> take_along_axis(A,i,axis=-1)array([[1, 2, 3],       [0, 4, 6]])>>> take_along_axis(B,i,axis=-1)array([[4, 1, 3],       [5, 1, 9]])

API: numpy.argsort

API: numpy.argsort

为了以后查找方便; 无时间整理, 也没整理的必要, 直接贴图了.      

参考网站: http://docs.scipy.org/doc/numpy/reference/index.html


摘自: 《NumPy Reference, Release 1.7.0

Argsort.argsort - 它在做什么?

Argsort.argsort - 它在做什么?

如何解决Argsort.argsort - 它在做什么??

为什么 numpy 给出这个结果:

x = numpy.array([-1,5,-2,0])

print x.argsort().argsort()

[1,3,2]

解决方法

numpy.argsort 函数

返回对数组进行排序的索引。


|   |   |
[Q ][W ]...
_[A ][S ]...
___[Z ][X ]...
|   |   |

要得到一个已排序的数组,您需要使用顺序$recordstoday = Eventos::whereDate(''created_at'',Carbon::today())->get();

x = numpy.array([-1,5,-2,0])
print(x.argsort())  # [2 0 3 1]

调用 [2 0 3 1]index [ 2 0 3 1] | | | | v v v v value -2 -1 0 5 << is sorted 相同。它给

x.argsort().argsort()
,

第一个 x.argsort() 按对数组 x 进行排序的顺序返回索引。当您对这个结果再次调用 argsort 时,您得到的是对之前的索引数组进行排序的索引数组:

x.argsort()
# array([2,3,1])
x.argsort().argsort()
# array([1,2])
# To sort the array x.argsort(),you start with index 1 which hold the value 0,and so on
,

argsort() 给出排序数组的索引。

第一个 argsort() 结果是:[2,1]

第二个 argsort() 结果 [1,2]

您可以在 NumPy 文档中找到详细信息。 https://numpy.org/doc/stable/reference/generated/numpy.argsort.html

Argsort无法与numba结合使用

Argsort无法与numba结合使用

如何解决Argsort无法与numba结合使用?

将np.argsort与numba结合使用时遇到一些问题。
输入坐标是由坐标组成的二维数组,需要按逆时针方式对其进行排序。所有变量都是float64和numy数组的。

@jit
def sortVertices(coord):
    yr = coord[:,1]
    xc = coord[:,0]
    xl = np.array([xc.size])
    yl = np.array([yr.size])
    center_xc = np.sum(xc)/xl
    center_yr = np.sum(yr)/yl
    theta = np.arctan2(yr-center_yr,xc-center_xc) * 180.0 / np.pi
    indices = np.argsort(theta)
    x = xc[indices]
    y = yr[indices]
    coord_new = np.vstack((x,y)).T
    return coord_new

我更新了numba。错误:

NumbaWarning: 
Compilation is falling back to object mode WITH looplifting enabled because Function sortVertices Failed at nopython mode lowering due to: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function make_quicksort_impl.<locals>.run_quicksort at 0x1298bc9d8>) found for signature:
 
run_quicksort(array(float64,1d,C))
 
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function ''register_jitable.<locals>.wrap.<locals>.ov_wrap'': File: numba/core/extending.py: Line 150.
    With argument(s): ''(array(float64,C))'':
   Rejected as the implementation raised a specific error:
     AttributeError: ''function'' object has no attribute ''get_call_template''
  raised from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numba/core/types/functions.py:511\

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

arrays – Perl:基于特定列对具有多个列的2D数组进行排序

arrays – Perl:基于特定列对具有多个列的2D数组进行排序

伪代码:
my @unsortedArray = { ["Harry",10],["Tim",8],["Joe",3]};
my @sortedArray = ?????

最终sortedArray应该基于col-2(整数)进行排序,处理与“人名”(col-1)的1对1关系.最终结果应如下所示:

sortedArray should be { ["Joe",3],["Harry",10] };

解决方法

您可以为谓词提供排序,即:一个被评估以比较列表元素的函数.
my @unsorted = ( ["Harry",3] );

my @sorted = sort { $a->[1] <=> $b->[1] } @unsorted;

在谓词(花括号中的表达式)中,$a和$b是外部列表中要比较的元素.

sort只关注一维列表,因此它不会混淆外部列表元素的内部结构.因此,名称和数字之间的关系可以毫不费力地保留.

有关更多详细信息,请参阅perldoc -f sort和perldoc perlop.

今天的关于对具有多个维度的numpy.argsort进行不变排序在多维分析中如果对多个维度进行限定的分享已经结束,谢谢您的关注,如果想了解更多关于API: numpy.argsort、Argsort.argsort - 它在做什么?、Argsort无法与numba结合使用、arrays – Perl:基于特定列对具有多个列的2D数组进行排序的相关知识,请在本站进行查询。

本文标签: