np.array操作

排序:按照最后一个维度进行排序,最后一个维度相同的随机排列

import numpy as np
a=np.array([[[3,4,3],[2,1,7],[2,3,4],[1,6,4]],[[3,4,2],[1,5,0],[2,0,0],[5,6,0]],[[3,4,5],[2,3,0],[2,7,0],[5,6,float("inf")]]])
print(a)
rng = np.random.default_rng()
a = rng.permutation(a,axis=1)
print(f'a[:,:,-2]: {a[:,:,-2]}')
tmp = [a[0,np.argsort(a[:,:,-1])[0],:]]
for i in range(1,a.shape[0]):
    #tmp = np.concatenate((tmp, [a[i, np.lexsort((a[:,:,-2], a[:,:,-1]))[i], :]]))
    tmp = np.concatenate((tmp, [a[i, np.argsort(a[:,:,-1])[i], :]]))
print(f'tmp:{tmp}')

拆分:中间参数是[x,x,x...]的array,数组中的数作为index进行指定分割

def split(ary, indices_or_sections, axis=0):
    """
    Split an array into multiple sub-arrays as views into `ary`.

    Parameters
    ----------
    ary : ndarray
        Array to be divided into sub-arrays.
    indices_or_sections : int or 1-D array
        If `indices_or_sections` is an integer, N, the array will be divided
        into N equal arrays along `axis`.  If such a split is not possible,
        an error is raised.

        If `indices_or_sections` is a 1-D array of sorted integers, the entries
        indicate where along `axis` the array is split.  For example,
        ``[2, 3]`` would, for ``axis=0``, result in

          - ary[:2]
          - ary[2:3]
          - ary[3:]

        If an index exceeds the dimension of the array along `axis`,
        an empty sub-array is returned correspondingly.
    axis : int, optional
        The axis along which to split, default is 0.

    Returns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值