攜手創作,共同成長!這是我參與「掘金日新計劃 · 8 月更文挑戰」的第5天,點擊查看活動詳情
NumPyProvide the main object isMultidimensional array of the same data type(homogeneous multidimensional array).He can actually be understood as a table element 通常是數字 所有元素具有相同的類型 These elements can use tuples of positive integers(tuple)來索引 .在NumPy中,The dimensions are called軸(axes).The total number of axes is called秩(rank).
舉例子,The coordinates of a point in three-dimensional space are[1 , 2, 1],它就是一個rank是1的一個數組.因為它只有一個axis,這個axis的長度是3. [[1., 0., 0.], [0., 1., 2.]] 該數組的rank是2(兩個維度),The length of the first dimension is2(2行),The second dimension is3(3列).
NumPy的數組叫做ndarray.Also has an aliasarray.numpy.array和 Python標准庫中的array.array完全不是一個東西,The latter only supports one-dimensional arrays and provides less functionality.ndaaryThe more important properties of objects are:
ndarray.ndim 數組的axes的數目
ndarray.shape 數組的形狀,如一個 n*m 的矩陣,它的shape就是(n,m).而且len(ndarray.shape)==ndarray.ndim
ndarray.size 數組中所有元素的個數.它實際上是ndarray.shapeproduct of all values in.
ndarray.dtype An object describing the type of elements in an array.我們可以使用Pythonstandard types to create or specifydtype.不過NumPyalso provides some of its own types,比如:numpy.int32,numpy.int16,numpy.float64 ...
ndarray.itemsize The size in bytes of a single element in the array.比如類型是float64,則itemsize就是8=(64/8).它跟ndarray.dtype.itemsize是等價的.
ndarray.data Store the actual data in the array.Usually we don't use this property directly,Because indexing functionality is almost always provided when we need to access data.
import numpy as np
a = np.arange(15).reshape(3,5)
復制代碼a.shape
復制代碼(3, 5)
復制代碼a.ndim
復制代碼2
復制代碼a.dtype.name, a.itemsize
復制代碼('int32', 4)
復制代碼type(a)
復制代碼numpy.ndarray
復制代碼b = np.array([6,7,8])
b
復制代碼array([6, 7, 8])
復制代碼type(b)
復制代碼numpy.ndarray
復制代碼比如:通過array,We can start from ordinaryPythonA list or tuple directly created.The type of the element is inferred from the original type.
a = np.array([2,3,4])
a
復制代碼array([2, 3, 4])
復制代碼b = np.array([1.2, 3.5, 5.1])
b.dtype
復制代碼dtype('float64')
復制代碼一個常見的錯誤是,Pass the contents of the array as a parameter directly toarray,instead of as a list or tuple:
a = np.array(1, 2, 3, 4) #錯誤
a = np.array([1, 2, 3, 4]) #正確
# arrayConvert a two-level nested sequence into a two-dimensional array,Three-level nested sequence into three-dimensional array,以此類推:
b = np.array([(1.5, 2, 3), (4, 5, 6)])
b
復制代碼array([[1.5, 2. , 3. ], [4. , 5. , 6. ]])
復制代碼Array types can also be specified at creation time
c = np.array([ [1,2], [3, 4]], dtype=complex)
c
復制代碼array([[1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j]])
復制代碼Arrays of fixed shape are required,but the data has not been loaded when the array is defined. NumPygives us some array creation functions with placeholders.This way we can avoid using dynamic arrays(Because dynamic array loading takes time)
默認情況下,創建的dtype都是float64
np.zeros( (3,4) ) # Its parameters are not data,but the shape of the array(shape)
復制代碼array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
復制代碼np.ones((2,3,4), dtype=np.int16 ) # 指定數據類型
復制代碼array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]], dtype=int16)
復制代碼np.empty((2,4) )
復制代碼array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000, 0.00000000e+000], [0.00000000e+000, 7.92481296e-321, 3.11525958e-307, 1.24610723e-306]])
復制代碼np.arange(10, 30, 5)
復制代碼array([10, 15, 20, 25])
復制代碼np.arange(0,2,0.5)
復制代碼array([0. , 0.5, 1. , 1.5])
復制代碼Because of the limited precision of floating point numbers,arrangeReturn length may be unpredictable.最好使用linspace,It can specify the desired array length,without setting the step size:
np.linspace(0, 2, 9)
復制代碼array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
復制代碼from numpy import pi
x = np.linspace(0, 2*pi, 100) # Especially useful when many points are to be sampled
f = np.sin(x)
f
復制代碼array([ 0.00000000e+00, 6.34239197e-02, 1.26592454e-01, 1.89251244e-01,
2.51147987e-01, 3.12033446e-01, 3.71662456e-01, 4.29794912e-01,
4.86196736e-01, 5.40640817e-01, 5.92907929e-01, 6.42787610e-01,
6.90079011e-01, 7.34591709e-01, 7.76146464e-01, 8.14575952e-01,
8.49725430e-01, 8.81453363e-01, 9.09631995e-01, 9.34147860e-01,
9.54902241e-01, 9.71811568e-01, 9.84807753e-01, 9.93838464e-01,
9.98867339e-01, 9.99874128e-01, 9.96854776e-01, 9.89821442e-01,
9.78802446e-01, 9.63842159e-01, 9.45000819e-01, 9.22354294e-01,
8.95993774e-01, 8.66025404e-01, 8.32569855e-01, 7.95761841e-01,
7.55749574e-01, 7.12694171e-01, 6.66769001e-01, 6.18158986e-01,
5.67059864e-01, 5.13677392e-01, 4.58226522e-01, 4.00930535e-01,
3.42020143e-01, 2.81732557e-01, 2.20310533e-01, 1.58001396e-01,
9.50560433e-02, 3.17279335e-02, -3.17279335e-02, -9.50560433e-02,
-1.58001396e-01, -2.20310533e-01, -2.81732557e-01, -3.42020143e-01,
-4.00930535e-01, -4.58226522e-01, -5.13677392e-01, -5.67059864e-01,
-6.18158986e-01, -6.66769001e-01, -7.12694171e-01, -7.55749574e-01,
-7.95761841e-01, -8.32569855e-01, -8.66025404e-01, -8.95993774e-01,
-9.22354294e-01, -9.45000819e-01, -9.63842159e-01, -9.78802446e-01,
-9.89821442e-01, -9.96854776e-01, -9.99874128e-01, -9.98867339e-01,
-9.93838464e-01, -9.84807753e-01, -9.71811568e-01, -9.54902241e-01,
-9.34147860e-01, -9.09631995e-01, -8.81453363e-01, -8.49725430e-01,
-8.14575952e-01, -7.76146464e-01, -7.34591709e-01, -6.90079011e-01,
-6.42787610e-01, -5.92907929e-01, -5.40640817e-01, -4.86196736e-01,
-4.29794912e-01, -3.71662456e-01, -3.12033446e-01, -2.51147987e-01,
-1.89251244e-01, -1.26592454e-01, -6.34239197e-02, -2.44929360e-16])
復制代碼array,zeros,zeros_likee,ones_like,empty,empty_like,arange,linspace,numpy,random ...
可以在notebook裡面直接np.array?閱讀,也可以去NumPy's management documentation
which prints out very much like a nested list
a = np.arange(6) # 一維
print(a)
復制代碼[0 1 2 3 4 5]
復制代碼b = np.arange(12).reshape(3,4) # 二維
print(b)
復制代碼[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
復制代碼c = np.arange(24).reshape(2,3,4) # 三維
print(c)
復制代碼[[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
[[12 13 14 15] [16 17 18 19] [20 21 22 23]]]
復制代碼If the array is too long,NumPyThe data in the middle part is automatically ignored,print only the first:
我們可以通過設置set_printoptionsoption to turn off the feature: np.set_printoptions(threshold='nan')
print(np.arange(100000))
復制代碼[ 0 1 2 ... 99997 99998 99999]
復制代碼print(np.arange(10000).reshape(100,100))
復制代碼[[ 0 1 2 ... 97 98 99]
[ 100 101 102 ... 197 198 199]
[ 200 201 202 ... 297 298 299]
...
[9700 9701 9702 ... 9797 9798 9799]
[9800 9801 9802 ... 9897 9898 9899]
[9900 9901 9902 ... 9997 9998 9999]]
復制代碼In the array, arithmetic operations are conducted on the element level(elementwise).
a = np.array([20, 30, 40, 50])
b = np.arange(4) # 0, 1, 2, 3
c = a - b
c
復制代碼array([20, 29, 38, 47])
復制代碼b ** 2
復制代碼array([0, 1, 4, 9])
復制代碼10 * np.sin(a)
復制代碼array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])
復制代碼a < 35
復制代碼array([ True, True, False, False])
復制代碼在線性代數中,乘號*The general expression is matrix multiplication,但在NumPyIt says in the element level of multiplication.
矩陣乘法在NumPy中使用dot:
A = np.array([ [1, 1],
[0, 1]] )
B = np.array( [[2, 0],
[3, 4]])
A*B # 元素級乘法
復制代碼array([[2, 0], [0, 4]])
復制代碼A.dot(B) # 矩陣乘法
復制代碼array([[5, 4], [3, 4]])
復制代碼np.dot(A,B)
復制代碼array([[5, 4], [3, 4]])
復制代碼a = np.ones((2, 3), dtype = int)
b = np.random.random((2, 3))
a *=3
a
復制代碼array([[3, 3, 3], [3, 3, 3]])
復制代碼b +=a
b
復制代碼array([[3.62220496, 3.85951178, 3.62499834], [3.51389196, 3.57738806, 3.47357054]])
復制代碼a +=b
復制代碼---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
Input In [33], in <cell line: 1>()
----> 1 a +=b
UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
復制代碼print(a.sum(), a.min(), a.max())
復制代碼a
復制代碼b = np.arange(12).reshape(3,4)
b
復制代碼b.sum(axis=0) # 每一列的和
復制代碼b.min(axis=1) # 每一行的最小值
復制代碼b.cumsum(axis=1) # Each row is the cumulative sum
復制代碼NumPyProvides some commonly used mathematical functions,如:sin,cos,exp等.在NumPy中,它們被稱為“通用函數”(ufunc).在NumPy中,When these functions work on arrays,are applied to each individual element,and produce a new array.
B = np.arange(3)
B
復制代碼np.exp(B)
復制代碼np.sqrt(B)
復制代碼C = np.array([2., -1., 4.])
np.add(B,C)
復制代碼一維數組非常類似於Pyton的序列(list,tuple),they can be indexed(index),被切片(slice),被遍歷(iterate).
a = np.arange(10)**3
a
復制代碼a[2]
復制代碼a[2:5]
復制代碼a[:6:2] = -1000
a
復制代碼a[::-1]
復制代碼for i in a:
print(i**(1/3.0))
復制代碼多維數組each axis(axis)has an index(index).這些索引使用","(半角逗號)分隔,以元組(tuple)的形式表現:
def f(x, y):
return 10*x + y
b = np.fromfunction(f,(5, 4),dtype=int)
b
復制代碼b[2,3]
復制代碼b[0:5,1] # 第二列,0,1,2,3,4行
復制代碼b[:,1] #第二列,row used
復制代碼b[1:3, :] # 第1,2行,所有列
復制代碼另外,NumPy還有...這種寫法,It will automatically determine the index that needs to be filled: 比如,We now have one with five axes(axis)的數組:
c = np.array( [[[ 0, 1, 2],
[ 10,12, 13]],
[[100, 101, 102],
[110, 112, 113]]])
c.shape
復制代碼c[1,...]
復制代碼c[...,2]
復制代碼Traverse begins with the first axis:
for row in b:
print(row)
復制代碼for element in b.flat: # level access
print(element,end=",")
復制代碼Change the shape of an array
a = np.floor(10*np.random.random( (3, 4)))
a
復制代碼a.shape
a
復制代碼在NunPyThere are many ways to change the shape of an array. 請注意,The following three ways are returns a modified array,without changing the original array:
a.ravel() # Returns the shape of the flattened array
復制代碼a.reshape(6,2) # Returns an array of the target shape
復制代碼a.T # Return to its transpose
復制代碼a
復制代碼a.resize((2,6))
復制代碼a
復制代碼a.reshape(3,-1)
復制代碼Multiple arrays can be stacked along different axis:
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print(b)
復制代碼np.vstack((a,b))
復制代碼np.hstack((a,b))
復制代碼c = np.array([3,3])
np.column_stack((a,c))
復制代碼使用split,We can split an array horizontally.Can be specified when splitting:
a = np.floor(10*np.random.random((2, 12)))
a
復制代碼np.hsplit(a,3)
復制代碼np.hsplit(a, (3,4)) # Divide in the third column and the fourth column
復制代碼Simple assignment will not happen any copy:
a = np.arange(12)
b = a # 不會新建對象
b is a # a 和 b 是對同一個 ndarray對象的兩個名字
復制代碼b.shape = 3,4 # 同時會修改a的形狀
a.shape
復制代碼PythonPass objects by reference,So the function call does not make a copy:
def f(x):
print( id(x))
print(id(a))
f(a)
復制代碼Different data objects can share the same data.viewMethod to create a new array object,but still using the same data.
c = a.view()
c is a
復制代碼c.base is a # c僅僅是aa view of the data in
復制代碼c.flags.owndata
復制代碼c.shape = 2,6 # ashape does not change
a
復制代碼c[0, 4] = 1234 # adata will change
a
復制代碼c
復制代碼Slicing an array returns a view of it:
s = a[:, 1:3]
s[:] = 10
a
復制代碼copy()The method makes a complete copy of the data and its data.
d = a.copy()
d is a
復制代碼d
復制代碼d[0, 0] = 999
a
復制代碼d
復制代碼palettle = np.array([ [ 0,0]])
復制代碼
Python program design foundation second edition Dong Fuguo edition after class exercise answers
Preparing for the postgraduate