numpy之每次看每次忘(还在更新中)

numpy之每次看每次忘(还在更新中)

基本的

ndim, shape, size(元素的总个数), dtype, itemsize(数组中每个元素的字节大小),其它都见名知义

另外关于numpy里面的数据类型也应该知道。

看下面的

>>> import numpy as np
>>> a = np.arange(20)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
>>> type(a[0])
<type 'numpy.int64'>

这说明numpy存的int的数值,默认是int64,是8个节节的, 如果转成int32的话就是4个字节了。 其实int32,float32都是4个字节的,而float64, int64是8个字节的。

常用的

reshape(),可以reshape((m, n))或者reshape(m,n)

np.linspace(0,2,3)

意思是从0到2,产生3个数,结果是array[0.,1.,2.] 注意这个不是linespace,有次我就写错了,还找不到bug。 这个非常好用,类似于range(1,10,2)但是linspace可以产生小数的,等分的时候很好用。

np.random.rand(d0,d1,d2,…)

这个是产生0到1之间的均匀分布的,可以是好几维的,里面不必加括号 和

np.random.random(size)好像是一样的,

np.random.randn(d0,d1,d2,…)

举个例子吧,如果要产生一个均值为3,标准差为4的,(2,2)维的正态分布的话,可以这样 3+ 4 * np.random.randn(2,2)

np.random.randint(low,high=None, size=None, dtype=’i’)

这个常常用到,用于产生从low到high的size个随机的整数,注意是有放回的,不包括high, low如果不写就认为是0. size可以是一维的,也可以是个tuple,比如(3,4)

np.random.choice(a, size=None, replace=True, p=None)

这个也非常的好,常常做随机选择的时候,而且还可以给定以某种概率。 replace=False指的是无放回随机选择,这个应该用的会比较多 比如下面的例子

>>> import numpy as np
>>> np.random.choice(range(20), 10, replace=False)
array([19, 18, 16,  5, 15, 13,  1,  7,  0, 17])
>>> np.random.choice(range(20), 10, replace=True)
array([18, 19,  8,  5, 18, 19, 16,  5, 19, 12])
>>> 


默认的是’True’.

np.newaxis

是添加一个新的维度的意思, 比如 a = np.array(range(10)) 的shape是(10,) 但是如果b = a[:, np.newaxis] 的话,b的shape就是(10,1)了。

np.cumsum(a, axis=None, dtype=None, out=None)

这个非常的好,特别是在算roc, ap, 这此的时候, 意思是累积求和, 所以在求roc,ap的时候比较好用。

见例子

>>> import numpy as np
>>> a = np.array(range(20))
>>> b = np.cumsum(a)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
>>> b
array([  0,   1,   3,   6,  10,  15,  21,  28,  36,  45,  55,  66,  78,
        91, 105, 120, 136, 153, 171, 190])

np.clip(a, a_min, a_max, out=None)

这个是clip一个数组中的值,如果小于a_min的话,就变成a_min,大于a_max的话就变成a_max,

见下面的例子

>>> import numpy as np
>>>
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 3,7)
array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 3,7, a)
array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])
>>> a
array([3, 3, 3, 3, 4, 5, 6, 7, 7, 7])
>>>

其中最后一个参数是out,如果out为自身的话,那这个值将会改变。

np.meshgrid()

见下面的例子。

>>> a = np.arange(3)
>>> b = np.linspace(0,1,5)
>>> np.meshgrid(a,b)
[array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]]), array([[0.  , 0.  , 0.  ],
       [0.25, 0.25, 0.25],
       [0.5 , 0.5 , 0.5 ],
       [0.75, 0.75, 0.75],
       [1.  , 1.  , 1.  ]])]
>>> c, d = np.meshgrid(a,b)
>>> c
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> d
array([[0.  , 0.  , 0.  ],
       [0.25, 0.25, 0.25],
       [0.5 , 0.5 , 0.5 ],
       [0.75, 0.75, 0.75],
       [1.  , 1.  , 1.  ]])

也就是把meshgrid的返回值的对应的位置配成坐标的话,可以当成grid来看。

np.copy(a, order=’K’)

这个感觉就像是deepcopy,看下面的例子

>>> a = np.array([1,2,3])
>>> a
array([1, 2, 3])
>>> b = a
>>> a[0] =100
>>> b
array([100,   2,   3])
>>> c = np.copy(a)
>>> c
array([100,   2,   3])
>>> a[1]=200
>>> a
array([100, 200,   3])
>>> b
array([100, 200,   3])
>>> c
array([100,   2,   3])
>>>

它的第二个参数,可以指定是按照C语言的形式来存还是按照其它的方式,default是’K’,意思是 match the layout of a as closely as possible.

np.where(a>10,1,0) 还可以这样用

看例子

>>> import numpy as np
>>> a = np.array(range(20)).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a[:,:]= np.where(a>10,1,0)
>>> a
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1],
       [1, 1, 1, 1, 1]])

这在产生mask的时候就比较方便了。

np.tofile(fid, sep=””, format=”%s”), np.fromfile()

这个可以写入二进制文件,filename可以是.bin结尾的,也可以是.txt结尾的 看下面的例子。

>>> import numpy as np
>>> a = np.random.random((10,10))
>>> a
array([[0.59281993, 0.3239744 , 0.40916617, 0.7026653 , 0.73275024,
        0.21081999, 0.53366245, 0.10331709, 0.32546868, 0.22810421],
       [0.98428505, 0.94670514, 0.63903532, 0.27788937, 0.88567724,
        0.37963127, 0.44450239, 0.69552021, 0.9621253 , 0.03284991],
       [0.42321447, 0.05881487, 0.06551279, 0.2043375 , 0.88272984,
        0.24789873, 0.87246889, 0.10402341, 0.12713767, 0.33367603],
       [0.11624641, 0.75637676, 0.20271353, 0.5155519 , 0.36666372,
        0.39928505, 0.30223019, 0.86106991, 0.17976945, 0.83550575],
       [0.98105967, 0.81721926, 0.77524547, 0.36720453, 0.94334179,
        0.59794438, 0.98942932, 0.0531472 , 0.23519734, 0.7861395 ],
       [0.4437484 , 0.50221219, 0.52620174, 0.62602009, 0.91305105,
        0.98763546, 0.24418486, 0.56778355, 0.79686608, 0.39581413],
       [0.7328945 , 0.57456536, 0.18012771, 0.81519295, 0.01466615,
        0.03374568, 0.40865905, 0.70241457, 0.30494482, 0.63398954],
       [0.46405281, 0.5332063 , 0.18604028, 0.26061367, 0.76300291,
        0.62996246, 0.21587994, 0.32441372, 0.63741871, 0.24269805],
       [0.43275035, 0.41381104, 0.4914946 , 0.47221879, 0.07892972,
        0.53239343, 0.55639538, 0.62165555, 0.48979182, 0.94992944],
       [0.44856271, 0.63900666, 0.1354305 , 0.21257494, 0.93571004,
        0.27395649, 0.68330413, 0.06238116, 0.60970981, 0.23192754]])
>>> a.tofile("a.bin")
>>> b = np.fromfile("a.bin")
>>> b.shape
(100,)
>>> b.reshape((10,10))
array([[0.59281993, 0.3239744 , 0.40916617, 0.7026653 , 0.73275024,
        0.21081999, 0.53366245, 0.10331709, 0.32546868, 0.22810421],
       [0.98428505, 0.94670514, 0.63903532, 0.27788937, 0.88567724,
        0.37963127, 0.44450239, 0.69552021, 0.9621253 , 0.03284991],
       [0.42321447, 0.05881487, 0.06551279, 0.2043375 , 0.88272984,
        0.24789873, 0.87246889, 0.10402341, 0.12713767, 0.33367603],
       [0.11624641, 0.75637676, 0.20271353, 0.5155519 , 0.36666372,
        0.39928505, 0.30223019, 0.86106991, 0.17976945, 0.83550575],
       [0.98105967, 0.81721926, 0.77524547, 0.36720453, 0.94334179,
        0.59794438, 0.98942932, 0.0531472 , 0.23519734, 0.7861395 ],
       [0.4437484 , 0.50221219, 0.52620174, 0.62602009, 0.91305105,
        0.98763546, 0.24418486, 0.56778355, 0.79686608, 0.39581413],
       [0.7328945 , 0.57456536, 0.18012771, 0.81519295, 0.01466615,
        0.03374568, 0.40865905, 0.70241457, 0.30494482, 0.63398954],
       [0.46405281, 0.5332063 , 0.18604028, 0.26061367, 0.76300291,
        0.62996246, 0.21587994, 0.32441372, 0.63741871, 0.24269805],
       [0.43275035, 0.41381104, 0.4914946 , 0.47221879, 0.07892972,
        0.53239343, 0.55639538, 0.62165555, 0.48979182, 0.94992944],
       [0.44856271, 0.63900666, 0.1354305 , 0.21257494, 0.93571004,
        0.27395649, 0.68330413, 0.06238116, 0.60970981, 0.23192754]])
>>>

也可以

>>> a.tofile("a.txt")
>>> b = np.fromfile("a.txt")
>>> b.reshape((10,10))
array([[0.59281993, 0.3239744 , 0.40916617, 0.7026653 , 0.73275024,
        0.21081999, 0.53366245, 0.10331709, 0.32546868, 0.22810421],
       [0.98428505, 0.94670514, 0.63903532, 0.27788937, 0.88567724,
        0.37963127, 0.44450239, 0.69552021, 0.9621253 , 0.03284991],
       [0.42321447, 0.05881487, 0.06551279, 0.2043375 , 0.88272984,
        0.24789873, 0.87246889, 0.10402341, 0.12713767, 0.33367603],
       [0.11624641, 0.75637676, 0.20271353, 0.5155519 , 0.36666372,
        0.39928505, 0.30223019, 0.86106991, 0.17976945, 0.83550575],
       [0.98105967, 0.81721926, 0.77524547, 0.36720453, 0.94334179,
        0.59794438, 0.98942932, 0.0531472 , 0.23519734, 0.7861395 ],
       [0.4437484 , 0.50221219, 0.52620174, 0.62602009, 0.91305105,
        0.98763546, 0.24418486, 0.56778355, 0.79686608, 0.39581413],
       [0.7328945 , 0.57456536, 0.18012771, 0.81519295, 0.01466615,
        0.03374568, 0.40865905, 0.70241457, 0.30494482, 0.63398954],
       [0.46405281, 0.5332063 , 0.18604028, 0.26061367, 0.76300291,
        0.62996246, 0.21587994, 0.32441372, 0.63741871, 0.24269805],
       [0.43275035, 0.41381104, 0.4914946 , 0.47221879, 0.07892972,
        0.53239343, 0.55639538, 0.62165555, 0.48979182, 0.94992944],
       [0.44856271, 0.63900666, 0.1354305 , 0.21257494, 0.93571004,
        0.27395649, 0.68330413, 0.06238116, 0.60970981, 0.23192754]])

np.append()

这个相当于数组的拼接,因为有时候都在和numpy打交道,所以这个函数还是很方便的。

见下面的几个例子

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> np.append(a,[4])
array([1, 2, 3, 4])
>>>

>>> a = np.zeros((0,3))
>>> np.append(a, [1,2,3])
array([1., 2., 3.])

 a = np.append([1,2,3], [[3,4,5], [6,7,8]])
>>> a
array([1, 2, 3, 3, 4, 5, 6, 7, 8])
>>> np.append([[1,2,3],[4,5,6]], [7,8,9])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> np.append([[1,2,3],[4,5,6]], [[7,8,9]], axis=0)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

np.expand_dims()

扩展一个维度,从低维到高维

>>> a = np.ones((3,4))
>>> a
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
>>> np.expand_dims(a, axis=0)
array([[[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]])
>>> a = np.expand_dims(a, axis=0)
>>> a.shape
(1, 3, 4)
>>>

np.random.permutation

np.random.permutation(10)
array([4, 3, 7, 2, 9, 5, 8, 6, 1, 0])
np.random.permutation(20)
array([ 5, 13, 14,  8, 18,  4,  9,  1,  7,  6, 12, 19, 16, 11,  2, 15,  0,
       17, 10,  3])

np.logspace(start, stop, num=50, base=10.0, axis=0, endpoint=True)

这个是产生等比数列的意思,意思是从base**startbase**stop产生num个数,endpoint=True意思是包括端点,

比如

np.logspace(1, 10, 10, base=2)
array([    2.,     4.,     8.,    16.,    32.,    64.,   128.,   256.,
         512.,  1024.])



np.logspace(1, 10, 10, base=2, endpoint=False)
array([   2.        ,    3.73213197,    6.96440451,   12.99603834,
         24.25146506,   45.254834  ,   84.44850629,  157.58648491,
        294.06677888,  548.74801282])
np.logspace(0, 10, 10, base=2, endpoint=False)
array([   1.,    2.,    4.,    8.,   16.,   32.,   64.,  128.,  256.,  512.])


np.arctan2与np.arctan

>>> import numpy as np
>>> np.arctan(1.732)
1.0471848490249274
>>> np.arctan2(1.732,1)
1.0471848490249271
>>> 

np.concatenate 函数

最近在将一些图片合成到同一个图中的时候用到了这个函数,

直接可以

a = np.concatenate(a,b)

这样来递归地增加a的内容,而不需要提前申请好总图的大小.

打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦