当前位置:网站首页>numpy数组计算
numpy数组计算
2022-07-02 09:54:00 【starmultiple】
数组计算
查看数组形状
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)两行三列
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)
修改数组形状
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('-------------------')
三维数组变成二维一维
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]]"""
#赋值运算是将返回值返回到等式左端,与直接返回到显示器输出设备本质是相同的,只是承受的对象不同
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())#数据转化为一维[ 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('-----------------------')
数组加减乘除
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意思为not a number inf意为无穷大
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]]]'''
#广播原则,两个数组形状不相同时,也并不是完全不能进行计算,当其中一个数组形状为(a,b)时,另一个数组形状为(b)或者(a,1)时可以进行对应位置元素的数值运算,并且会广播到其他维数。
#331可以和任意(3,x)计算,反之除了(3,3),其余的(X,3)都不能计算
#再补充一下 补充一下:(a,b,c)可以和(b,c)以及(1.c) /(b,1)计算
当扩展到三维时,例如一个数组形状为(a,b,c),那么当另一个数组的形状为(a,b,1),(b,c),,(b,1)四种种情况时都可以完成广播计算。
边栏推荐
- Unity SKFramework框架(十四)、Extension 扩展函数
- Embedded software development
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- 三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
- 机器学习基础(二)——训练集和测试集的划分
- bellman-ford AcWing 853. Shortest path with side limit
- Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
- Jerry's watch stops ringing [article]
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- Jerry's watch delete alarm clock [chapter]
猜你喜欢

移动式布局(流式布局)

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

中文姓名提取(玩具代码——准头太小,权当玩闹)

Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本

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

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

C operator

Heap acwing 838 Heap sort

阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
![[opencv] [image gradient]](/img/37/1f366501e2b4e70ecee6365088167c.jpg)
[opencv] [image gradient]
随机推荐
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
spfa AcWing 851. SPFA finding the shortest path
To bypass obregistercallbacks, you need to drive the signature method
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Record idea shortcut keys
移动式布局(流式布局)
Browser node event loop
Mysql常用命令详细大全
The redis development document released by Alibaba covers all redis operations
架构师必须了解的 5 种最佳软件架构模式
The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
[opencv learning] [Canny edge detection]
【笔耕不辍勋章活动】生命不止,写作不息
Finally, someone explained the supervised learning clearly
Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
[opencv] [image gradient]
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Std:: vector batch import fast de duplication method
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials