当前位置:网站首页>numpy庫常用知識整理
numpy庫常用知識整理
2022-06-22 04:50: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边栏推荐
- Recruiters - issue 23
- Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products
- Pourquoi golang ne recommande - t - il pas ceci / self / me / this / _ Ça.
- [fault diagnosis] using multithreading, the program does not report an error but does not run
- 拦截器的具体概念
- numpy库常用知识整理
- Spark - executor initialization &
- mysql常用sql
- torch DDP Training
- MySQL notes
猜你喜欢

Golang concise architecture practice

Web design and production final assignment report - minority music website

【故障诊断】stitch.py脚本失效

软考2022年下半年考试科目安排

网页设计与制作期末大作业报告——动画家宫崎骏

Spark - executor initialization &

爬梯子&&卖卖股份的最佳时期(跑路人笔记)

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

Use echart to draw 3D pie chart, dashboard and battery diagram

Get the specified row content in Oracle rownum and row_ number()
随机推荐
About SSM integration, this is enough ~ (nanny level hands-on tutorial)
slurm 使用教程
Zhongmin online: sharing online channel resources with "points" as the link
Take you to develop an efficiency enhancing tool -- vscode plug-in
使用Echart绘制3D饼环图、仪表盘、电池图
《数据库原理》期末考试题
NFT mall building digital collection mall building digital collection market digital collection development company
Spark - executor initialization &
[details] domestic website filing process and steps
103. simple chat room 6: using socket communication
mongo模糊查詢,帶有特殊字符需要轉義,再去查詢
Ora-15063: ASM discovered an insufficient number of disks for diskgroup
What is the difference between TOC and Tob
10 "no no no" redis interview questions
爬梯子&&卖卖股份的最佳时期(跑路人笔记)
【科研笔记】关于使用openslide切图的下采样倍数
Odoo Development Manual (I) the second day of contact with odoo
【故障诊断】CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace b
LeetCode 437. Path sum III - binary tree series question 13
MySQL notes