当前位置:网站首页>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]]
边栏推荐
- Day07 operation
- 笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误
- 帮扶、提振、担当,第六届土巴兔718全民家装节的新价值和意义
- Following the example of IE, is the decline of Firefox inevitable?
- [single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer
- Windows与网络基础-15-本地安全策略
- How far can invisible orthodontics go under the tuyere?
- #夏日挑战赛#【FFH】实时聊天室之WebSocket实战
- CUE语言基础入门:CUE是一门为配置而生的语言
- Swift QQ authorized login pit set
猜你喜欢
随机推荐
DDD(领域驱动设计)分层架构
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
选择体育场馆的LED显示屏时应该注重哪些方面
Database hyperphone (I)
成本高、落地难、见效慢,开源安全怎么办?
How to extract tables from PDF through C /vb.net
App crash collection and analysis
Windows与网络基础-15-本地安全策略
Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?
数据库超话(二)
MySQL: 函数
Gradient ring progress bar
Swift QQ authorized login pit set
Maximum number less than n
Lichuang EDA - layout and inspection of schematic diagram (III)
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
数据库超话(四)
格力「不清凉」:巨头诉讼落幕又遭大经销商减持,空调新战场还晚人一步?
(2)融合cbam的two-stream项目搭建----数据准备
小于n的最大数




![[MCU] 2.2 pin function of AT89S52](/img/d0/b204efb80ec5f0b7bc2d83d0250eb9.png)



