当前位置:网站首页>Numpy basic operation
Numpy basic operation
2022-06-10 00:11:00 【Stone to diamond?】
numpy Basic operation
- Create a vector
- Create a matrix
- Create sparse matrix
- Show matrix properties
- Multiple elements apply an operation
- Maximum and minimum
- mean value 、 variance 、 Standard deviation
- Matrix distortion
- Transposition
- Expand the matrix
- Calculate the rank of the matrix
- Calculate determinant
- Get the diagonal elements of the matrix
- Calculate the trace of the matrix
- Calculate the eigenvalues and eigenvectors
- Calculate the dot product of the vector
- Matrix addition or subtraction
- Matrix multiplication ## Pay attention to distinguish between dot multiplication and corresponding element multiplication
- The matrix of the inverse
- The monkey is in a hurry
This is a review of the basics , Cross reference table of contents , Review the relevant content , Students studying together can also try this method to build a foundation .
Import the package first
import numpy as np
Create a vector
vct_row = np.array([1, 2, 3]) # Row vector
vct_col = np.array([[4],
[5],
[6]]) # Column vector
Create a matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
mtx = np.mat([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Create sparse matrix
from scipy import sparse
mtx = np.array([[0, 2, 3],
[4, 0, 0],
[7, 8, 0]])
# Compression matrix
mtx_sparse = sparse.csr_matrix(mtx)
Show matrix properties
# Line number
mtx.shape # Note that there are no parentheses at the end
vct.shape
# Number
mtx.size
# dimension
mtx.ndim
Multiple elements apply an operation
# radio broadcast (radcasting)
mtx + 100
# There is another way to view books
add_100 = lambda i: 1 + 100
vct_add_100 = np.vectorize(add_100)
vct_add_100(mtx)
Maximum and minimum
# Maximum
mtx.max(axis = 1) # axis, 0 is col,1 is row.
np.max(mtx, axis = 0)
# minimum value
mtx.min()
np.min(mtx)
mean value 、 variance 、 Standard deviation
# mean value
np.mean(mtx, axis = 0)
mtx.mean(axis = 1)
# variance
np.var(mtx)
mtx.var()
# Standard deviation
np.std(mtx)
mtx.var()
Matrix distortion
mtx.reshape(row, col) # mtx.size = row*col
mtx.reshape(1, -1)
Transposition
mtx.T
vct.T
Expand the matrix
mtx.flatten()
Calculate the rank of the matrix
np.linalg.matrix_rank(mtx)
Calculate determinant
np.linalg.det(mtx)
Get the diagonal elements of the matrix
mtx.diagonal(offset = 1)
Calculate the trace of the matrix
mtx.trace()
Calculate the eigenvalues and eigenvectors
feature, vector = np.linalg.eig(mtx)
Calculate the dot product of the vector
np.dot(vector1, vector2)
Matrix addition or subtraction
np.add(matrix1, matrix2)np.subtract(matrix1, matrix2)
Matrix multiplication ## Pay attention to distinguish between dot multiplication and corresponding element multiplication
np.dot(matrix1, matrix2)
matrix1 @ matrix2 # python3.5 Above version , It should be almost all now ?..
# The corresponding elements are multiplied by
matrix1 * matrix2
The matrix of the inverse
np.linalg.inv(mtx)
If inverse matrix exists , be mtx @ np.linalg.inv(mtx) Should be an identity matrix , The computer is infinitely close 1 Value
The monkey is in a hurry
In practice , Some may not remember , It can be used dir(mtx), dir(vct) See what is included
Welcome to share and supplement
边栏推荐
- With the advent of the digital era, 360 has joined hands with the dark horse of entrepreneurship to help small and medium-sized enterprises seize the key future
- Some problems of youtubednn recall
- Huangxiting: psychological research should follow the path of the Chinese people, and should do psychological research with Chinese characteristics
- 手推单精度浮点类型
- CSRF/XSRF简介
- C#实用技巧之:将图标打包进DLL并读取
- 478. 在圆内随机生成点
- Single cell data reproduction - lung cancer article code reproduction 7
- What are the advantages of SaaS services
- 流程测试支持批量参数导入,测试效率直接拉满!
猜你喜欢

Do you know about singleton mode? I don't understand anyway.

PMP high frequency knowledge sorting

Q-learning

Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability

MySQL开发实战总结(一)

SpingBoot+Quartrz集群版实现动态定时任务(利用反射实现自定义业务)

“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO

Simulated annealing-n queen problem

企业无忧 | Apipost私有化部署活动即将火热开启

« Lorsque vous n'êtes plus programmeur, beaucoup de choses échappent au contrôle » - conversation avec Suse CTO, la plus grande entreprise open source indépendante au monde
随机推荐
I've taken it. The MySQL table has 500W rows. Is there anyone who doesn't partition?
Difference between stacked cable optical module and ordinary optical module
C# WPF布局控件LayoutControl介绍
Sparksql source code series | to understand the distribution source code system (spark3.2)
荐书 | 即使是来自星星的你,我也想去靠近
請問徽商期貨是正規的嗎?開戶交易安全嗎?
C#实用技巧之:将图标打包进DLL并读取
929. 独特的电子邮件地址
电脑连接打印机出现的问题(打印机显示未指定)解决方法
2022 practice questions and mock exam of quality controller municipal direction general basic (quality controller) examination
Automatically open personal when opening xlsx file Xlsb table file
流程测试支持批量参数导入,测试效率直接拉满!
Detailed explanation of IEEE 754 floating point number standard
Core [email protected] < 3.4 is no longer maintained and not recommended for usage due to the
Retrofit2.0 添加Header的方法总结
消息队列的精髓与灵魂
14 乘法器:如何像搭乐高一样搭电路(下)?
片上变化(on chip variation,OCV)概念学习
DDD驱动领域设计学习笔记
Do your filial duty to make an old people's fall prevention alarm system for your family