当前位置:网站首页>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框架(二十)、VFX Lab 特效库
- [opencv] [image gradient]
- Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
- Std:: vector batch import fast de duplication method
- 【OpenGL】笔记二十九、高级光照(镜面高光)
- 无向图的桥
- Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
- 移动式布局(流式布局)
- JS逆向之巨量创意signature签名
- Get started REPORT | today, talk about the microservice architecture currently used by Tencent
猜你喜欢

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具

【云原生数据库】遇到慢SQL该怎么办(上)?

最近公共祖先LCA的三种求法

难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸

(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP

嵌入式软件开发
![Jerry's watch delete alarm clock [chapter]](/img/7f/d51b37872b4ce905a0a723a514b2dc.jpg)
Jerry's watch delete alarm clock [chapter]

Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications

Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk

Interesting interview questions
随机推荐
Unity SKFramework框架(十三)、Question 问题模块
C modifier
Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
Unity SKFramework框架(十四)、Extension 扩展函数
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
Jerry's watch modifies the alarm clock [chapter]
每日一题:1175.质数排列
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
EasyDSS点播服务分享时间出错如何修改?
Jerry's weather code table [chapter]
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Linear DP acwing 902 Shortest editing distance
spfa AcWing 852. SPFA judgement negative ring
Linear DP acwing 896 Longest ascending subsequence II
Record idea shortcut keys
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
嵌入式软件开发