当前位置:网站首页>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]]
边栏推荐
- Smart fish tank design based on stm32
- 步 IE 后尘,Firefox 的衰落成必然?
- [MCU] 2.2 pin function of AT89S52
- The 7-year-old boy broke his finger by AI robot just because he played chess too fast?
- Database hyperphone (I)
- 一文理解分布式开发中的服务治理
- 神经网络实现手写数字分类matlab
- 【Codeforces】 B. Make it Divisible by 25
- High cost, difficult to implement, slow to take effect, what about open source security?
- 灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
猜你喜欢

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model

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

详解分布式系统的幂等

Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro

诸神黄昏,“猫抖快”告别大V时代

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

Database hyperphone (I)

Chen Yili of ICT Institute: reducing cost and increasing efficiency is the greatest value of cloud native applications

20年前,他是马云最大的敌人
随机推荐
Sharing of local file upload technology of SAP ui5 fileuploader
Lichuang EDA - layout and inspection of schematic diagram (III)
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
立创EDA——原理图的布局与检查(三)
Summer Challenge [FFH] real time chat room websocket practice
公网域名如何解析到内网IP服务器——快解析域名映射外网访问
运行loam_velodyne实时建图
Mysql: function
Array of C language
帮扶、提振、担当,第六届土巴兔718全民家装节的新价值和意义
An analysis of CPU explosion of a smart logistics WCS system in.Net
如何开发一款在线Excel表格系统(上)
树莓派驱动代码的编译和测试
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
Flutter的布局
Database hyperphone (I)
Coca Cola's primary challenge is not vitality forest
Today's sleep quality record 82 points
Smart fish tank design based on stm32
KMP模板——字符串匹配