当前位置:网站首页>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]))) # 排序
边栏推荐
- OSS CDN alicloud configuration method
- 钉钉开放平台-小程序开发实战(钉钉小程序服务器端)
- PIP batch complete uninstall package
- PHP inherited in class return does not work
- 一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
- 小程序中实现视频通话及互动直播功能
- Mutex of thread synchronization (mutex)
- MySQL enable logbin in Qunhui docker
- Tp6 controller does not exist: app\index\controller\index
- Thinkphp6 implements a simple lottery system
猜你喜欢

How much do you make by writing a technical book? To tell you the truth, 2million is easy!

Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步

Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules

Capture packets (Wireshark)

SQL related knowledge - DDL

CTF PHP audit bypasses filtering learning from topics

Etcd watch principle

Mutex of thread synchronization (mutex)

Sixtool- source code of multi-functional and all in one generation hanging assistant
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me
随机推荐
Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
Nailing open platform - applet development practice (nailing applet server side)
Ubuntu installs PostgreSQL and uses omnidb to view
2021-02-07
Your requirements could not be resolved
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
Report on the "fourteenth five year plan" and future development direction of global and Chinese indoor vertical farms from 2022 to 2028
2020-12-18
Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
ctf [RoarCTF 2019]easy_ calc
[Nuggets' operation routine disclosure] the routine of being truly Nuggets
Nightmare
Clean up photo SCR virus / iframekill injection removal /iframekill removal photo scr
[Qunhui] this suite requires you to start PgSQL adapter service
BSC 及HT 等链的NFT 创造及绑定图片教程
Introduction to markdown grammar
PHP splits a string into arrays
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
Zhubo Huangyu: all the precious metals you want to know are here