Numpy-基础


目录

Python list Python array Numpy.ndarray nparr的优势 矩阵和随机数的生成 np.zeros 生成随机数 ndarray基础操作 取值操作 切片 二维数组切片 矩阵的合并 聚合操作

import numpy as np

In [2]:

np.__version__

Out[2]:

'1.18.1'

Python list

In [4]:

li = list(range(10))
li

Out[4]:

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

In [5]:

li[5] = 'hello python'

In [6]:

li

Out[6]:

[0, 1, 2, 3, 4, 'hello python', 6, 7, 8, 9]

Python array

In [7]:

import array

In [8]:

arr = array.array('i',list(range(10)))
arr

Out[8]:

array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [9]:

arr[5] = 'hello python'
------------------------------------------------
TypeError      Traceback (most recent call last)
<ipython-input-9-b1067373fade> in <module>
----> 1 arr[5] = 'hello python'

TypeError: an integer is required (got type str)

In [10]:

arr[5] = 78

In [11]:

arr

Out[11]:

array('i', [0, 1, 2, 3, 4, 78, 6, 7, 8, 9])

In [12]:

# array的效率比list高
# 多维了array计算就不方便了,不如numpy的ndarray

Numpy.ndarray

In [15]:

nparr = np.array(list(range(10)))
nparr

Out[15]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [16]:

type(nparr)

Out[16]:

numpy.ndarray

In [17]:

nparr.dtype # 元素类型

Out[17]:

dtype('int64')

In [18]:

nparr[3] = 33

In [19]:

nparr

Out[19]:

array([ 0,  1,  2, 33,  4,  5,  6,  7,  8,  9])

In [20]:

nparr[3]

Out[20]:

33

In [21]:

nparr[5] = 'hello python'
------------------------------------------------
ValueError     Traceback (most recent call last)
<ipython-input-21-9b90ad2c54c6> in <module>
----> 1 nparr[5] = 'hello python'

ValueError: invalid literal for int() with base 10: 'hello python'

In [22]:

nparr[5] = 55.23

In [23]:

nparr

Out[23]:

array([ 0,  1,  2, 33,  4, 55,  6,  7,  8,  9])

In [24]:

# 发现小数部分被去掉了变成了整数!
# 整数类型的ndarray 放入小数 截取小数点前的整数

In [25]:

nparr2 = np.array([1,2,3.0])

In [26]:

nparr2

Out[26]:

array([1., 2., 3.])

In [27]:

nparr2.dtype

Out[27]:

dtype('float64')

In [28]:

nparr3 = np.array([1,2,3],dtype=float)

In [29]:

nparr3

Out[29]:

array([1., 2., 3.])

In [30]:

nparr3.dtype

Out[30]:

dtype('float64')

nparr的优势

  1. 方便处理多维度数组或者矩阵之间的运算
  2. 在处理多维度数组或者矩阵时,运算效率高

In [32]:

def python_test(n):
    a = [i**2 for i in range(n)]
    b = [i**3 for i in range(n)]
    c = []
    for i in range(n):
        c.append(a[i]+b[i])
    return c
python_test(10)

Out[32]:

[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]

In [37]:

def numpy_test(n):
    a = np.arange(n) ** 2 # 矩阵的平方就是对其中每个元素平方
    b = np.arange(n) ** 3
    c = a + b
    return c
numpy_test(10)

Out[37]:

array([  0,   2,  12,  36,  80, 150, 252, 392, 576, 810])

In [38]:

%time res = python_test(10000000)
CPU times: user 7.22 s, sys: 590 ms, total: 7.81 s
Wall time: 7.87 s

In [39]:

%time res = numpy_test(10000000)
CPU times: user 249 ms, sys: 142 ms, total: 391 ms
Wall time: 392 ms

矩阵和随机数的生成

In [40]:

np.array([1,2,3])

Out[40]:

array([1, 2, 3])

In [41]:

np.array(range(10))

Out[41]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [42]:

np.arange(10)

Out[42]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [43]:

np.arange(2,20,3)

Out[43]:

array([ 2,  5,  8, 11, 14, 17])

In [44]:

np.arange(2,20,0.3)

Out[44]:

array([ 2. ,  2.3,  2.6,  2.9,  3.2,  3.5,  3.8,  4.1,  4.4,  4.7,  5. ,
        5.3,  5.6,  5.9,  6.2,  6.5,  6.8,  7.1,  7.4,  7.7,  8. ,  8.3,
        8.6,  8.9,  9.2,  9.5,  9.8, 10.1, 10.4, 10.7, 11. , 11.3, 11.6,
       11.9, 12.2, 12.5, 12.8, 13.1, 13.4, 13.7, 14. , 14.3, 14.6, 14.9,
       15.2, 15.5, 15.8, 16.1, 16.4, 16.7, 17. , 17.3, 17.6, 17.9, 18.2,
       18.5, 18.8, 19.1, 19.4, 19.7])

np.zeros

In [45]:

np.zeros(10)

Out[45]:

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [46]:

np.zeros(10,dtype=int)

Out[46]:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [48]:

# shape keyword
np.zeros(shape=(3,5),dtype=int) # 3行5列的矩阵

Out[48]:

array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

In [49]:

# np.ones
np.ones(10)

Out[49]:

array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

In [50]:

np.ones(shape=(3,5))

Out[50]:

array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

In [51]:

# np.full
np.full(10,99)

Out[51]:

array([99, 99, 99, 99, 99, 99, 99, 99, 99, 99])

In [53]:

np.full((3,5),99)

Out[53]:

array([[99, 99, 99, 99, 99],
       [99, 99, 99, 99, 99],
       [99, 99, 99, 99, 99]])

In [54]:

# 等差数列
np.linspace(0,20,10)

Out[54]:

array([ 0.        ,  2.22222222,  4.44444444,  6.66666667,  8.88888889,
       11.11111111, 13.33333333, 15.55555556, 17.77777778, 20.        ])

生成随机数

In [55]:

np.random.randint(0,10)

Out[55]:

7

In [56]:

np.random.randint(0,10,size=5)

Out[56]:

array([4, 5, 1, 9, 0])

In [57]:

np.random.randint(0,10,size=(2,5))

Out[57]:

array([[2, 6, 5, 1, 0],
       [2, 8, 1, 6, 0]])

In [58]:

np.random.randint(0,10,size=(2,5,6))

Out[58]:

array([[[0, 5, 3, 5, 7, 2],
        [4, 0, 3, 5, 3, 7],
        [8, 3, 1, 7, 7, 3],
        [5, 2, 5, 9, 8, 5],
        [8, 3, 8, 9, 6, 2]],

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

In [60]:

np.random.randint(1,10,5) #每次运行都不一样

Out[60]:

array([3, 6, 1, 3, 4])

In [62]:

# 要求随机数生成一致
np.random.seed(99)
np.random.randint(1,10,5)

Out[62]:

array([2, 4, 9, 9, 3])

In [64]:

np.random.random() # 0-1的float

Out[64]:

0.19331310365809062

In [65]:

np.random.random((3,5))

Out[65]:

array([[0.97734806, 0.57772631, 0.51376629, 0.85151867, 0.38105394],
       [0.27389368, 0.64060144, 0.49361568, 0.33677231, 0.61632181],
       [0.36050726, 0.33167662, 0.6274314 , 0.69347639, 0.32690352]])

ndarray基础操作

In [66]:

A = np.ones(shape=(3,5))

In [67]:

A

Out[67]:

array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

In [68]:

A.ndim #获取数组维度

Out[68]:

2

In [69]:

A.shape

Out[69]:

(3, 5)

In [70]:

A.size # 总元素个数

Out[70]:

15

In [71]:

# reshape 修改数组维度
a = np.arange(10)
a

Out[71]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [73]:

X = a.reshape((2,5))
X

Out[73]:

array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In [74]:

a.reshape(2,-1) # 我要2行,多少列扔给np自己算

Out[74]:

array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In [76]:

X = a.reshape(-1,2) # 我要2列,多少行我不管
X

Out[76]:

array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

取值操作

In [77]:

X = np.arange(15).reshape(3,5)

In [78]:

X

Out[78]:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [79]:

X[0]

Out[79]:

array([0, 1, 2, 3, 4])

In [80]:

X[-1]

Out[80]:

array([10, 11, 12, 13, 14])

In [82]:

X[0][1] # 不推荐

Out[82]:

1

In [85]:

X[(0,1)] # 推荐

Out[85]:

1

In [86]:

X[0,1] # 省略括号

Out[86]:

1

切片

In [87]:

a = np.arange(10)
a

Out[87]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [88]:

a[0:5]

Out[88]:

array([0, 1, 2, 3, 4])

In [89]:

a[:5]

Out[89]:

array([0, 1, 2, 3, 4])

In [90]:

a[5:]

Out[90]:

array([5, 6, 7, 8, 9])

In [91]:

a[0:8:2]

Out[91]:

array([0, 2, 4, 6])

In [92]:

a[::2]

Out[92]:

array([0, 2, 4, 6, 8])

In [93]:

a[::-1]

Out[93]:

array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

二维数组切片

In [94]:

X

Out[94]:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [95]:

# 前3列前2行
X[0:2,0:3]

Out[95]:

array([[0, 1, 2],
       [5, 6, 7]])

In [96]:

X[:2,:3]

Out[96]:

array([[0, 1, 2],
       [5, 6, 7]])

In [98]:

X[:2][:3] #取前2行,又取前3行,所以上面不推荐这种写法来取值

Out[98]:

array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In [99]:

X[:2,::2] # 取前2行,然后列数以步长为2取

Out[99]:

array([[0, 2, 4],
       [5, 7, 9]])

In [100]:

X[::-1,::-1]

Out[100]:

array([[14, 13, 12, 11, 10],
       [ 9,  8,  7,  6,  5],
       [ 4,  3,  2,  1,  0]])

In [101]:

X

Out[101]:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [104]:

# 转置
X.T

Out[104]:

array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14]])

矩阵的合并

In [105]:

X1 = np.array([[22,168],
               [18,187]])

In [107]:

X2 = np.array([[0],
              [1]])

In [108]:

X = np.concatenate([X1,X2],axis=1) # axis =0行 =1 列

In [109]:

X

Out[109]:

array([[ 22, 168,   0],
       [ 18, 187,   1]])

聚合操作

In [110]:

X = np.arange(15).reshape(3,5)

In [111]:

X

Out[111]:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

In [112]:

np.sum(X)

Out[112]:

105

In [113]:

np.max(X)

Out[113]:

14

In [114]:

np.min(X)

Out[114]:

0

In [115]:

np.mean(X)

Out[115]:

7.0