当前位置:网站首页>Numpy array matrix operation
Numpy array matrix operation
2022-07-27 17:51:00 【Excited Xiao Fei】
Learning goals : Study numpy Array ( matrix ) establish , Information view , Transformation
for example :
- One day master numpy Array operation
Learning content :
for example :
- numpy What libraries are used for
- numpy Library array ( matrix ) establish
- Array ( matrix ) Information view
- Master arrays ( matrix ) Transformation
- For detailed study suggestions, go to see the official api
Learning time :
Tips :2022/7/24
Learning output :
**
1numpy What libraries are used for
**
numpy Library for Scientific Computing , It is the basic library of other scientific computing libraries , It is widely used in open source projects , Such as :Pandas、Seaborn、Matplotlib、scikit-learn etc. .
Here is the learning matrix ( Mathematical name ), Computer related majors are called arrays ( data structure ), It's called matrix here ,out For output results
*2 Matrix creation *
2.1 Use python data structure list,tuple To a matrix
1 That's ok 4 Column , And 2 That's ok 4 Column results
#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 Use numpy Function quick creation
arange()0 To 10 The interval is 2, a line 5 Column
np.arange(0,10,2)
out:
array([0, 2, 4, 6, 8])
3 That's ok 3 Column 0 Matrix
np.zeros([3,3])
out:
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
3 That's ok 3 Column Unit matrix
np.ones([3,3])
out:
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
linspace()0 To 2 Divide into 10 branch 1 That's ok 10 Column Row vector
logspace() hold 0 To 2 Divide into 5-1 branch , The tolerance is (0-2)/(5-1))=0.5, With 10 Bottom ,10^0 ,10^0.5 ,10^1 ,10^2 A sequence of equal differences
#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 That's ok 5 Column positive diagonal matrix
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 ride 3 That's ok 3 Column unit matrix
np.full((3,3),6)
out:
array([[6, 6, 6],
[6, 6, 6],
[6, 6, 6]])
randint()100 To 200 The random number of , Generate 2 That's ok 4 Column matrices
# 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 To 1 Random generation 3 That's ok 2 Column matrices
#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 View matrix information
**
ndim The rank of a matrix ,shape matrix ( That's ok , Column ) dtype data type
astype() Data type transformation
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 Matrix row column transformation
**
-1 Automatic calculation , The row vector becomes 3 That's ok 2 Column ,2 That's ok 3 Column matrices
# 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 That's ok 2 The column matrix becomes a row vector
# 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 individual 2 That's ok 4 The column matrix is arranged horizontally , Merge vertically into one
# 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 That's ok 2 The column matrix is split horizontally and vertically
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.]])]
Matrix transposition
# 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]]
边栏推荐
- Run loam_ Velodyne real-time mapping
- Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
- 【单片机】2.3 AT89S52的CPU
- An analysis of CPU explosion of a smart logistics WCS system in.Net
- Database hyperphone (II)
- High cost, difficult to implement, slow to take effect, what about open source security?
- Switch and router technology-02-working principle of Ethernet switch
- 交换机和路由器技术-03-交换机基本配置
- JSP自定义标签(下)
- ES6 array method and pseudo array to array method
猜你喜欢

The 7-year-old boy broke his finger by AI robot just because he played chess too fast?

面试好难啊!蚂蚁金服的六轮面试我是强撑过来!差点OUT(面试复盘)

KMP template - string matching

Soul 1: why is es more suitable for complex condition search than MySQL?

kubernetes 1.24高可用集群二进制部署

Zhengzhou University database course resource description

微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists

JDBC连接数据库读取前台无法显示数据

泰山OFFICE技术讲座:WORD奇怪的段落边框

Coca Cola's primary challenge is not vitality forest
随机推荐
快解析结合华途文档加密软件
Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
20 years ago, he was Ma Yun's biggest enemy
[OBS] newsocketloopenable network optimization
#夏日挑战赛#【FFH】实时聊天室之WebSocket实战
在CRA创建的项目中使用@并让其识别@路径并给出路径提示
【Codeforces】 B. Make it Divisible by 25
阿里巴巴鹰眼系统简介
KMP模板——字符串匹配
Yyds dry inventory interview must brush top101: specified interval reversal in the linked list
【数据库系统概论(王珊)】第1章——绪论
Tencent cloud upload
Switch and router technology-03-basic configuration of switch
Hegong sky team vision training Day7 - vision, Jetson naon and d435i
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转
灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
Kubernetes Part 7: using kubernetes to deploy prometheus+grafana monitoring system (kubernetes work practice class)
General process of background management system permission setting
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
js实现右键菜单栏功能