当前位置:网站首页>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)四种种情况时都可以完成广播计算。
边栏推荐
- West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
- Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk
- Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
- Jerry's watch reads the alarm clock [chapter]
- C modifier
- How to get the operating system running PHP- How to get the OS on which PHP is running?
- Unity skframework framework (XXI), texture filter map resource filtering tool
- 移动式布局(流式布局)
- Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
- Jerry's watch time synchronization [chapter]
猜你喜欢
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
Hash table acwing 841 String hash
Interval DP acwing 282 Stone merging
Browser storage scheme
Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Unity skframework framework (XIV), extension extension function
无向图的桥
The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
随机推荐
Ali on three sides, it's really difficult to successfully get the offer rated P7
How to get the operating system running PHP- How to get the OS on which PHP is running?
中文姓名提取(玩具代码——准头太小,权当玩闹)
Jerry's weather code table [chapter]
mac(macos Monterey12.2 m1) 个人使用php开发
完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
net share
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
Efficiency comparison between ArrayList and LinkedList
Record idea shortcut keys
Linear DP acwing 897 Longest common subsequence
Std:: vector batch import fast de duplication method
自主可控三维云CAD:CrownCAD赋能企业创新设计
Jerry's watch ringtone audition [article]
C operator
[opencv learning] [Canny edge detection]
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
moon
JDBC prevent SQL injection problems and solutions [preparedstatement]