当前位置:网站首页>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]))) # 排序
边栏推荐
- Nightmare
- Laravel uses phpword to generate word documents
- SQL related knowledge - DQL
- Install cenos in the virtual machine
- The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
- Tencent Interviewer: How did binder get its system services?
- Laravel file stream download file
- NFT creation and binding of BSC and HT chains
- CTF crypto (I) some simple encoding and encryption
- redis集群的方式
猜你喜欢

Zeromq from getting started to mastering

How does virtual box virtual machine software accelerate the network speed in the virtual system?

SQL related knowledge - DDL
![Laravel framework Alipay payment fails to receive asynchronous callback request [original]](/img/a7/139604ec3a977f2a4e96f392e56602.jpg)
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me

六、项目实战---识别猫和狗

Nailing open platform - applet development practice (nailing applet client)

The open software of win10 system is too small. How to make it larger (effective through personal test)

Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start

2021/11/6-burpsuit packet capturing and web page source code modification
随机推荐
Wechat applet is bound to a dynamic array to implement a custom radio box (after clicking the button, disable the button and enable other buttons)
Upload script file (one sentence back door) WAF bypass (PHP)
Laravel access error could not be opened
Performance test comparison between PHP framework jsnpp and thinkphp6
Sixtool- source code of multi-functional and all in one generation hanging assistant
Zhubo Huangyu: you can try these non-agricultural operation skills
Install cenos in the virtual machine
PHP small factory moves bricks for three years - interview series - my programming life
Yapi cross domain request plug-in installation
Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
Microsoft prohibits Russian users from downloading and installing win10/11
Simple use of redis in laravel
An unexpected attempt (Imperial CMS list template filters spaces and newlines in smalltext introduction)
MySQL enable logbin in Qunhui docker
mysql自带的性能测试工具mysqlslap执行压力测试
Install SVN in Pagoda and build SVN version Library
MySQL index details
Laravel pay payment access process
Parse JSON interface and insert it into the database in batch
Install dbeaver and connect Clickhouse