返回介绍

expand_dims

发布于 2021-06-12 09:43:38 字数 1169 浏览 1043 评论 0 收藏 0

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

numpy.expand_dims(arr, axis)

Where,

Sr.No.参数和描述
1

arr

输入数组

2

axis

插入新轴的位置

例子 (Example)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文