numpy选择s_, index_exp,item,itemset,getfield,take,put, nonzero,select,choose,extract,compress (tcy)

1.函数:
    np.s_[:]                         数组索引	
    np.index_exp[2::2]               建立数组索引元组的更好方法	
        
    a.item(* args)                   复制元素到python标量  参数:None:len(a)=1; Int_type:数组平面索引;Int_types:元祖	
    a.itemset(* args)                更改数组中数值        参数:参数1int 或元祖,位置(x,[y]);参数2更改值	
    a.getfield(dtype,offset = 0)     以给定类型返回给定数组的字段.	
            
    np.take(a,indices,axis = None,out = None,mode ='raise') 获取(默认1D )元素;优于花式索引	
    np.put(a,ind,v,mode ='raise')    设置1D元素;等价a.flat[ind] = v	
            
    np.nonzero(a)                    返回输入数组中非零元素的索引.	
    np.count_nonzero(a,axis = None )              计算数组中非零值的数量	

    np.select(condlist, choicelist, default=0)    返回从选择列表中的元素绘制的数组,取决于条件	
    np.choose(a,choice,out = None,mode ='raise')  根据条件选择	
    np.where(condition, [x, y])                   返回输入数组中满足给定条件的元素的索引.	
    np.extract()                                  根据某个条件从数组中抽取元素,返回满条件元素	
            
    np.compress(condition,axis = None,out = None )沿给定的轴返回此数组的选定切片	
2.实例	
    	
实例1:np.s_-数组索引	
    np.s_[2::2]                                   # slice(2, None, 2)	
    np.index_exp[2::2]                            # (slice(2, None, 2),)	
    np.array([0, 1, 2, 3, 4])[np.s_[2::2]]        # array([2, 4])	
    np.array([0, 1, 2, 3, 4])[np.index_exp[2::2]] # array([2, 4])	
实例2.1:item-获取标量	
    a=np.arange(12).reshape(3,4)	
    a.item(7)               #7     获取标量	
    a.item(0,2,)            #2     获取标量    等价a.item((0,2))	
    	
实例2.2.:itemset-设置标量	
    a.itemset(7,-7)         #修改元素为-7	a =array([[ 0,  1,  2,  3],[ 4,  5,  6, -7],[ 8,  9, 10, 11]])	
    a.itemset((0,2),-2)     #修改元素为-2	a =array([[ 0,  1, -2,  3], [ 4,  5,  6, -7],[ 8,  9, 10, 11]])	
实例3:getfield-获取数值字段	
    x = np.diag([1.+1.j]*2)   	#x = array([[1.+1.j, 0.+0.j],[0.+0.j, 1.+1.j]])
    x[1, 1] = 2+ 4.j	        #x = array([[ 1.+1.j,  0.+0.j],[ 0.+0.j,  2.+4.j]])	
    x.getfield(np.float64)      #array([[ 1.,  0.], [ 0.,  2.]])	
            
    # 选择8字节偏移量得到虚部视图	
    x.getfield(np.float64, offset=8)# array([[ 1.,  0.], [ 0.,  4.]])	
实例4.1:take-选取元素	
    a = np.array([10, 11, 12, 13, 14, 15])	
            
    np.take(a, [0,1,2,3])           # array([10, 11, 12, 13])	
    np.take(a, [[0, 1], [2, 3]])    # array([[10, 11],[12, 13]])	
            
    a=np.arange(10,22).reshape(3,4)	
    np.take(a,[0,1,2,3])            # array([10, 11, 12, 13])	
    np.take(a,[[0,1],[2,3]])        # array([[10, 11], [12, 13]])	
            
    a.take([1,2],axis=0)            # array([[14, 15, 16, 17],[18, 19, 20, 21]])    #选取第2,3行	
    a.take([1,2],axis=1)            # array([[11, 12],[15, 16], [19, 20]])              #选取第2,3列	
    	
实例4.2:put-替换选定位置数据	
    a = np.arange(5)	
    a.put([1,2],-1)                    #选定元素用-1替代	a = array([ 0, -1, -1,  3,  4])	    	
    np.put(a, [4,3,2,1], [-4,-3,-2,-1])#选定元素用list替换	a = array([ 0, -1, -2, -3, -4])	
实例5:nonzero-非零元素索引	
    np.nonzero ([3,0,2,5,0,6])           # (array([0, 2, 3, 5], dtype=int64),)	
    a = np.array([[3,4,0],[0,2,1],[5,0,6]])	
    b=np.nonzero (a)                     # (array([0, 0, 1, 1, 2, 2], dtype=int64),array([0, 1, 1, 2, 0, 2], dtype=int64))	
    np.transpose(b)                      # array([[0, 0], [0, 1], [1, 1],[1, 2],[2, 0],[2, 2]], dtype=int64)	
    a[b]                                 # array([3, 4, 2, 1, 5, 6])	

    # 一个常用用法是查找条件为True数组的索引	
    a = np.array([[1,2,3],[4,5,6]])	
    a > 3                                # array([[False, False, False],[ True, True, True]])	
    np.nonzero(a > 3)                    # 结果同下	
    (a > 3).nonzero()                    # (array([1, 1, 1], dtype=int64), array([0, 1, 2], dtype=int64))	
            
    np.count_nonzero(a,axis = None )     # 计算数组中非零值的数量a	
    np.count_nonzero(a)	                 # 6
实例6:select-根据条件选择相应的值	
    x = np.arange(10)	
    condlist = [x<3, x>5]	
    choicelist = [x, x**2]	
    np.select(condlist, choicelist)    # array([ 0,  1,  2,  0,  0,  0, 36, 49, 64, 81])	
    np.select(condlist, choicelist,-1) # array([ 0,  1,  2, -1, -1, -1, 36, 49, 64, 81])	
实例7:choose-根据条件选择	
    result=np.array([0,0,0,0])	
    a=np.choose([0,0,1,2],[0,-1,-2,-3,-4],out=result)        #a为1维choices为1维a = array([ 0,  0, -1, -2])   result==a	    	
    b=np.choose([[0,1,2],[3,4,5],[5,4,3]],[0,-1,-2,-3,-4,-5])#a为2维choices为1维	b = array([[ 0, -1, -2],[-3, -4, -5],[-5, -4, -3]])	
            
    c=np.choose([4,3,2,1,0],                                 #a为1维choices为2维	
                 [[0,-1,-2,-3,-4],[10,11,12,13,14],[20,21,22,23,24],[30,31,32,33,34],[40,41,42,43,44]])	
    c   # array([40, 31, 22, 13, -4])    4---0 对应choices(4,0) (3,1) (2,2) (1,3) (0,4)	
        
    d=np.choose([[4,3,2,1,0],[0,1,2,3,4],[0,1,2,3,4]],        #a为2维choices为2维	
                [[0,-1,-2,-3,-4],[10,11,12,13,14],[20,21,22,23,24],[30,31,32,33,34],[40,41,42,43,44]])	
    d   # array([[40, 31, 22, 13, -4], [ 0, 11, 22, 33, 44],[ 0, 11, 22, 33, 44]])	
实例8:where-根据条件选择	
    x = np.arange(9).reshape(3,  3)	
    y = np.where(x >  3)# (array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))	
    x[y]                          # array([4, 5, 6, 7, 8])	
            
    condition = np.mod(x,2) == 0# 定义条件, 选择偶数元素	
    condition                     # array([[True,False,True],[False,True,False],[True,False,True]])	
    np.extract(condition, x)      # array([0., 2., 4., 6., 8.])   # 使用条件提取元素	
实例9:compress-沿轴返回此数组选定切片	    	
    a = np.array([[1, 2], [3, 4], [5, 6]])	
    b1=np.compress([1, 1,0], a, axis=0)   # 按行选取,前为逻辑条件,选取第1,2行	     array([[1, 2],[3, 4]])
    b2=np.compress([4, True], a, axis=1)  # 按列选取,前为逻辑条件,表示选取第1,2列	 array([[1, 2],[3, 4].[5,6]])
            
    #在平面阵列上工作时不会沿着轴返回切片,而是选择元素	
    b3=np.compress([2, True,0,1,4], a)    # 条件为真时选一个元素;逻辑条件最多6个	 array([1, 2, 4, 5])

 

3.备注:	
3.1.np.choose(a,choice,out = None,mode ='raise')
    用途:根据条件选择-从索引数组和一组数组构建一个数组以供选择.	
    说明:np.choose(i_a,a) == np.array([a[i_a[I]] [I] for I in ndi.ndindex(i_a.shape)])   		
    参数:	
        a :     int 数组元素0~n-1	
        choices:要操作数组,维度和a匹配	
        out:    接收运算结果维度和 a 一样	
        mode:   raise默认,a中元素不能超过 n	
        clip:   a 中的元素如小于0将其变为0,如大于n-1变为n-1	
        wrap:   将a中的值 value变为value mod n,即值除以n余数
3.2.numpy.take(a,indices,axis = None,out = None,mode ='raise')
    用途:获取元素-索引工作在展平的目标数组上

    实例:	
        a=np.arange(10,34).resape(2,3,4)	
        indices = [0, 10 23]           #相当于从一维数组【标记从0---23】取值	
        np.take(a, indices)            #array([10, 20, 23])	
        np.take(a, [[0, 1], [2, 3]])   #array([[10, 11],[12, 13]])	
3.3.numpy.put(a,ind,v,mode ='raise')
    用途:替换元素.索引工作在展平的目标数组上.	
    说明:相当于a.flat[ind] = v	

    实例:
        a=np.arange(10,34).reshape(2,3,4)	
        '''
        array([[[10, 11, 12, 13],
                    [14, 15, 16, 17],
                    [18, 19, 20, 21]],

                   [[22, 23, 24, 25],
                    [26, 27, 28, 29],
                    [30, 31, 32, 33]]])		
        '''
        np.put(a,[23],[26*2])#     数组,索引,修改值	
        a
        '''
        array([[[10, 11, 12, 13],
                    [14, 15, 16, 17],
                    [18, 19, 20, 21]],

                   [[22, 23, 24, 25],
                    [26, 27, 28, 29],
                    [30, 31, 32, 52]]])
        '''

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值