当前位置:网站首页>Numpy stack function
Numpy stack function
2022-04-23 02:56:00 【Live up to your youth】
The function prototype
numpy.stack(arrays, axis=0, out=None)
Function interpretation
Stack arrays with the same shape on the specified dimension
The usage function
A simple example of a two-dimensional array
>>> a = np.arange(9).reshape((3, 3))
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> b = np.arange(9, 18).reshape((3, 3))
>>> b
array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
>>> c = np.arange(18, 27).reshape((3, 3))
>>> c
array([[18, 19, 20],
[21, 22, 23],
[24, 25, 26]])
# take a、b、c Three arrays on the axis axis=0 Stack on , Get a three-dimensional array
>>> d = np.stack((a, b, c), axis=0)
>>> d
array([[[ 0, 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]]])
# take a、b、c Three arrays on the axis axis=1 Stack on , Also get a three-dimensional array
>>> e = np.stack((a, b, c), axis=1)
>>> e
array([[[ 0, 1, 2],
[ 9, 10, 11],
[18, 19, 20]],
[[ 3, 4, 5],
[12, 13, 14],
[21, 22, 23]],
[[ 6, 7, 8],
[15, 16, 17],
[24, 25, 26]]])
>>> f = np.stack((a, b, c), axis=2)
>>> f
array([[[ 0, 9, 18],
[ 1, 10, 19],
[ 2, 11, 20]],
[[ 3, 12, 21],
[ 4, 13, 22],
[ 5, 14, 23]],
[[ 6, 15, 24],
[ 7, 16, 25],
[ 8, 17, 26]]])
Let's take another example of a three-dimensional array
>>> a = np.zeros((2, 2, 2), dtype=int)
>>> a
array([[[0, 0],
[0, 0]],
[[0, 0],
[0, 0]]])
>>> b = np.ones((2, 2, 2), dtype=int)
>>> b
array([[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]]])
>>> c = np.full((2, 2, 2), 2, dtype=int)
>>> c
array([[[2, 2],
[2, 2]],
[[2, 2],
[2, 2]]])
# In the shaft axis=0 Stack on
>>> d = np.stack((a, b, c), axis=0)
>>> d
array([[[[0, 0],
[0, 0]],
[[0, 0],
[0, 0]]],
[[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]]],
[[[2, 2],
[2, 2]],
[[2, 2],
[2, 2]]]])
>>> d.shape
(3, 2, 2, 2)
>>> e = np.stack((a, b, c), axis=1)
>>> e
array([[[[0, 0],
[0, 0]],
[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]]],
[[[0, 0],
[0, 0]],
[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]]]])
>>> e.shape
(2, 3, 2, 2)
# In the shaft axis=2 Stack on
>>> f = np.stack((a, b, c), axis=2)
>>> f
array([[[[0, 0],
[1, 1],
[2, 2]],
[[0, 0],
[1, 1],
[2, 2]]],
[[[0, 0],
[1, 1],
[2, 2]],
[[0, 0],
[1, 1],
[2, 2]]]])
>>> f.shape
(2, 2, 3, 2)
# amount to g = np.stack((a, b, c), axis=-1)
>>> g = np.stack((a, b, c), axis=3)
>>> g
array([[[[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2]]],
[[[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2]]]])
>>> g.shape
(2, 2, 2, 3)
版权声明
本文为[Live up to your youth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220657127458.html
边栏推荐
- Codeforces Round #784 (Div. 4) (A - H)题解
- Opencv reads webcam video and saves it locally
- php+mysql对下拉框搜索的内容修改
- Sonic cloud real machine tutorial
- VirtualBox virtual machine (Oracle VM)
- JZ35 replication of complex linked list
- 接口请求时间太长,jstack观察锁持有情况
- Linux Redis——Redis 数据库缓存服务
- Android 高阶面试必问:全局业务和项目的架构设计与重构
- mysql function函数语法
猜你喜欢

First knowledge of C language ~ branch statements

Traversée de l'arbre L2 - 006

Rhcsa day 1 operation

Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl

Windows MySQL 8 zip installation

php+mysql对下拉框搜索的内容修改

Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5

工业互联网+危化安全生产综合管理平台怎样建

重大危险源企业如何保障年底前完成双预防机制数字化建设任务

Domestic lightweight Kanban scrum agile project management tool
随机推荐
win查看端口占用 命令行
Leangoo brain map - shared multi person collaborative mind mapping tool
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
Shell learning notes -- shell processing of output stream awk
Android 高阶面试必问:全局业务和项目的架构设计与重构
Planning code ROS migration POMDP prediction planning (I)
基于Scrum进行创新和管理
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
[learn junit5 from official documents] [II] [writingtests] [learning notes]
Traversée de l'arbre L2 - 006
MySQL function syntax
Servlet template engine usage example
Classification and regression tree of machine learning
Processes and threads
Restart redis
php+mysql对下拉框搜索的内容修改
Encapsulation of ele table
Innovation and management based on Scrum
[unity3d] rolling barrage effect in live broadcasting room