当前位置:网站首页>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]]
边栏推荐
- 详解分布式系统的幂等
- Technical practice dry goods | from workflow to workflow
- x-sheet 开发教程:初始化配置自定义布局
- KMP模板——字符串匹配
- Cow! His secret is to reproduce the paper in 2 hours——
- 【单片机】2.1 AT89S52单片机的硬件组成
- Initial polymorphism
- $attrs and $listeners components transfer values
- 如何开发一款在线Excel表格系统(上)
- 【Codeforces】 B. Make it Divisible by 25
猜你喜欢

Neural network implementation of handwritten numeral classification matlab

详解分布式系统的幂等

【单片机】2.3 AT89S52的CPU

如何限制root远程登入,使普通用户拥有root权限

Microsoft silently donated $10000 to curl, which was not notified until half a year later

Why is domestic Xinguan oral medicine a drug for the treatment of AIDS

Explain the pile of binary trees in detail

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

写好技术原创文章的一点建议

Uncle's nephew and his students
随机推荐
DDD(领域驱动设计)分层架构
7 岁男孩被 AI 机器人折断手指,仅因下棋太快?
【obs】x264_encoder_encode 编码输出pts dts和 framesize
Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
每条你收藏的资讯背后,都离不开TA
微软默默给 curl 捐赠一万美元,半年后才通知
信号量保护之位带操作
Smart fish tank design based on stm32
Neural network implementation of handwritten numeral classification matlab
Tencent cloud upload
Initial polymorphism
Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book
#夏日挑战赛#【FFH】实时聊天室之WebSocket实战
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?
Because the employee set the password to "123456", amd stolen 450gb data?
(2)融合cbam的two-stream项目搭建----数据准备
KMP template - string matching
Mysql: function
Introduction to cue language foundation: cue is a language born for configuration