当前位置:网站首页>Numpy array calculation
Numpy array calculation
2022-07-02 13:13:00 【starmultiple】
Array calculation
View array shapes
import numpy as np
t1=np.arange(3,12,2)
print(t1)#[ 3 5 7 9 11]
print(t1.shape)#(5,)
t2=np.array([[1,2,3],[4,5,6]])
print(t2)#[[1 2 3]
# [4 5 6]]
print(t2.shape)#(2, 3) Two lines and three columns
t3=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(t3)#[[[ 1 2 3]
# [ 4 5 6]]
# [[ 7 8 9]
# [10 11 12]]]
print(t3.shape)#(2, 2, 3)
Modify array shape
t4=np.arange(12)
print(t4)#[ 0 1 2 3 4 5 6 7 8 9 10 11]
print(t4.reshape((3,4)))
"""[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]"""
print('-------------------')
3D array becomes 2D and 1D
t5=np.arange(24).reshape((2,3,4))
print(t5)#[[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
print(t5.reshape((4,6)))
"""[[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]"""
# The assignment operation is to return the return value to the left end of the equation , It is essentially the same as returning directly to the display output device , It's just that the objects are different
print(t5.reshape((24,)))#[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(t5.reshape((24,1)))
"""[[ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23]]"""
print(t5.reshape((1,24)))#[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]]
print(t5.flatten())# Data is converted into one dimension [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print('-----------------------')
Array addition, subtraction, multiplication and division
t6=np.arange(12)
print(t4)#[ 0 1 2 3 4 5 6 7 8 9 10 11]
print(t6)#[ 0 1 2 3 4 5 6 7 8 9 10 11]
print(t6+2)#[ 2 3 4 5 6 7 8 9 10 11 12 13]
print(t6+t4)#[ 0 2 4 6 8 10 12 14 16 18 20 22]
print(t6+t4-1)#[-1 1 3 5 7 9 11 13 15 17 19 21]
print(t6*2)#[ 0 2 4 6 8 10 12 14 16 18 20 22]
print(t6/2)#[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]
#print(t6/0)#[nan inf inf inf inf inf inf inf inf inf inf inf]
# nan You mean for not a number inf Meaning infinity
print(t4*t6)#[ 0 1 4 9 16 25 36 49 64 81 100 121]
print(t2)#[[1 2 3]
# [4 5 6]]
print(t3)#[[[ 1 2 3]
# [ 4 5 6]]
# [[ 7 8 9]
# [10 11 12]]]
print(t2+t3)
'''[[[ 2 4 6] [ 8 10 12]] [[ 8 10 12] [14 16 18]]]'''
# Broadcasting principles , The shapes of the two arrays are different , It is not completely impossible to calculate , When one of the array shapes is (a,b) when , Another array shape is (b) perhaps (a,1) You can perform the numerical operation of the corresponding position element , And broadcast to other dimensions .
#331 You can talk to anyone (3,x) Calculation , On the contrary, except (3,3), The rest (X,3) Can't calculate
# Add a little more Add up :(a,b,c) You can talk to (b,c) as well as (1.c) /(b,1) Calculation
When expanding to three dimensions , For example, the shape of an array is (a,b,c), Then when the shape of another array is (a,b,1),(b,c),,(b,1) The broadcast calculation can be completed in all kinds of situations .
边栏推荐
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
- Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
- (7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
- Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
- Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- Unity SKFramework框架(十二)、Score 计分模块
- Mysql常用命令详细大全
- 文件的下载与图片的预览
猜你喜欢

Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy

Interesting interview questions
![[opencv] [image gradient]](/img/37/1f366501e2b4e70ecee6365088167c.jpg)
[opencv] [image gradient]

国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)

Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment

Interval DP acwing 282 Stone merging

Linear DP acwing 895 Longest ascending subsequence

Unity SKFramework框架(十九)、POI 兴趣点/信息点
![Jerry's watch ringtone audition [article]](/img/18/905c4b64443f4efca55188e36f4b28.jpg)
Jerry's watch ringtone audition [article]

Variable, "+" sign, data type
随机推荐
JS逆向之巨量创意signature签名
The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
【笔耕不辍勋章活动】生命不止,写作不息
Jerry's watch time synchronization [chapter]
[opencv learning] [Canny edge detection]
无向图的桥
js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
Finally, someone explained the supervised learning clearly
Browser storage scheme
Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee
Ali on three sides, it's really difficult to successfully get the offer rated P7
PR usage skills, how to use PR to watermark?
【云原生数据库】遇到慢SQL该怎么办(上)?
spfa AcWing 852. SPFA judgement negative ring
The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
Hash table acwing 841 String hash
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy