当前位置:网站首页>numpy数组矩阵操作
numpy数组矩阵操作
2022-07-27 15:45:00 【激动的小非】
学习目标:学习numpy数组(矩阵)创建,信息查看,变换
例如:
- 一天掌握 numpy 数组操作
学习内容:
例如:
- numpy 库用于做什么
- numpy 库数组(矩阵)创建
- 数组(矩阵)信息查看
- 掌握数组(矩阵)变换
- 详细学习建议去看官方api
学习时间:
提示:2022/7/24
学习产出:
**
1numpy 库用于做什么
**
numpy库用于科学计算,是其他科学计算库的基础库,广泛应用在开源的项目中,如:Pandas、Seaborn、Matplotlib、scikit-learn等。
下面介绍学习矩阵(数学叫法),计算机相关专业叫做数组(数据结构),这里统一叫做矩阵好了,out为输出结果
*2矩阵的创建 *
2.1使用python数据结构 list,tuple转换为矩阵
1行4列,与2行4列结果
#python data structure conversion array
import numpy as np
arry1=np.array([1,2,3,4])
arry2=np.array((5,6,7,8))
arry3=np.array([[9,10,11,12]
,[13,14,15,16]])
print(arry1,arry2,arry3)
out:
[1 2 3 4] [5 6 7 8] [[ 9 10 11 12]
[13 14 15 16]]
2.2使用numpy函数快速创建
arange()0到10间隔为2,一行5列
np.arange(0,10,2)
out:
array([0, 2, 4, 6, 8])
3行3列 0方阵
np.zeros([3,3])
out:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
3行3列 单位矩阵
np.ones([3,3])
out:
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
linspace()0到2分成10分 1行10列 行向量
logspace() 把0到2分成5-1分,公差为(0-2)/(5-1))=0.5,以10为底,10^0 ,10^0.5 ,10^1 ,10^2等差数列
#create a array have float
#create a array have float 10**0-10**2
print(np.linspace(0,2,10),'\n',np.logspace(0,2,5))
out:
[0. 0.22222222 0.44444444 0.66666667 0.88888889 1.11111111
1.33333333 1.55555556 1.77777778 2. ]
[ 1. 3.16227766 10. 31.6227766 100. ]
5行5列正对角矩阵
np.diag([1,1,1,1,1])
out:
array([[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]])
6乘3行3列单位矩阵
np.full((3,3),6)
out:
array([[6, 6, 6],
[6, 6, 6],
[6, 6, 6]])
randint()100到200的随机数字,生成2行4列矩阵
# random a 2*4 rarry
np.random.randint(100,200,size=(2,4))
out:
array([[153, 199, 185, 113],
[150, 130, 116, 142]])
random.rand(3,2)0到1随机生成3行2列矩阵
#generate 3 rwo 2 column arry
arr1,arr2=np.random.rand(3,2),np.random.rand(5)
# generate 5 number from 0-1
print('arr1',arr1,'\n arr2',arr2)
out:
arr1 [[0.29692384 0.17947901]
[0.02604587 0.7050282 ]
[0.18891757 0.93545309]]
arr2 [0.1219717 0.53417921 0.97106664 0.25865028 0.817187 ]
**
3矩阵信息查看
**
ndim矩阵的秩,shape矩阵(行,列) dtype 数据类型
astype()数据类型转变
wep1=np.array([[0,1,2,3]
,[1,3,5,7]
,[2,4,6,8]])
print('array ndim :',wep1.ndim)
print('array shape:',wep1.shape)
print('size:',wep1.size)
# set array shape to 4,3
wep1.shape=4,3
print('dataType:',wep1.dtype)
print('new array',wep1)
print('set data type->',wep1.astype(np.str).dtype)
out:
array ndim : 2
array shape: (3, 4)
size: 12
dataType: int32
new array [[0 1 2]
[3 1 3]
[5 7 2]
[4 6 8]]
set data type-> <U11
**
4矩阵行列变换
**
-1自动计算,行向量变为3行2列,2行3列矩阵
# change arry dimension ,-1 auto calcuate
arr3=np.arange(6)
arr3.reshape(3,-1)
arr3.reshape(-1,3)
out:
array([[0, 1, 2],
[3, 4, 5]])
3行2列矩阵变为行向量
# make array to 1 dimension
wep2=np.arange(6).reshape(3,-1)
print(' result 1\n',wep2.ravel(),'\n result 2\n',wep2.flatten())
out:
result 1
[0 1 2 3 4 5]
result 2
[0 1 2 3 4 5]
2个2行4列矩阵按照横向,纵向合并为一个
# array merge hstack=transverse=axis=1;vstack=longitudinal=axis=0
wep3,wep4=np.arange(8).reshape(2,-1),np.array(((7,7,7,7),(8,8,8,8)))
print(' transverse \n',np.hstack((wep3,wep4)),'\n longitudinal \n',np.vstack((wep3,wep4)))
p1,p2=np.concatenate((wep3,wep4),axis=1),np.concatenate((wep3,wep4),axis=0)
print('p1',p1,'\n p2 \n',p2)
out:
transverse
[[0 1 2 3 7 7 7 7]
[4 5 6 7 8 8 8 8]]
longitudinal
[[0 1 2 3]
[4 5 6 7]
[7 7 7 7]
[8 8 8 8]]
p1 [[0 1 2 3 7 7 7 7]
[4 5 6 7 8 8 8 8]]
p2
[[0 1 2 3]
[4 5 6 7]
[7 7 7 7]
[8 8 8 8]]
4行2列矩阵按照横向纵向拆分
arr4=np.ones([2,4]).reshape(4,-1)
print(np.vsplit(arr4,2),'\n',np.hsplit(arr4,1))
print('transverse 1 spilt 1',np.split(arr4,1,axis=1),
'\nlongitudinal 1 spilt 4\n',np.split(arr4,4,axis=0))
out:
[array([[1., 1.],
[1., 1.]]), array([[1., 1.],
[1., 1.]])]
[array([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]])]
transverse 1 spilt 1 [array([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]])]
longitudinal 1 spilt 4
[array([[1., 1.]]), array([[1., 1.]]), array([[1., 1.]]), array([[1., 1.]])]
矩阵转置
# array transpose row to column
arr5=np.arange(6).reshape(3,2)
print('arr5\n',arr5)
print(arr5.transpose((1,0)))
print(arr5.T)
out:
arr5
[[0 1]
[2 3]
[4 5]]
[[0 2 4]
[1 3 5]]
[[0 2 4]
[1 3 5]]
边栏推荐
- 选择体育场馆的LED显示屏时应该注重哪些方面
- Understanding service governance in distributed development
- Uncle's nephew and his students
- Mysql: function
- 在CRA创建的项目中使用@并让其识别@路径并给出路径提示
- Can oracle-linux-7.9 support oracle-19c ACFs file system?
- 基于STM32的智能鱼缸设计
- What should we pay attention to when choosing the LED display screen of the stadium
- Flutter的布局
- Helm install kubevela complete makefile script content
猜你喜欢

Coca Cola's primary challenge is not vitality forest

App crash collection and analysis

Today's sleep quality record 82 points

奇瑞欧萌达也太像长安UNI-T了,但长得像,产品力就像吗?

Understanding service governance in distributed development

Sharing of local file upload technology of SAP ui5 fileuploader

Kubernetes第七篇:使用kubernetes部署prometheus+grafana监控系统(Kubernetes工作实践类)

今日睡眠质量记录82分

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

立创EDA——PCB的布局(四)
随机推荐
泰山OFFICE技术讲座:WORD奇怪的段落边框
可口可乐的首要挑战,不是元气森林
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
Explain the pile of binary trees in detail
阿里巴巴鹰眼系统简介
记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析
数据库超话(四)
二舅的外甥和他的学生们
Motion capture system for end positioning control of flexible manipulator
$attrs与$listeners组件传值
Explain the idempotence of distributed system in detail
Branch loop statement of C language
深度学习能颠覆视频编解码吗?国家技术发明奖一等奖得主在小红书给你唠
Database hyperphone (III)
What should we pay attention to when choosing the LED display screen of the stadium
【Codeforces】 B. Make it Divisible by 25
【Codeforces】 A. Computer Game
Gartner authority predicts eight development trends of network security in the next four years
Kubernetes第八篇:使用kubernetes部署NFS系统完成数据库持久化(Kubernetes工作实践类)
[OBS] newsocketloopenable network optimization