NumPy 中的 expand_dims函数

首页 / Numpy入门教程 / NumPy 中的 expand_dims函数

此功能通过在指定位置插入新轴来扩展数组。该功能需要两个参数。

numpy.expand_dims(arr, axis)
Sr.No. Parameter & 描述
1

arr

输入数组

2

axis

无涯教程网

要插入新轴的位置

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-expand-dims.html

来源:LearnFk无涯教程网

import numpy as np 
x = np.array(([1,2],[3,4])) 

print 'Array x:' 
print x 
print '\n'  
y = np.expand_dims(x, axis = 0) 

print 'Array y:' 
print y 
print '\n'

print 'The shape of X and Y array:' 
print x.shape, y.shape 
print '\n'  
# insert axis at position 1 
y = np.expand_dims(x, axis = 1) 

print 'Array Y after inserting axis at position 1:' 
print y 
print '\n'  

print 'x.ndim and y.ndim:' 
print x.ndim,y.ndim 
print '\n'  

print 'x.shape and y.shape:' 
print x.shape, y.shape

上述程序的输出如下:

Array x:
[[1 2]
 [3 4]]

Array y:
[[[1 2]
 [3 4]]]

The shape of X and Y array:
(2, 2) (1, 2, 2)

Array Y after inserting axis at position 1:
[[[1 2]]
 [[3 4]]]

x.ndim and y.ndim:
2 3

x.shape and y.shape:
(2, 2) (2, 1, 2)

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

黄勇的OKR实战笔记 -〔黄勇〕

编译原理之美 -〔宫文学〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

设计模式之美 -〔王争〕

检索技术核心20讲 -〔陈东〕

打造爆款短视频 -〔周维〕

如何落地业务建模 -〔徐昊〕

林外 · 专利写作第一课 -〔林外〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)