当前位置:网站首页>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)四种种情况时都可以完成广播计算。
边栏推荐
- Lucky numbers in the [leetcode daily question] matrix
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
- [opencv] [image gradient]
- Async/await asynchronous function
- Hundreds of web page special effects can be used. Don't you come and have a look?
- Apply lnk306gn-tl converter, non isolated power supply
- Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
- [opencv learning] [contour detection]
- Everyone wants to eat a broken buffet. It's almost cold
- Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
猜你喜欢

Unity skframework framework (XIV), extension extension function

Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China

国产免费数据仓库ETL调度自动化运维专家—TASKCTL
![[opencv learning] [image filtering]](/img/4c/fe22e9cdf531873a04a7c4e266228d.jpg)
[opencv learning] [image filtering]

JS iterator generator asynchronous code processing promise+ generator - > await/async
![[error record] cannot open](/img/d3/0435ae698ad635be71729c7c047a22.jpg)
[error record] cannot open "XXX" because Apple cannot check whether it contains malware

Variable, "+" sign, data type

上海交大教授:何援军——包围盒(包容体/包围盒子)

Linear DP acwing 896 Longest ascending subsequence II
![[200 opencv routines] 100 Adaptive local noise reduction filter](/img/89/9e9b667dd28cb25af005b6028ef26c.jpg)
[200 opencv routines] 100 Adaptive local noise reduction filter
随机推荐
Mysql常用命令详细大全
mac(macos Monterey12.2 m1) 个人使用php开发
3 a VTT terminal regulator ncp51200mntxg data
[opencv learning] [moving object detection]
记忆函数的性能优化
架构师必须了解的 5 种最佳软件架构模式
阿里发布的Redis开发文档,涵盖了所有的redis操作
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
leetcode621. 任务调度器
无向图的桥
机器学习基础(二)——训练集和测试集的划分
Embedded software development
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
Unity SKFramework框架(十五)、Singleton 单例
Oracle from entry to mastery (4th Edition)
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
VIM super practical guide collection of this one is enough
[opencv learning] [contour detection]
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)