numpy中bincount、repeat、newaxis以及assert的用法详解

一、numpy.bincount详解

先给出官网上的解释。

这里写图片描述

函数的功能:统计输入数组中每个元素出现的次数。当然输入数组中的每个元素必须是非负的。

函数的参数: X : 一维数组的形式,数组元素需是非负整数
weights: 数组,与输入数组X拥有相同的shape,可选参数
minlength: int,限定了返回中数组的大小

函数的返回值: out :一个一维数组。如果函数没有传入minlength参数,则返回值数组的大小为输入数组中元素的最大值加1,即 np.amax(x)+1

函数所引发的异常: 数值异常:输入数组不是一维的。输入数组里面的值有负值,或者minlength的值为负数。
类型异常: 输入数组为float类型、复数类型

numpy.bincount( x , weights=None ,minlength=0)的作用原理:

# example one
>>> import numpy as np
>>> a1=np.arange(5)
>>> a1
array([0, 1, 2, 3, 4])  
# 统计数组中每个位置的元素出现的次数。
# 首先 返回数组的长度为 数组元素中的最大值 + 1  = 4+1=5
# 观察a1数组,可以发现,0 出现的次数为 1 次;1 出现的次数 为 1 次;,,,;4出现的次数为 1 次。
# 在返回数组中的数据相应位置填上它们各自出现的次数。
>>> np.bincount(a1)
array([1, 1, 1, 1, 1])


# example two

# length of output array = 10+1 = 11
# calculate the  frequency of every element in the input array
>>> a2=np.array([3,7,8,5,4,10,3,5,7,1])
>>> np.bincount(a2)
array([0, 1, 0, 2, 1, 2, 0, 2, 1, 0, 1])
>>> len(np.bincount(a2))
11



# 输出数组的每个元素-------每个元素下标在X中出现的次数。
# 如果weight参数值给定,那么当输出数组下标对应的元素在x找到一次时,
# 那么这个下标元素的count=count + weight ,而不是默认的count= count +1 
# Each bin gives the number of occurrences of its index value in x. 
# If weights is specified the input array is weighted by it, 
# i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1

二、numpy.repeat详解

numpy;repeat( )函数的功能:对输入数组的元素进行重复的复制。

numpy(a1 , repeats , axis=None):对输入数组a1沿着axis指定的轴将此轴的元素进行复制repeats 次

由于数组不能进行动态扩展,故函数调用之后都重新分配新的空间来存储扩展后的数据。

下面举例说明:

>>> re1=np.arange(16).reshape(2,2,4)
>>> re1
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=0)  # axis=0 ,则在 0 轴上的每次元素都各自沿 0 轴 复制 3 次
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=1)    # axis=1 ,则在 1 轴上的每次元素都各自沿 1 轴 复制 3 次
array([[[ 0,  1,  2,  3],
        [ 0,  1,  2,  3],
        [ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 4,  5,  6,  7],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [ 8,  9, 10, 11],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [12, 13, 14, 15],
        [12, 13, 14, 15]]])
>>> np.repeat(re1,3,axis=2)    # axis=2 ,则在 2 轴上的每个元素都各自沿 2 轴 复制 3 次
array([[[ 0,  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  3],
        [ 4,  4,  4,  5,  5,  5,  6,  6,  6,  7,  7,  7]],

       [[ 8,  8,  8,  9,  9,  9, 10, 10, 10, 11, 11, 11],
        [12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15]]])   

# 经过一系列的操作之后,那么最后来看看re1的数组变化了没有

>>> re1
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])
# 发现并没有什么变化,这说明numpy.repeat操作是返回的一个新数组,原数组不会发生变化。

三、numpy中的newaxis详解

numpy中包含的 newaxis 可以给数组增加一个维度

newaxis放置的位置不同,产生的新数组也不同

# 下面来举例说明:
# example one  
# 对于一维数组
>>> new1=np.arange(4)
>>> new1
array([0, 1, 2, 3])
>>> new1[np.newaxis,:]     # 在行上增加一个维度
array([[0, 1, 2, 3]])
>>> new1[:,np.newaxis]     # 在列上增加一个维度
array([[0],
       [1],
       [2],
       [3]])
# example two       
# 对于二维数组
>>> new2=np.arange(8).reshape(2,4)
>>> new2
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
>>> new2[np.newaxis,:]  # 在原始数组的第一维之前增加一维
array([[[0, 1, 2, 3],
        [4, 5, 6, 7]]])
>>> new2[:,np.newaxis,:] # 在原始数组的第一维之后,第二维之前
array([[[0, 1, 2, 3]],

       [[4, 5, 6, 7]]])
>>> new2[:,:,np.newaxis]  # 在原始数组的第二维之后增加一维。
array([[[0], 
        [1],
        [2],
        [3]],

       [[4],
        [5],
        [6],
        [7]]])

# 再来看看它们分别增加维度之后的shape

>>> new2[np.newaxis,:].shape
(1, 2, 4)
>>> new2[:,np.newaxis,:].shape
(2, 1, 4)
>>> new2[:,:,np.newaxis].shape
(2, 4, 1)
>>> 
# 总结: 将newaxis之后的每个大元素增加一个[ ]在外面就好了。
# 例如: new2[:,:,np.newaxis] 在原始数组的最后一个维度的每个元素加上[ ],并且用逗号分隔。

四、python中assert的详解
作用:在没有完成一个程序之前,我们不知道它会在哪里出错。与其让一个程序在运行时崩溃,还不如在出现错误条件时就崩溃。这就需要python中assert断言的作用。
python 的 assert声明的表达式的布尔值必须为真,若表达式的值为假就会触发异常,可以为断言添加异常参数。

用法如下:

>>> x=1
>>> y=1
>>> assert x==y   # 表达式为真,则程序正常执行,没有assert异常抛出
>>> x=1
>>> y=3
>>> assert x==y   # 表达式为假,则程序崩溃,assert抛出异常

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    assert x==y
AssertionError   # 这里表示断言异常
>>> x=1
>>> y=7
>>> assert x==y , "x is not equal to y ,please check the value of x,y"  # 给断言添加了异常参数,若前面的表达式的值为假,则断言的异常参数会随异常一起打印出来

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    assert x==y , "x is not equal to y ,please check the value of x,y"
AssertionError: x is not equal to y ,please check the value of x,y  # 异常参数被打印了出来

今天学习到这里。明天见。。。。

猜你喜欢

转载自blog.csdn.net/xiongchengluo1129/article/details/79213303