当前位置:网站首页>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)四种种情况时都可以完成广播计算。
边栏推荐
- Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
- Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
- Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
- Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
- [200 opencv routines] 100 Adaptive local noise reduction filter
- Mysql常用命令详细大全
- net share
- [opencv learning] [moving object detection]
- nohup命令
- moon
猜你喜欢

The redis development document released by Alibaba covers all redis operations

Interesting interview questions

Js1day (input / output syntax, data type, data type conversion, VaR and let differences)

mac(macos Monterey12.2 m1) 个人使用php开发

Browser storage scheme

Efficiency comparison between ArrayList and LinkedList

面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六

Dijkstra AcWing 850. Dijkstra finding the shortest circuit II

无向图的桥
![[opencv learning] [contour detection]](/img/96/aaec61f137e4526c2c329e6fcfa1a2.jpg)
[opencv learning] [contour detection]
随机推荐
Efficiency comparison between ArrayList and LinkedList
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
MySQL: Invalid GIS data provided to function st_ geometryfromtext
Mobile layout (flow layout)
Post order traversal sequence of 24 binary search tree of sword finger offer
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
Interval DP acwing 282 Stone merging
[opencv learning] [common image convolution kernel]
Counting class DP acwing 900 Integer partition
【云原生数据库】遇到慢SQL该怎么办(上)?
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
Apply lnk306gn-tl converter, non isolated power supply
Variable, "+" sign, data type
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
Unity SKFramework框架(十四)、Extension 扩展函数
Unity skframework framework (XIX), POI points of interest / information points
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸