Python extension library NumPy Quick Start

NumPy (Numerical Python) is a Python library expansion. Support dimension of the array and matrix operations, provide a lot of math library.

ndarray categories: NumPy array class is referred ndarray

Attributes:

ndarray.ndim represents a dimension of the array.

ndarray.shape is an integer tuple, to represent the size of the array in each dimension. For example, a n row and m -column matrix, which shape is ( n, m ).

ndarray.size represents the number of elements in the array, which is equal to shape the product of all integers

ndarray.dtype used to describe the elements in the array type

ndarray.itemsize used to indicate the byte size of each array element 

import numpy as np
a=np.arange(10).reshape(2,5)
a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
注意:size=n*m 否则会报错
a.shape (
2, 5) a.ndim 2 a.size 10 a.dtype dtype('int32') a.itemsize 4

Array creation

Using the list or directly tuple
Import numpy NP AS A np.array = ([1,2,3,4,5,6]) B = np.array ((1,2,3,4,5,6)) A Array ([. 1, 2,. 3,. 4,. 5,. 6]) B Array ([. 1, 2,. 3,. 4,. 5,. 6])
parameters passed in must be the same structure, the structure is not the same conversion occurs

 NumPy also provides a convenient way to create an array of specific

   a=np.zeros((3,3))  

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

 a = np.ones ([2,4])

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

 c = np.arange(15)

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

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

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
g = np.arange(0,15,3)
array([ 0,  3,  6,  9, 12])

 h = np.arange(0,3,0.4)

array([0. , 0.4, 0.8, 1.2, 1.6, 2. , 2.4, 2.8])

 The basic operation of the array ( addition, subtraction )

   Corresponding to the position of addition, subtraction, this one is very simple, do not introduced

  In the NumPy, *a multiplication between the array element, rather than matrix multiplication, matrix multiplication can be used dot()methods to achieve 

a=np.array([[1,2],[4,5]])
b=np.array([[0,1],[0,1]])
a
array([[1, 2],
       [4, 5]])
b
array([[0, 1],
       [0, 1]])
a*b
array([[0, 2],
       [0, 5]])
a.dot(b)
array([[0, 3],
       [0, 9]])

 Summary matrix product:

1. Only when the number of rows the number of columns of a matrix is ​​equal to the second matrix, the matrix can be multiplied by two

2. The matrix multiplication is not commutative, AB! = BA

3. erasure rate matrix multiplication is not satisfied, for example: AB = AC, and A! = 0 B = C can not be introduced

ndarray class implements a method of operating an array of a number of one yuan, such as sum, maximum, minimum and the like.

 a = np.random.random((2,3))
求和:a.sum() 

最小值:a.min()
最大值:a.max()
axis = 0表示列操作
axis = 1表示行操作
 
 
 

 


 

 

Guess you like

Origin www.cnblogs.com/wookong/p/11236552.html