当前位置:网站首页>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 .
边栏推荐
- Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
- 完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
- 研究表明“气味相投”更易成为朋友
- Counting class DP acwing 900 Integer partition
- Browser node event loop
- Web基础
- Jerry's watch gets the default ringtone selection list [article]
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- Finally, someone explained the supervised learning clearly
- Word efficiency guide - word's own template
猜你喜欢
Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
Linear DP acwing 897 Longest common subsequence
Ali on three sides, it's really difficult to successfully get the offer rated P7
三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
Jerry's watch delete alarm clock [chapter]
Unity skframework framework (XIV), extension extension function
Unity SKFramework框架(十九)、POI 兴趣点/信息点
阿里发布的Redis开发文档,涵盖了所有的redis操作
随机推荐
【OpenGL】笔记二十九、高级光照(镜面高光)
Linear DP acwing 896 Longest ascending subsequence II
机器学习基础(二)——训练集和测试集的划分
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
How can attribute mapping of entity classes be without it?
Record idea shortcut keys
[opencv learning] [image histogram and equalization]
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
C modifier
Unity skframework framework (XIV), extension extension function
de4000h存储安装配置
leetcode621. 任务调度器
Browser storage scheme
Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf
Jerry's watch ringtone audition [article]
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Unity SKFramework框架(十二)、Score 计分模块
How to get the operating system running PHP- How to get the OS on which PHP is running?
上海交大教授:何援军——包围盒(包容体/包围盒子)
国产免费数据仓库ETL调度自动化运维专家—TASKCTL