current position:Home>[Python] numpy notes

[Python] numpy notes

2022-02-01 18:23:27 cout0

「 This is my participation 11 The fourth of the yuegengwen challenge 29 God , Check out the activity details :2021 One last more challenge 」.

numpy brief introduction

What is? numpy

NumPy yes Python The basic package of scientific computing in . It's a Python library , Provide multidimensional array objects , Various derived objects ( Such as mask array and matrix ), And various methods for fast array operation API, It includes == mathematics 、 Logic 、 Shape operation 、 Sort 、 choice 、 Input and output 、 Discrete Fourier transform 、 Basic linear algebra , Basic statistical operation and random simulation, etc ==.

numpy Application

NumPy Usually with SciPy(Scientific Python) and Matplotlib( Drawing library ) Use it together , This combination is widely used in == replace MatLab==, It's a powerful scientific computing environment , It helps us to get through Python Study == Data science or machine learning ==.

ndarray

numpy Of ndarray object , Alias called array. It should be noted that ,==numpy.array≠array.array==, The latter is python Objects of the standard library , It can only handle one-dimensional array objects and has less functions .

  • ndarray Properties of
Property name describe
ndarray.ndim n dimension, namely array The number of dimensions of , Also known as rank
ndarray.shape That is, the dimensions of an array , Returns an integer tuple , Such as two-dimensional array (m,n)
ndarray.size Number of array elements , amount to shape Element grades
ndarray.dtype An object that describes the type of element in an array
  • Example
>>> import numpy as np
>>> num=np.array([[1,2,3],[4,5,6]])
>>> num.ndim
2
>>> num.shape
(2, 3)
>>> num.size
6
 Copy code 

zeros

Used to create a 0 matrix .

>>> s=np.zeros((5,2))
>>> s
array([[0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.],
       [0., 0.]])
 Copy code 

ones

Be similar to zeros, Created is full 1 matrix .

arrange

Used to create a one-dimensional array

  • ndarray.arrange(first,end,len), Interval is [first,end], step len.
  • ndarray.arrange(num), The array created is 0,1,2……num-1.
>>> np.arange(0,5,2)
array([0, 2, 4])
>>> np.arange(5)
array([0, 1, 2, 3, 4])
 Copy code 

reshape

Used to change the dimension of the matrix .

>>> num=num.reshape(2,3)
>>> num
array([[1, 2, 3],
       [4, 5, 6]])
>>> num=num.reshape(6)
>>> num
array([1, 2, 3, 4, 5, 6])
 Copy code 

max,min and sum

>>> print(a.dot(b))
[[10 13]
 [28 40]]
>>> print(a)
[[0 1 2]
 [3 4 5]]
>>> a.sum()
15
>>> a.min()
0
>>> a.max()
5
 Copy code 

Matrix multiplication

array in ,==* Represents an operation within an element , Not matrix multiplication , For multiplication @ or dot Function substitution ==

>>> a=np.arange(6).reshape(2,3)
>>> b=np.arange(6).reshape(3,2)
>>> print(a)
[[0 1 2]
 [3 4 5]]
>>> print(2*a)
[[ 0  2  4]
 [ 6  8 10]]
>>> print([email protected])
[[10 13]
 [28 40]]
 >>> print(a.dot(b))
[[10 13]
 [28 40]]
 Copy code 

axis Parameters

Specify the axis of the operation

>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>
>>> b.sum(axis=0)                            # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1)                            # min of each row
array([0, 4, 8])
 Copy code 

Transpose matrix

The common method is to access array Of T attribute .

>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> a.T
array([[0, 3],
       [1, 4],
       [2, 5]])
 Copy code 

Unit matrix

  • It can be used identity or eye
>>> np.identity(2)
array([[1., 0.],
       [0., 1.]])
>>> np.eye(2)
array([[1., 0.],
       [0., 1.]])
 Copy code 
  • You can also use == Diagonal matrix == Of diag Generative method
>>> np.diag([1]*2)
array([[1, 0],
       [0, 1]])
 Copy code 

Matrix stacking

  • hstack Lateral stacking
  • vstack Vertical stacking
>>> a=np.array([0,1,2])
>>> b=np.array([3,4,5])
>>> np.hstack((a,b))
array([0, 1, 2, 3, 4, 5])
>>> np.vstack((a,b))
array([[0, 1, 2],
       [3, 4, 5]])

 Copy code 

Matrix partition

Divide the matrix equally

  • hsplit Horizontal segmentation
  • vsplit Vertical segmentation

>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.hsplit(a,3)
[array([[0],
       [3]]), array([[1],
       [4]]), array([[2],
       [5]])]
>>> np.vsplit(a,2)
[array([[0, 1, 2]]), array([[3, 4, 5]])]
 Copy code 

linalg

linalg.det

Calculate the determinant of a matrix :numpy.linalg.det(a)

solve

numpy.linalg.solve(a,b) For solving linear matrix equations

>>> b=np.array([4]).reshape(1,1)
>>> a=np.array([2]).reshape(1,1)
>>> np.linalg.solve(a,b)
array([[2.]])
 Copy code 

inv

numpy.linalg.inv(a) Find the inverse of a matrix

>>> a=([[1,2],[2,1]])
>>> b=np.linalg.inv(a)
>>> [email protected]
array([[1., 0.],
       [0., 1.]])
 Copy code 

copyright notice
author[cout0],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011823250084.html

Random recommended