当前位置:网站首页>numpy 通用函数
numpy 通用函数
2022-06-26 04:28:00 【莉莉妖_】
numpy 通用函数
数组形状:.T/.reshape()/.resize()
.T
转置,一维数组转置后结果不变,二维数组的行列交换(2,5) → (5,2)
import numpy
ar = numpy.zeros((5,2))
print(ar)
print(ar.T)
.reshape()
# 原数组的行列和改变后的行列要保持一致,例如(0,10) = (5,2)
ar2 = numpy.arange(0,10)
print(ar2)
print(ar2.reshape(5,2)) # 用法1:直接将已有数组改变形状
# 用法2:生成数组后直接改变形状
print(numpy.zeros((5,6)).reshape(10,3))
# 用法3:参数内添加数组,然后修改目标形状
print(numpy.reshape(numpy.arange(0,16),(2,8)))
.resize()
# 用法一:返回具有指定形状的新数组,如元素不够则重复填充所需数量的元素
print(numpy.resize(numpy.arange(5),(3,4)))
# 如果元素超出所需元素,则按所需元素为准
print(numpy.resize(numpy.arange(15),(3,4)))
ar = numpy.arange(0,10)
# 用法二:虽然输出 None,但是直接修改了数组本身
print(ar.resize(2,5))
print(ar)
注意:.T/.reshape()/.resize()都是生成新的数组
copy():数组的复制
# 赋值逻辑:指向内存中生成的一个值 →
# 这里ar1和ar2指向同一个值,所以ar1改变,ar2一起改变
import numpy
ar1 = numpy.arange(0,10)
ar2 = ar1
print(ar2)
ar1[1] = 10
print(ar2)
# copy():生成数组及其数据的完整拷贝,不受前数组影响
ar3 = ar1.copy()
ar1[1] = 20
print(ar2)
print(ar3)
.astype():数组类型转换
import numpy
ar1 = numpy.arange(0,10,dtype = float)
ar2 = ar1.astype(numpy.int64) # 默认numpy.int为int32
print(ar1,ar1.dtype)
print(ar2,ar2.dtype)
# 注意:养成好习惯,数组类型用numpy.int32,而不是直接int32
数组堆叠(参数使用元祖)
hstack():横向连接,vstack():垂直连接
stack(array,axis):任意连接
import numpy
ar1 = numpy.arange(0,5)
ar2 = numpy.arange(5,10)
print(numpy.hstack((ar1,ar2))) # 横向连接
ar3 = numpy.array([[1],[2],[3]])
ar4 = numpy.array([["a"],["b"]])
print(numpy.vstack((ar3,ar4))) # 竖向连接
# 重点解释axis参数的意思,假设两个数组[1 2 3]和[4 5 6],shape均为(3,0)
# axis=0:[[1 2 3] [4 5 6]],shape为(2,3)
# axis=1:[[1 4] [2 5] [3 6]],shape为(3,2)
ar5 = numpy.arange(1,4)
ar6 = numpy.arange(4,7)
print(numpy.stack((ar5,ar6),axis = 0))
数组拆分
hsplit():横向拆分,vsplit():垂直拆分
import numpy
ar = numpy.arange(0,16).reshape(4,4)
print(ar)
# 将数组水平(逐列)拆分为多个子数组 → 按列拆分
print(numpy.hsplit(ar,2))
# 将数组垂直(逐行)拆分为多个子数组 → 按行拆分
print(numpy.vsplit(ar,4))
# 输出结果为列表,列表中元素为数组
数组简单运算
import numpy
ar = numpy.arange(6).reshape(2,3)
print(ar)
print(ar + 10) # 加法
print(ar * 2) # 乘法
print(1 / (ar+1)) # 除法
print(ar ** 2) # 幂
# 与标量的运算
print(ar.mean()) # 求平均值
print(ar.max()) # 求最大值
print(ar.min()) # 求最小值
print(ar.std()) # 求标准差
print(ar.var()) # 求方差
# 求和,numpy.sum() → axis = 0:按列求和,axis = 1:按行求和
print(ar.sum(), numpy.sum(ar,axis = 0))
print(numpy.sort(numpy.array([1,4,3,2,5,6]))) # 排序
边栏推荐
- How much do you make by writing a technical book? To tell you the truth, 2million is easy!
- Jenkins introduces custom jars
- Redis cluster mode
- Thinkphp6 parsing QR code
- Laravel framework Alipay payment fails to receive asynchronous callback request [original]
- Computer network high frequency interview questions
- 2020-12-18
- [Qunhui] no port access (reverse proxy + intranet penetration)
- CTF crypto (I) some simple encoding and encryption
- PHP has the problem of using strtotime to obtain time in months and months [original]
猜你喜欢
钉钉开放平台-小程序开发实战(钉钉小程序客户端)
Swagger
Parse JSON interface and insert it into the database in batch
钉钉开放平台-小程序开发实战(钉钉小程序服务器端)
Using jsup to extract images from interfaces
Nailing open platform - applet development practice (nailing applet client)
Mutex of thread synchronization (mutex)
Nailing open platform - applet development practice (nailing applet server side)
Simple personal summary of tp6 multi application deployment -- Part I [original]
Threejs special sky box materials, five kinds of sky box materials are downloaded for free
随机推荐
Report on operation mode and future development trend of China's refining and chemical market 2022-2028
Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)
08_ Spingboot integrated redis
Nailing open platform - applet development practice (nailing applet server side)
Resolve PHP is not an internal or external command
Which is the best embedded visual programming software? (introduction, evaluation and selection of visual programming platform) [scratch, mind+, mixly]
Zeromq from getting started to mastering
redis集群的方式
Sixtool- source code of multi-functional and all in one generation hanging assistant
Introduction to markdown grammar
A troubleshooting of website crash due to high CPU
Be a hard worker from today on
Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
List of provinces, cities and counties in China
钉钉开放平台-小程序开发实战(钉钉小程序服务器端)
修改Oracle连接数
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Laravel pay payment access process
Oracle data pump table
Thinkphp6 implements a simple lottery system