As you know, we are in big data and artificial intelligence boom, python learning because of its low cost, flexible features became popular language. Numpy as a powerful python module, data analysis, machine learning and other situations have very important applications. Here follow the dog's head notes come together to learn about it!
Numpy installation
Method 1: download the official website to find the Getting Started you can find the installation method
Method 2: Use directly mounted pip
pip install numpy
Basics
First, open the python
1.import Numpy module
import numpy as np # 命名为np (约定俗成)
2. Matrix
Create a 2X3 matrix
matrix = np.array([ [1,2,3], [4,5,6] ])
Numerical matrix type definition
np.array([77,88,99], dtype=np.int32)
np.array([77,88,99], dtype=np.int64) # 默认的int
np.array([77,88,99], dtype=np.float32)
np.array([77,88,99], dtype=np.float64) # 默认的float
Create a 3X3 matrix elements are all 0 or 1
np.zeros((3,3))
np.ones((3,3))
np.ones((3,3),dtype=np.int16) # 同样的也可以改变他的数值类型
(Dummy) empty matrix
np.empty((2,2))
Generates an ordered sequence
np.arange(5) # [0 1 2 3 4]
np.arange(3,8) # [3 4 5 6 7]
np.arange(2,10,2) # [2 4 6 8]
You can also specify the number of elements in the sequence
np.linspace(1,10,5) # [1 3.25 5.5 7.75 10] 间隔9/4=2.25
By a sequence generator matrix
# 将[0,1,2,3,4,5,6,7,8] 变成 3X3 的矩阵(按行排列)
np.arange(9).reshape((3,3))
3. View Properties
Dimension of the matrix
matrix.ndim
Matrix shape
matrix.shape
The number of elements of the matrix
matrix.size
Numerical matrix of the type
matrix.type
Analyzing element size
matrix < 3 # 返回True 或 False 的列表
matrix > 3
matrix == 3
Summing element
np.sum(matrix)
np.sum(matrix, axis=0) # 求每一行的和
np.sum(matrix, axis=1) # 求每一列的和
Seeking maximum or minimum element
np.max(matrix)
np.max(matrix, axis=0) # 求每一行的最大值
np.max(matrix, axis=1) # 求每一列的最大值
np.min(matrix)
np.min(matrix, axis=0) # 求每一行的最小值
np.min(matrix, axis=1) # 求每一列的最小值
Seeking maximum or minimum value of the index
np.argmax(matrix)
np.argmin(matrix)
# 也可以指定行列,同上
Averaging, median, and accumulated, the accumulated difference
np.mean(matrix)
matrix.mean()
np.average(matrix)
np.median(matrix)
np.cumsum(matrix)
np.diff(matrix)
Progressive sorting
np.sort(matrix)
4. Simple operation
Addition and subtraction (addition and subtraction corresponding to the position of the element)
a = np.array([10, 20, 30, 40])
b = np.arange(4)
a + b
a - b
Multiplication and division (multiplication element corresponding to a position other)
a * b
a / b
Take the power of each element
b**2 # 平方
b**3 # 立方
Trigonometric functions
np.sin(a)
np.cos(a)
np.tan(a)
Matrix Multiplication
m1 = np.array([ [1,1], [0,1] ])
m2 = np.arange(4).reshape((2,2))
np.dot(m1,m2)
m1.dot(m2) # 两种方法一样
Matrix transpose
np.tanspose(m1)
m1.T
Cutting matrix
np.clip(matrix,3,8)
#小于3的元素都变成3,大于8的元素都变成8
The random number
Random generator matrix
np.random.seed(123)
np.random.random((2,4))
np.random.randint(8)
np.random.randint(4,10,size=6)
np.random.uniform() # 默认0到1的均匀分布
np.random.uniform(1,6)
numpy.random.normal(loc=0.0, scale=1.0, size=None) # 正态分布随机