|NO.Z.00031|——————————|BigDataEnd|——|Hadoop&Python.v09|——|Arithmetic.v09|NumPy科学计算库:NumPy形状操作|
一、形状操作
### --- 数组变形
import numpy as np
arr1 = np.random.randint(0,10,size = (3,4,5))
arr2 = arr1.reshape(12,5) # 形状改变,返回新数组
arr3 = arr1.reshape(-1,5) # ⾃动“整形”,⾃动计算
### --- 数组转置
import numpy as np
arr1 = np.random.randint(0,10,size = (3,5)) # shape(3,5)
arr1.T # shape(5,3) 转置
arr2 = np.random.randint(0,10,size = (3,6,4)) # shape(3,6,4)
np.transpose(arr2,axes=(2,0,1)) # transpose改变数组维度 shape(4,3,6)
### --- 数组堆叠
import numpy as np
arr1 = np.array([[1,2,3]])
arr2 = np.array([[4,5,6]])
np.concatenate([arr1,arr2],axis = 0)
# 串联合并shape(2,3) axis = 0表示第⼀维串联 输出为
# array([[1, 2, 3],
# [4, 5, 6]])
np.concatenate([arr1,arr2],axis = 1)
# shape(1,6) axis = 1表示第⼆维串联 输出为:array([[1, 2, 3, 4, 5, 6]])
np.hstack((arr1,arr2)) # ⽔平⽅向堆叠 输出为:array([[1, 2, 3, 4, 5, 6]])
np.vstack((arr1,arr2))
# 竖直⽅向堆叠,输出为:
# array([[1, 2, 3],
# [4, 5, 6]])
### --- split数组拆分
import numpy as np
arr = np.random.randint(0,10,size = (6,5)) # shape(6,5)
np.split(arr,indices_or_sections=2,axis = 0) # 在第⼀维(6)平均分成两份
np.split(arr,indices_or_sections=[2,3],axis = 1) # 在第⼆维(5)以索引2,3为断点分割成3份
np.vsplit(arr,indices_or_sections=3) # 在竖直⽅向平均分割成3份
np.hsplit(arr,indices_or_sections=[1,4]) # 在⽔平⽅向,以索引1,4为断点分割成3份
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv027-python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了