当前位置:网站首页>numpy库常用知识整理
numpy库常用知识整理
2022-06-22 04:48:00 【爱打羽毛球的小怪兽】
目录
数组
np.ones():创建一个数组
>>> import numpy as np
>>> z1=np.ones((3,3))#创建一个数组
>>> print(z1)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
<class 'numpy.ndarray'>np.array():转换成数组类型
>>> d1=[1,2,3,4,5,6,7,8,9]
>>> d11=np.array(d1)#转换成数组类型
>>> s11=d11.shape#查看大小
>>> print(type(d11))
<class 'numpy.ndarray'>
>>> print(d11)
[1 2 3 4 5 6 7 8 9]
>>> print(s11)
(9,)reshape():重组数组
若要详细了解课查看Python——numpy库中reshape的用法_我就是一个小怪兽的博客-CSDN博客
数组进行四则运算
>>> A=np.array([[1,2],[3,4]])
>>> B=np.array([[5,6],[7,8]])
>>> c1=A-B
>>> c2=A*B#数组可以进行四则运算
>>> E=np.exp(A)
>>> print(E)
[[ 2.71828183 7.3890561 ]
[20.08553692 54.59815003]]ix_():对数组进行切片选择
>>> D=np.array([[1,2,3,4,],[5,6,99,6],[5,6,12,3],[7,8,5,32]])
>>> print(D)
>>> d1=D[np.ix_([1,2],[0,3])]
>>> d2=D[np.ix_(np.arange(3),[1,3])]
>>> d3=D[np.ix_(D[:,1]>5,[2])]
>>> print(d1,d2,d3,sep='\n')
[[ 1 2 3 4]
[ 5 6 99 6]
[ 5 6 12 3]
[ 7 8 5 32]]
[[5 6]
[5 3]]
[[2 4]
[6 6]
[6 3]]
[[99]
[12]
[ 5]]数组之间的融合:hstack()和vstack()
>>> A=np.array([[1,2],[3,4]])
>>> B=np.array([[5,6],[7,8]])
>>> C1=np.hstack((A,B))#要求行数相同
>>> C2=np.vstack((A,B))#要求列数相同
>>> print(C1,C2,sep='\n')
[[1 2 5 6]
[3 4 7 8]]
[[1 2]
[3 4]
[5 6]
[7 8]]save()数据存取与load()加载
#数据的存取///使用save('文件名',数据名)存储数据///使用load('文件名.文件类型')
>>> A=np.array([[1,2],[3,4]])
>>> np.save('data',A)
>>> Q=np.load('data.npy')
>>> print(Q)
[[1 2]
[3 4]]ravel():是数组编程一维数组
>>> arr=np.arange(12)
>>> arr1=arr.reshape((3,4))
>>> arr2=arr1.ravel()
>>> print(arr,arr1,arr2,sep='\n')
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 1 2 3 4 5 6 7 8 9 10 11]sort():进行数值排序
argmax():获取最大值的索引
argmin():获取最小值的索引
>>> arrs=np.array([516,1,132,15,19,561,56,1616,16,16,65,13,651,16,12])
>>> arrs_1=np.sort(arrs)
>>> print(arrs_1)
[ 1 12 13 15 16 16 16 19 56 65 132 516 561 651
1616]
>>> arrs_2=arrs.reshape(3,5)
>>> print(arrs_2)
[[ 516 1 132 15 19]
[ 561 56 1616 16 16]
[ 65 13 651 16 12]]
>>> maxindex=np.argmax(arrs)#获取最大值的索引
>>> minindex=np.argmin(arrs)#获取最小值的索引
>>> maxindex_1=np.argmax(arrs_2,axis=0)
>>> minindex_1=np.argmin(arrs_2,axis=1)
>>> print(maxindex,minindex,maxindex_1,minindex_1,sep='\n')
7
1
[1 1 1 1 0]
[1 3 4]矩阵
mat():转换成矩阵
>>> mat1=np.mat('1,5,9;,5,6,4;,6,8,7')#转换成矩阵
>>> mat2=np.mat(arrs_2)
>>> print(mat1,arrs_2,sep='\n')
[[1 5 9]
[5 6 4]
[6 8 7]]
[[ 516 1 132 15 19]
[ 561 56 1616 16 16]
[ 65 13 651 16 12]]eye():创建单位矩阵
bmat():合成矩阵
矩阵的转置,矩阵的共轭转置
>>> arr1=np.eye(3)#创建单位矩阵
>>> arr2=arr1*4
>>> arr3=arr1*6
>>> mat_s=np.bmat('arr1 arr2 arr3;arr1 arr3 arr2;arr3 arr1 arr2')
>>> print(mat_s,end='\n')
[[1. 0. 0. 4. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 4. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 4. 0. 0. 6.]
[1. 0. 0. 6. 0. 0. 4. 0. 0.]
[0. 1. 0. 0. 6. 0. 0. 4. 0.]
[0. 0. 1. 0. 0. 6. 0. 0. 4.]
[6. 0. 0. 1. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 1. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 1. 0. 0. 4.]]
>>> print(mat_s.T,end='\n')#转置
[[1. 0. 0. 1. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 1. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 1. 0. 0. 6.]
[4. 0. 0. 6. 0. 0. 1. 0. 0.]
[0. 4. 0. 0. 6. 0. 0. 1. 0.]
[0. 0. 4. 0. 0. 6. 0. 0. 1.]
[6. 0. 0. 4. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 4. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 4. 0. 0. 4.]]
>>> print(mat_s.H,end='\n')#共轭转置
[[1. 0. 0. 1. 0. 0. 6. 0. 0.]
[0. 1. 0. 0. 1. 0. 0. 6. 0.]
[0. 0. 1. 0. 0. 1. 0. 0. 6.]
[4. 0. 0. 6. 0. 0. 1. 0. 0.]
[0. 4. 0. 0. 6. 0. 0. 1. 0.]
[0. 0. 4. 0. 0. 6. 0. 0. 1.]
[6. 0. 0. 4. 0. 0. 4. 0. 0.]
[0. 6. 0. 0. 4. 0. 0. 4. 0.]
[0. 0. 6. 0. 0. 4. 0. 0. 4.]]
>>> print(mat_s.I,end='\n')#逆矩阵
[[-0.18181818 0. 0. 0.09090909 0. 0.
0.18181818 0. 0. ]
[ 0. -0.18181818 0. 0. 0.09090909 0.
0. 0.18181818 0. ]
[ 0. 0. -0.18181818 0. 0. 0.09090909
0. 0. 0.18181818]
[-0.18181818 0. 0. 0.29090909 0. 0.
-0.01818182 0. 0. ]
[ 0. -0.18181818 0. 0. 0.29090909 0.
0. -0.01818182 0. ]
[ 0. 0. -0.18181818 0. 0. 0.29090909
0. 0. -0.01818182]
[ 0.31818182 0. 0. -0.20909091 0. 0.
-0.01818182 0. 0. ]
[ 0. 0.31818182 0. 0. -0.20909091 0.
0. -0.01818182 0. ]
[ 0. 0. 0.31818182 0. 0. -0.20909091
0. 0. -0.01818182]]矩阵可以进行四则运算
#矩阵可以进行四则运算,按照数学中的矩阵算法计算
>>> mat1=np.mat('1,2,3;4,5,6;5,6,8')
>>> mat2=mat1*3
>>> mat3=mat1*mat2
>>> mat4=np.multiply(mat1,mat2)#点乘
>>> print(mat1,mat2,mat3,mat4,sep='\n')
[[1 2 3]
[4 5 6]
[5 6 8]]
[[ 3 6 9]
[12 15 18]
[15 18 24]]
[[ 72 90 117]
[162 207 270]
[207 264 345]]
[[ 3 12 27]
[ 48 75 108]
[ 75 108 192]]逆矩阵
>>> mat=np.mat('1,1,1;1,2,3;,1,3,6')
>>> inverse=np.linalg.inv(mat)#计算逆矩阵
>>> print(mat)
[[1 1 1]
[1 2 3]
[1 3 6]]
>>> print(inverse)
[[ 3. -3. 1.]
[-3. 5. -2.]
[ 1. -2. 1.]]求解方程组:其中矩阵为系数矩阵,解为数组
>>> #求解方程组
>>> A=np.mat('1,-1,1;2,1,0;,2,1,-1')
>>> b=np.array([4,3,-1])
>>> x=np.linalg.solve(A,b)
>>> print(x)
[1. 1. 4.]求特征值和特征向量
>>> #求解特征值和特征向量
>>> A=np.mat('1,0,2;0,3,0;2,0,1')
>>> print(np.linalg.eigvals(A))#可以直接求特征值
[ 3. -1. 3.]
>>> a_value,a_vector=np.linalg.eig(A)
>>> print(a_value,a_vector,sep='\n')
[ 3. -1. 3.]
[[ 0.70710678 -0.70710678 0. ]
[ 0. 0. 1. ]
[ 0.70710678 0.70710678 0. ]]求矩阵的奇异值分解
>>> A=np.mat('4.0,11.0,14.0;8.0,7.0,-2.0')
>>> u,s,v=np.linalg.svd(A,full_matrices=False)#求一个矩阵的奇异值分解
>>> print(u,s,v,sep='\n')
[[-0.9486833 -0.31622777]
[-0.31622777 0.9486833 ]]
[18.97366596 9.48683298]
[[-0.33333333 -0.66666667 -0.66666667]
[ 0.66666667 0.33333333 -0.66666667]]求矩阵行列式的值
>>> A=np.mat('1,-1,1;2,1,0;,2,1,-1')#求解矩阵行列式的值
>>> print(np.linalg.det(A))
-2.9999999999999996边栏推荐
- Golang concise architecture practice
- 【故障诊断】cv2.imwrite无法写入图片,但程序就是不报错
- Large website technology architecture | application server security defense
- mysql常用sql
- Graph calculation on nlive:nepal's graph calculation practice
- mysql笔记
- 当程序员编程时被打扰 | 每日趣闻
- MySQL notes
- Daily question: the difference between ArrayList and LinkedList
- Researcher of Shangtang intelligent medical team interprets organ image processing under intelligent medical treatment
猜你喜欢

"O & M youxiaodeng" active directory batch modification user

Odoo Development Manual (I) the second day of contact with odoo

Learning signal integrity from scratch -- 7-si analysis and simulation

Arrangement of soft test subjects in the second half of 2022

Web design and production final assignment report - minority music website

What is a forum virtual host? How to choose?

Windows10 cannot access LAN shared folder

POSIX shared memory

Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products

POSIX semaphore
随机推荐
[从零开始学习FPGA编程-39]:进阶篇 - 语法-硬件模块的单元测试:仿真激励、testbench
tinymce. Init() browser compatibility issue
【科研笔记】Focal Loss
Go learning (II. Built in container)
[learn FPGA programming from scratch -39]: Advanced - syntax - unit test of hardware module: simulation excitation, testbench
About SSM integration, this is enough ~ (nanny level hands-on tutorial)
It is easy to analyze and improve R & D efficiency by understanding these five figures
The feeling of leaving full-time for a single month!
中闽在线:以“积分”为纽带 共享线上渠道资源
Why does golang not recommend this/self/me/that/_ this
requests cookie更新值
系统整理|这个模型开发前的重要步骤有多少童鞋忘记细心做好(实操)
What is the value of the FC2 new domain name? How to resolve to a website?
Write the first C application -- Hello, C
获取DPI函数返回值永远是96 | 获取DPI函数返回值不正确 | GetDpiForMonitor/GetDeviceCaps返回值不正确的原因
Slurm tutorial
mongo模糊查詢,帶有特殊字符需要轉義,再去查詢
每日一问:ArrayList和LinkedList的区别
[fault diagnosis] cv2 Imwrite cannot write the picture, but the program does not report an error
[sdx62] IPA log fetching instructions