当前位置:网站首页>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)四种种情况时都可以完成广播计算。
边栏推荐
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- Interview questions for software testing - a collection of interview questions for large factories in 2022
- Unity SKFramework框架(十四)、Extension 扩展函数
- 难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
- 面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
- [opencv learning] [image filtering]
- Get started REPORT | today, talk about the microservice architecture currently used by Tencent
- Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
- How to get the operating system running PHP- How to get the OS on which PHP is running?
- The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
猜你喜欢
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
研究表明“气味相投”更易成为朋友
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
leetcode621. 任务调度器
Ali on three sides, it's really difficult to successfully get the offer rated P7
Variable, "+" sign, data type
Efficiency comparison between ArrayList and LinkedList
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Unity SKFramework框架(十四)、Extension 扩展函数
随机推荐
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
3 a VTT terminal regulator ncp51200mntxg data
Finally, someone explained the supervised learning clearly
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
MySQL: Invalid GIS data provided to function st_ geometryfromtext
JS逆向之巨量创意signature签名
Unity SKFramework框架(十五)、Singleton 单例
完全自主可控三维云CAD:CrownCAD便捷的命令搜索,快速定位所需命令具体位置。
Js1day (input / output syntax, data type, data type conversion, VaR and let differences)
Modular commonjs es module
Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
C operator
Obtain file copyright information
The redis development document released by Alibaba covers all redis operations
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
JS逆向之行行查data解密
JDBC prevent SQL injection problems and solutions [preparedstatement]
【youcans 的图像处理学习课】总目录
Linear DP acwing 895 Longest ascending subsequence