import numpy as np
import pandas as pd
from pandas import Series, DataFrame
ndarray
一、创建ndarray
data = [1 ,2 ,3 ,4 ]
n1 = np.array(data)
n1
array([1, 2, 3, 4])
np.ones(5 ,dtype=int)
array([1, 1, 1, 1, 1])
np.zeros(5 ,dtype=int)
array([0, 0, 0, 0, 0])
np.full(shape=(3 ,4 ),fill_value=1024 )
array([[1024, 1024, 1024, 1024],
[1024, 1024, 1024, 1024],
[1024, 1024, 1024, 1024]])
np.eye(N=4 ,M=3 )
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.],
[ 0., 0., 0.]])
np.linspace(1 ,100 ,num=50 ,dtype=int)
array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51,
53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77,
79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 100])
np.arange(0 ,100 ,step=5 )
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95])
np.random.randint(0 ,100 ,size=(4 ,3 ))
array([[70, 19, 93],
[24, 86, 92],
[77, 99, 88],
[18, 55, 29]])
np.random.randn(100 )
array([-0.07352457, -1.3136685 , 1.51471995, -0.08136916, -1.11574765,
0.17386269, -1.31727833, -0.41155414, 1.30430032, -0.86782806,
-0.76215743, -0.50927113, 1.52628811, -0.18189482, -0.02033947,
1.97168795, 0.24356417, 0.98391052, -0.41973603, 0.32262384,
-1.26531272, 0.51957893, -0.53221152, -0.18077624, -0.04793903,
-1.07987469, -0.14964406, -1.50501509, 0.22443113, 1.02252398,
1.8288228 , 0.14265383, -1.07763155, -1.45768047, 0.57438741,
-0.02607326, 0.76216751, -0.21004165, -1.45035988, -2.21018297,
0.18629033, 0.51804587, 0.6108886 , -1.79317464, -0.01333462,
-1.02223808, 1.02241954, -0.21770695, -0.13238381, -2.43526884,
1.77094614, 1.58023618, 0.86634068, 0.63893764, -0.09904435,
0.19179536, 0.70664661, 0.18497984, 1.06407283, 0.08213135,
0.1189964 , -0.05590335, -0.73709569, -0.14316891, -0.90192259,
0.53208371, 1.52754737, -0.06674151, 1.07920869, 0.42858941,
-1.15588987, -1.10701065, 0.01149264, -1.36904717, -0.0157708 ,
0.81281272, -0.87613834, -0.05256806, 0.36815293, -1.19499011,
1.28316201, 0.68097169, -0.02886232, -0.45725581, 2.17369421,
0.67138163, 1.7039416 , 0.16232207, -0.3319481 , 0.8932495 ,
-1.67827068, -1.61199462, 0.60000361, -1.52476205, -0.28960245,
0.98590361, -0.67797509, -1.03187526, -0.84967747, 0.11290939])
np.random.normal(10 ,5 ,10 )
array([ 8.41062812, 7.48295308, 17.81455213, 15.14675061,
3.74643964, 10.79360893, 5.2854533 , 16.02048099,
3.22449646, 3.80641927])
np.random.random(10 )
array([ 0.68289218, 0.80123252, 0.91247595, 0.73536506, 0.867282 ,
0.89442711, 0.06441671, 0.80846567, 0.77142565, 0.19021568])
二、ndarray的属性
n = np.random.randint(100 ,size=(4 ,4 ))
display(n.ndim, n.shape, n.size, n.dtype)
2
(4, 4)
16
dtype('int64')
三、ndarray的基本操作
n = np.random.randint(100 ,size=(4 ,4 ,4 ))
n
array([[[16, 66, 17, 90],
[97, 29, 56, 95],
[10, 97, 63, 22],
[39, 83, 25, 89]],
[[52, 88, 36, 20],
[64, 25, 51, 78],
[48, 95, 87, 63],
[97, 73, 83, 55]],
[[16, 16, 7, 51],
[10, 99, 83, 10],
[45, 35, 5, 20],
[68, 71, 23, 7]],
[[81, 20, 55, 63],
[ 8, 39, 9, 40],
[66, 6, 79, 92],
[10, 79, 94, 36]]])
n[0 ,0 ,0 ]
16
n[:]
array([[[16, 66, 17, 90],
[97, 29, 56, 95],
[10, 97, 63, 22],
[39, 83, 25, 89]],
[[52, 88, 36, 20],
[64, 25, 51, 78],
[48, 95, 87, 63],
[97, 73, 83, 55]],
[[16, 16, 7, 51],
[10, 99, 83, 10],
[45, 35, 5, 20],
[68, 71, 23, 7]],
[[81, 20, 55, 63],
[ 8, 39, 9, 40],
[66, 6, 79, 92],
[10, 79, 94, 36]]])
n[:,0 :2 ]
array([[[16, 66, 17, 90],
[97, 29, 56, 95]],
[[52, 88, 36, 20],
[64, 25, 51, 78]],
[[16, 16, 7, 51],
[10, 99, 83, 10]],
[[81, 20, 55, 63],
[ 8, 39, 9, 40]]])
n[::-1 ]
array([[[81, 20, 55, 63],
[ 8, 39, 9, 40],
[66, 6, 79, 92],
[10, 79, 94, 36]],
[[16, 16, 7, 51],
[10, 99, 83, 10],
[45, 35, 5, 20],
[68, 71, 23, 7]],
[[52, 88, 36, 20],
[64, 25, 51, 78],
[48, 95, 87, 63],
[97, 73, 83, 55]],
[[16, 66, 17, 90],
[97, 29, 56, 95],
[10, 97, 63, 22],
[39, 83, 25, 89]]])
n = np.random.randint(100 ,size=16 ).reshape((4 ,4 ))
n
array([[67, 11, 6, 41],
[51, 9, 30, 73],
[98, 75, 52, 84],
[34, 17, 2, 19]])
n1 = np.random.randint(100 , size=(4 ,4 ))
n2 = np.random.randint(100 , size=(4 ,4 ))
display(n1,n2)
array([[78, 0, 3, 0],
[28, 72, 92, 59],
[46, 14, 97, 63],
[15, 45, 2, 63]])
array([[85, 61, 30, 21],
[59, 66, 66, 36],
[87, 72, 15, 72],
[11, 33, 12, 16]])
np.concatenate((n1,n2))
array([[78, 0, 3, 0],
[28, 72, 92, 59],
[46, 14, 97, 63],
[15, 45, 2, 63],
[85, 61, 30, 21],
[59, 66, 66, 36],
[87, 72, 15, 72],
[11, 33, 12, 16]])
np.concatenate((n1,n2),axis=1 )
array([[78, 0, 3, 0, 85, 61, 30, 21],
[28, 72, 92, 59, 59, 66, 66, 36],
[46, 14, 97, 63, 87, 72, 15, 72],
[15, 45, 2, 63, 11, 33, 12, 16]])
n_stack = np.hstack(n1)
n_stack
array([78, 0, 3, 0, 28, 72, 92, 59, 46, 14, 97, 63, 15, 45, 2, 63])
np.vstack(n_stack)
array([[78],
[ 0],
[ 3],
[ 0],
[28],
[72],
[92],
[59],
[46],
[14],
[97],
[63],
[15],
[45],
[ 2],
[63]])
>>>a = np.random.randint(0 , 10 , size=20 )
>>>np.split(a, [3 ,5 ])
[array([3 , 4 , 4 ]),
array([4 , 8 ]),
array([4 , 6 , 4 , 9 , 8 , 7 , 2 , 1 , 9 , 6 , 0 , 4 , 2 , 8 , 6 ]]
>>>b = np.random.randint(0 ,10 ,size = 20 ).reshape((4 ,5 ))
>>>b
array([[6 , 0 , 1 , 0 , 7 ],
[3 , 3 , 5 , 9 , 4 ],
[4 , 9 , 5 , 4 , 5 ],
[5 , 6 , 9 , 5 , 3 ]])
>>>np.vsplit(b,(1 ,3 ))
[array([[6 , 0 , 1 , 0 , 7 ]]), array([[3 , 3 , 5 , 9 , 4 ],
[4 , 9 , 5 , 4 , 5 ]]), array([[5 , 6 , 9 , 5 , 3 ]])]
>>>np.hsplit(b, (2 ,3 ))
[array([[6 , 0 ],
[3 , 3 ],
[4 , 9 ],
[5 , 6 ]]), array([[1 ],
[5 ],
[5 ],
[9 ]]), array([[0 , 7 ],
[9 , 4 ],
[4 , 5 ],
[5 , 3 ]])]
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
>>>b = np.array([1 , 2 , None ])
>>>b
array([1 , 2 , None ], dtype=object)
>>>c = np.array([1 ,10 ,np.nan])
>>>np.nansum()
11.0
>>>a = np.array([[-3 ,0 ],[5 ,0.5 ]])
>>>b = np.array([[-7 ,2 ],[4 ,6 ]])
>>>display(np.dot(a,b),a*b)
array([[ 21. , -6. ],
[-33. , 13. ]])
array([[ 21. , 0. ],
[ 20. , 3. ]])