NumPyexpand_dims函数

/ / NumPyexpand_dims函数

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

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

arr

输入数组

2

要插入新轴的位置

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)

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

Python工匠:案例、技巧与工程实践 -〔朱雷〕

超级访谈:对话道哥 -〔吴翰清(道哥)〕

运维监控系统实战笔记 -〔秦晓辉〕

网络排查案例课 -〔杨胜辉〕

如何看懂一幅画 -〔罗桂霞〕

架构实战案例解析 -〔王庆友〕

安全攻防技能30讲 -〔何为舟〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

软件测试52讲 -〔茹炳晟〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者