当前位置:网站首页>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 .
边栏推荐
- Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
- Oracle from entry to mastery (4th Edition)
- Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
- leetcode621. 任务调度器
- Unity skframework Framework (XVI), package manager Development Kit Manager
- nohup命令
- OpenApi-Generator:简化RESTful API开发流程
- Hundreds of web page special effects can be used. Don't you come and have a look?
- Web基础
- SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
猜你喜欢
![Jerry's watch time synchronization [chapter]](/img/64/a48772b4e503ae0a2d36fc292e4e0d.jpg)
Jerry's watch time synchronization [chapter]

The redis development document released by Alibaba covers all redis operations

Unity SKFramework框架(十五)、Singleton 单例

Unity SKFramework框架(十四)、Extension 扩展函数
![Jerry's watch gets the default ringtone selection list [article]](/img/94/e469864fa6ab688dabe46f606efdbc.jpg)
Jerry's watch gets the default ringtone selection list [article]

de4000h存储安装配置

Five best software architecture patterns that architects must understand

Unity SKFramework框架(十三)、Question 问题模块

国产免费数据仓库ETL调度自动化运维专家—TASKCTL

spfa AcWing 852. SPFA judgement negative ring
随机推荐
moon
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
Unity skframework framework (XIII), question module
Five best software architecture patterns that architects must understand
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
PR usage skills, how to use PR to watermark?
Jerry's watch time synchronization [chapter]
How to get the operating system running PHP- How to get the OS on which PHP is running?
[opencv] [image gradient]
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Unity skframework Framework (XVI), package manager Development Kit Manager
【youcans 的图像处理学习课】总目录
Finally, someone explained the supervised learning clearly
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Ali on three sides, it's really difficult to successfully get the offer rated P7
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Unity SKFramework框架(十三)、Question 问题模块
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
研究表明“气味相投”更易成为朋友