当前位置:网站首页>numpy基础操作
numpy基础操作
2022-06-09 23:01:00 【石头变钻石?】
numpy基础操作
本篇为回顾一下基础,对照目录,将相关内容复习一遍,一起学习的同学也可以尝试这种方法来筑基。
先导入包
import numpy as np
创建一个向量
vct_row = np.array([1, 2, 3]) # 行向量
vct_col = np.array([[4],
[5],
[6]]) # 列向量
创建矩阵
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
mtx = np.mat([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
创建稀疏矩阵
from scipy import sparse
mtx = np.array([[0, 2, 3],
[4, 0, 0],
[7, 8, 0]])
# 压缩矩阵
mtx_sparse = sparse.csr_matrix(mtx)
展示矩阵属性
# 行列号
mtx.shape # 注意最后不带括号
vct.shape
# 数量
mtx.size
# 维数
mtx.ndim
多元素应用某一操作
# 广播(radcasting)
mtx + 100
# 查看书籍还有一种方法
add_100 = lambda i: 1 + 100
vct_add_100 = np.vectorize(add_100)
vct_add_100(mtx)
最大值和最小值
# 最大值
mtx.max(axis = 1) # axis, 0 is col,1 is row.
np.max(mtx, axis = 0)
# 最小值
mtx.min()
np.min(mtx)
均值、方差、标准差
# 均值
np.mean(mtx, axis = 0)
mtx.mean(axis = 1)
# 方差
np.var(mtx)
mtx.var()
# 标准差
np.std(mtx)
mtx.var()
矩阵变形
mtx.reshape(row, col) # mtx.size = row*col
mtx.reshape(1, -1)
转置
mtx.T
vct.T
展开矩阵
mtx.flatten()
计算矩阵的秩
np.linalg.matrix_rank(mtx)
计算行列式
np.linalg.det(mtx)
获取矩阵对角线元素
mtx.diagonal(offset = 1)
计算矩阵的迹
mtx.trace()
计算特征值和特征向量
feature, vector = np.linalg.eig(mtx)
计算向量点积
np.dot(vector1, vector2)
矩阵相加或相减
np.add(matrix1, matrix2)np.subtract(matrix1, matrix2)
矩阵乘法 ##注意区分点乘和对应元素相乘
np.dot(matrix1, matrix2)
matrix1 @ matrix2 # python3.5以上版本,现在应该基本都是了吧?。。
# 对应元素相乘为
matrix1 * matrix2
矩阵的逆
np.linalg.inv(mtx)
若逆矩阵存在,则mtx @ np.linalg.inv(mtx)应该为单位矩阵,计算机中为无限接近1的值
猴急
实际使用中,部分可能记不住,可以用dir(mtx), dir(vct)查看包含哪些
欢迎大家分享补充
边栏推荐
- 在线JSON转CSV工具
- 2022爱分析· 隐私计算厂商全景报告 | 爱分析报告
- Server operation and maintenance environment security system (Part 2)
- « 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
- 记一次应急排查“新“路历程
- On how enterprise we media can stand out
- Simulated annealing-n queen problem
- 不能在此路径中使用此配置节,如果在父级别上锁定了该节,便会出现这种情况的解决办法
- 腾讯-NCNN简介
- C# WPF布局控件LayoutControl介绍
猜你喜欢

Swift GCD Notify after concurrent execution Lock barrier

又一重磅内容|海外现金贷产品形态及风控措施

Huangxiting: psychological research should follow the path of the Chinese people, and should do psychological research with Chinese characteristics

Simulated annealing-n queen problem

【卷指南】Mendeley文献管理工具教程

Record the "new" course of an emergency investigation

剖析虚幻渲染体系(15)- XR专题

To serve the "nervous system" with a broad and subtle vision

Swift GCD Notify after concurrent execution Lock barrier

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
随机推荐
Dynamic reading of protobuf data
Is Hebei Hengyin futures a regular platform? Is it safe?
读书会招募 | 一起来读《蛤蟆先生去看心理医生》吧
2022年质量员-市政方向-通用基础(质量员)考试练习题及模拟考试
模拟退火-n皇后问题
How to record the login information of accessing the database in oracle?
leetcode695. 岛屿的最大面积(中等)
Online JSON to CSV tool
IPSec的特征与功能
C#如何获取实体类属性名和值?
String-4-242. 有效的字母异比特詞
Install idea
What is liquidity pledge? What is a farm pledge?
TL, how do you manage project risks?
双塔模型-语义索引策略 [In-batch Negatives]
yum 删除包及依赖
jg-文件上传代码-以及导出excel
Introduction à Tencent - ncnn
leetcode547. 省份数量(中等,求连通分量个数)
MATLAB中对tif格式栅格影像读取-保存