|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

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示