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

# 小郑之家~

### 基本的

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

>>> 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'>



### 常用的

reshape（），可以reshape((m, n))或者reshape(m,n)

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

>>> 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])
>>>



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

>>> 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)

>>> 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])
>>>



### 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.  ]])


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

>>> 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])
>>>


### 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]])



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

>>> 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()

>>> 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)

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)