Home >Backend Development >Python Tutorial >Analysis of common operation examples of ndarray in Python Numpy
NumPy (Numerical Python) is an open source numerical computing extension for Python. This tool can be used to store and process large matrices. It is much more efficient than Python's own nested list structure (which can also be used to represent matrices) and supports a large number of dimensional array and matrix operations. , in addition, it also provides a large number of mathematical function libraries for array operations.
Numpy mainly uses ndarray to process N-dimensional arrays. Most of the properties and methods in Numpy serve ndarray, so it is very necessary to master the common operations of ndarray in Numpy!
The main object of NumPy is isomorphic multidimensional arrays. It is a list of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. Called axis in NumPy dimensions.
In the example shown below, the array has 2 axes. The length of the first axis is 2 and the length of the second axis is 3.
[[ 1., 0., 0.], [ 0., 1., 2.]]
ndarray.ndim: Axis (dimension) of the array ) number. In the Python world, the number of dimensions is called rank.
ndarray.shape: Dimensions of the array. This is a tuple of integers representing the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). Therefore, the length of the shape tuple is the rank or number of dimensions ndim.
ndarray.size: The total number of array elements. This is equal to the product of the elements of shape.
ndarray.dtype: An object describing the type of elements in the array. A dtype can be created or specified using standard Python types. Additionally NumPy provides its own types. For example numpy.int32, numpy.int16 and numpy.float64.
ndarray.itemsize : The byte size of each element in the array. For example, an array with elements of type float64 has an itemsize of 8 (=64/8), while an array of type complex32 has an itemsize of 4 (=32/8). It is equal to ndarray.dtype.itemsize.
>>> import numpy as np >>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> a.shape (3, 5) >>> a.ndim 2 >>> a.dtype.name 'int64' >>> a.itemsize 8 >>> a.size 15 >>> type(a) <type 'numpy.ndarray'> >>> b = np.array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) <type 'numpy.ndarray'>
In the same ndarray, the same type of data is stored. Common data types of ndarray include:
## ndarray reshape操作 array_a = np.array([[1, 2, 3], [4, 5, 6]]) print(array_a, array_a.shape) array_a_1 = array_a.reshape((3, 2)) print(array_a_1, array_a_1.shape) # note: reshape不能改变ndarray中元素的个数,例如reshape之前为(2,3),reshape之后为(3,2)/(1,6)... ## ndarray转置 array_a_2 = array_a.T print(array_a_2, array_a_2.shape) ## ndarray ravel操作:将ndarray展平 a.ravel() # returns the array, flattened array([ 1, 2, 3, 4, 5, 6 ]) 输出: [[1 2 3] [4 5 6]] (2, 3) [[1 2] [3 4] [5 6]] (3, 2) [[1 4] [2 5] [3 6]] (3, 2)
astype(dtype[, order, casting, subok, copy]): Modify the data type in ndarray. Pass in the data type that needs to be modified, and other keyword parameters can be ignored.
array_a = np.array([[1, 2, 3], [4, 5, 6]]) print(array_a, array_a.dtype) array_a_1 = array_a.astype(np.int64) print(array_a_1, array_a_1.dtype) 输出: [[1 2 3] [4 5 6]] int32 [[1 2 3] [4 5 6]] int64
NumPy mainly creates ndarray arrays through the np.array()
function.
>>> import numpy as np >>> a = np.array([2,3,4]) >>> a array([2, 3, 4]) >>> a.dtype dtype('int64') >>> b = np.array([1.2, 3.5, 5.1]) >>> b.dtype dtype('float64')
You can also explicitly specify the type of the array when creating:
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex ) >>> c array([[ 1.+0.j, 2.+0.j], [ 3.+0.j, 4.+0.j]])
It can also be created by using the np.random.random
function Random ndarray array.
>>> a = np.random.random((2,3)) >>> a array([[ 0.18626021, 0.34556073, 0.39676747], [ 0.53881673, 0.41919451, 0.6852195 ]])
Typically, the elements of an array are initially unknown, but its size is known. Therefore, NumPy provides several functions to create arrays with initial placeholder contents. This reduces the need for array growth, which is a costly operation.
Functionzeros
creates an array consisting of 0s, function ones
creates a complete array, function empty
creates an array whose initial content is random , depends on the state of the memory. By default, the dtype of the created array is float64.
>>> np.zeros( (3,4) ) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]) >>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified 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,3) ) # uninitialized, output may vary array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], [ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
To create an array of numbers, NumPy provides a function similar to range
, which returns an array instead of a list.
>>> np.arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> np.arange( 0, 2, 0.3 ) # it accepts float arguments array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
Unlike many matrix languages, the product operators*
operate element-wise in NumPy arrays. Matrix products can be performed using the @
operator (in python> = 3.5) or the dot
function or method:
>>> A = np.array( [[1,1], ... [0,1]] ) >>> B = np.array( [[2,0], ... [3,4]] ) >>> A * B # elementwise product array([[2, 0], [0, 4]]) >>> A @ B # matrix product array([[5, 4], [3, 4]]) >>> A.dot(B) # another matrix product array([[5, 4], [3, 4]])
Certain operations (e.g. =
and *=
) will more directly change the matrix array being operated on without creating a new matrix array.
>>> 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.417022 , 3.72032449, 3.00011437], [ 3.30233257, 3.14675589, 3.09233859]]) >>> a += b # b is not automatically converted to integer type Traceback (most recent call last): ... TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise array (a behavior called upcasting).
>>> a = np.ones(3, dtype=np.int32) >>> b = np.linspace(0,pi,3) >>> b.dtype.name 'float64' >>> c = a+b >>> c array([ 1. , 2.57079633, 4.14159265]) >>> c.dtype.name 'float64' >>> d = np.exp(c*1j) >>> d array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j, -0.54030231-0.84147098j]) >>> d.dtype.name 'complex128'
Many unary operations, such as calculating the sum of all elements in an array, are implemented as methods of the ndarray
class.
>>> a = np.random.random((2,3)) >>> a array([[ 0.18626021, 0.34556073, 0.39676747], [ 0.53881673, 0.41919451, 0.6852195 ]]) >>> a.sum() 2.5718191614547998 >>> a.min() 0.1862602113776709 >>> a.max() 0.6852195003967595
By default, these operations work on the array as if it were a list of numbers, regardless of its shape. However, by specifying the axis parameter, you can apply operations along the specified axis of the array:
>>> b = np.arange(12).reshape(3,4) >>> b array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> >>> b.sum(axis=0) # 计算每一列的和 array([12, 15, 18, 21]) >>> >>> b.min(axis=1) # 计算每一行的和 array([0, 4, 8]) >>> >>> b.cumsum(axis=1) # cumulative sum along each row array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]]) 解释:以第一行为例,0=0,1=1+0,3=2+1+0,6=3+2+1+0
One Dimension Arrays can be indexed, sliced, and iterated just like lists and other Python sequence types.
>>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> a[:6:2] = -1000 # 等价于 a[0:6:2] = -1000; 从0到6的位置, 每隔一个设置为-1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, fan 216, 343, 512, 729]) >>> a[ : :-1] # 将a反转 array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000])
Multidimensional arrays can have one index per axis. These indices are given as a comma-separated tuple:
>>> b array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]]) >>> b[2,3] 23 >>> b[0:5, 1] # each row in the second column of b array([ 1, 11, 21, 31, 41]) >>> b[ : ,1] # equivalent to the previous example array([ 1, 11, 21, 31, 41]) >>> b[1:3, : ] # each column in the second and third row of b array([[10, 11, 12, 13], [20, 21, 22, 23]]) >>> b[-1] # the last row. Equivalent to b[-1,:] array([40, 41, 42, 43])
几个数组可以沿不同的轴堆叠在一起,例如:np.vstack()
函数和np.hstack()
函数
>>> a = np.floor(10*np.random.random((2,2))) >>> a array([[ 8., 8.], [ 0., 0.]]) >>> b = np.floor(10*np.random.random((2,2))) >>> b array([[ 1., 8.], [ 0., 4.]]) >>> np.vstack((a,b)) array([[ 8., 8.], [ 0., 0.], [ 1., 8.], [ 0., 4.]]) >>> np.hstack((a,b)) array([[ 8., 8., 1., 8.], [ 0., 0., 0., 4.]])
column_stack()
函数将1D数组作为列堆叠到2D数组中。
>>> from numpy import newaxis >>> a = np.array([4.,2.]) >>> b = np.array([3.,8.]) >>> np.column_stack((a,b)) # returns a 2D array array([[ 4., 3.], [ 2., 8.]]) >>> np.hstack((a,b)) # the result is different array([ 4., 2., 3., 8.]) >>> a[:,newaxis] # this allows to have a 2D columns vector array([[ 4.], [ 2.]]) >>> np.column_stack((a[:,newaxis],b[:,newaxis])) array([[ 4., 3.], [ 2., 8.]]) >>> np.hstack((a[:,newaxis],b[:,newaxis])) # the result is the same array([[ 4., 3.], [ 2., 8.]])
使用hsplit()
,可以沿数组的水平轴拆分数组,方法是指定要返回的形状相等的数组的数量,或者指定应该在其之后进行分割的列:
同理,使用vsplit()
,可以沿数组的垂直轴拆分数组,方法同上。
################### np.hsplit ################### >>> a = np.floor(10*np.random.random((2,12))) >>> a array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.], [ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]]) >>> np.hsplit(a,3) # Split a into 3 [array([[ 9., 5., 6., 3.], [ 1., 4., 9., 2.]]), array([[ 6., 8., 0., 7.], [ 2., 1., 0., 6.]]), array([[ 9., 7., 2., 7.], [ 2., 2., 4., 0.]])] >>> np.hsplit(a,(3,4)) # Split a after the third and the fourth column [array([[ 9., 5., 6.], [ 1., 4., 9.]]), array([[ 3.], [ 2.]]), array([[ 6., 8., 0., 7., 9., 7., 2., 7.], [ 2., 1., 0., 6., 2., 2., 4., 0.]])] >>> x = np.arange(8.0).reshape(2, 2, 2) >>> x array([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]) ################### np.vsplit ################### >>> np.vsplit(x, 2) [array([[[0., 1.], [2., 3.]]]), array([[[4., 5.], [6., 7.]]])]
The above is the detailed content of Analysis of common operation examples of ndarray in Python Numpy. For more information, please follow other related articles on the PHP Chinese website!