numpy.stack()函数用于沿新轴连接相同尺寸数组的序列。axis参数指定结果轴尺寸中新轴的索引。例如,如果axis = 0,它将是第一个尺寸;如果axis = -1,它将是最后的尺寸。

用法: numpy.stack(arrays, axis)

参数:

arrays :[数组]相同形状的数组序列。

axis :[int]输入数组沿其堆叠的结果数组中的轴。

Return :[stacked ndarray]输入数组的堆栈数组,其维数比输入数组大。

代码1:

# Python program explaining

# stack() function

import numpy as geek

# input array

in_arr1 = geek.array([ 1, 2, 3] )

print ("1st Input array:\n", in_arr1)

in_arr2 = geek.array([ 4, 5, 6] )

print ("2nd Input array:\n", in_arr2)

# Stacking the two arrays along axis 0

out_arr1 = geek.stack((in_arr1, in_arr2), axis = 0)

print ("Output stacked array along axis 0:\n ", out_arr1)

# Stacking the two arrays along axis 1

out_arr2 = geek.stack((in_arr1, in_arr2), axis = 1)

print ("Output stacked array along axis 1:\n ", out_arr2)

输出:

1st Input array:

[1 2 3]

2nd Input array:

[4 5 6]

Output stacked array along axis 0:

[[1 2 3]

[4 5 6]]

Output stacked array along axis 1:

[[1 4]

[2 5]

[3 6]]

代码2:

# Python program explaining

# stack() function

import numpy as geek

# input array

in_arr1 = geek.array([[ 1, 2, 3], [ -1, -2, -3]] )

print ("1st Input array:\n", in_arr1)

in_arr2 = geek.array([[ 4, 5, 6], [ -4, -5, -6]] )

print ("2nd Input array:\n", in_arr2)

# Stacking the two arrays along axis 0

out_arr1 = geek.stack((in_arr1, in_arr2), axis = 0)

print ("Output stacked array along axis 0:\n ", out_arr1)

# Stacking the two arrays along axis 1

out_arr2 = geek.stack((in_arr1, in_arr2), axis = 1)

print ("Output stacked array along axis 1:\n ", out_arr2)

# Stacking the two arrays along last axis

out_arr3 = geek.stack((in_arr1, in_arr2), axis = -1)

print ("Output stacked array along last axis:\n ", out_arr3)

输出:

1st Input array:

[[ 1 2 3]

[-1 -2 -3]]

2nd Input array:

[[ 4 5 6]

[-4 -5 -6]]

Output stacked array along axis 0:

[[[ 1 2 3]

[-1 -2 -3]]

[[ 4 5 6]

[-4 -5 -6]]]

Output stacked array along axis 1:

[[[ 1 2 3]

[ 4 5 6]]

[[-1 -2 -3]

[-4 -5 -6]]]

Output stacked array along last axis:

[[[ 1 4]

[ 2 5]

[ 3 6]]

[[-1 -4]

[-2 -5]

[-3 -6]]]

python代码大全和用法用量_Python numpy.stack()用法及代码示例相关推荐

  1. python代码大全和用法用量_Python生成器的使用方法和示例代码

    本文是<Effect Python 编写高质量Python代码的59个有效方法>的学习笔记.主要记录生成器的使用方法和示例代码. 返回队列的函数 如果函数要产生一系列结果,那么最简单的做法 ...

  2. python中argmin函数_Python numpy.argmin()用法及代码示例

    numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...

  3. python中argmin函数_python中argmin函数_Python numpy.argmin()用法及代码示例

    numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...

  4. python的stack用法_Python numpy.stack函数方法的使用

    numpy.stack numpy.stack(arrays, axis=0, out=None)      [source] 沿着新的轴连接数组序列. axis参数在结果的维度中指定新轴的索引.例如 ...

  5. python arg_python argmin_python中argmin函数_Python numpy.argmin(

    numpy.argmin(array,axis = None,out = None):返回特定轴上数组min元素的索引. 参数: array:Input array to work on axis : ...

  6. python斐波那契递归_Python递归斐波那契示例

    python斐波那契递归 Good day, learners! In this tutorial we are going to learn about Python Recursion and u ...

  7. python代码大全和用法用量_python零基础入门命令方式汇总大全,快速恶补你的Python基础...

    原标题:python零基础入门命令方式汇总大全,快速恶补你的Python基础 无意中浏览到这篇来自大牛的分享,总结的很全面,我重新排版下放在这里,希望能帮助到大家.在此十分感谢原作者! 在开始之前还是 ...

  8. python中squeeze函数_Python numpy.squeeze()用法及代码示例

    当我们要从数组形状中删除一维条目时,将使用numpy.squeeze()函数. 用法: numpy.squeeze(arr, axis=None ) 参数: arr :[数组]输入数组. axis : ...

  9. python中numpy模块的around方法_Python numpy.around()用法及代码示例

    numpy.around(arr,decimals = 0,out = None):此数学函数可帮助用户将数组元素平均舍入为给定的小数位数. 参数: array :[数组]输入数组. decimal ...

最新文章

  1. mybatis + oracle insert clob,出现ORA-01461:仅能绑定要插入LONG列的LONG值
  2. 微软公布19财年财报:净利润增长22%,云计算首超个人计算业务
  3. python 排列组合之itertools
  4. 如何用 Netty 设计一个百万级推送服务?
  5. linux怎么看日期,linux 怎么用命令查看日期
  6. NGINX介绍及参数
  7. email 添加附件 java_Java发送email 带附件 | 学步园
  8. MTK 驱动(83)---mtklog相关知识
  9. 【Elasticsearch】eBay上的Elasticsearch性能调优实践
  10. 有人问李嘉诚的儿子李泽楷:“你父亲都教了你哪些成功赚钱的秘诀?”
  11. 多线程模拟渡河 C语言 Linux
  12. 苹果计算机磁盘格式,苹果电脑上怎么进行格式化磁盘?
  13. SD卡--摄像头中扩容卡原理及检测机制
  14. java 体检套餐_体检套餐的项目
  15. 史上最全 Vue 前端代码风格指南
  16. (4)Dojo学习之动画学习
  17. 修改Visual Studio Code编辑器的主题和代码颜色
  18. android vold,android – vold和udevd之间的区别
  19. 2022IC秋招面经分享【复旦微电子·数字前端工程师(上海)】
  20. 洛谷 P1983 车站分级

热门文章

  1. c/C++计算int / int *数组的长度;sizeof(指针),sizeof(数组名)的区别
  2. Vuex源码学习(五)加工后的module
  3. 【MySQL】简单命令总结
  4. Android AdapterView 源码分析以及其相关回收机制的分析
  5. angular-oauth2 —— NG 的 OAuth2 认证模块
  6. Android基于mAppWidget实现手绘地图(四)--如何附加javadoc
  7. ffmpeg源码分析:transcode_init()函数
  8. 文件格式 tar.gz zip zp2 zip rar
  9. 人月神话——焦油坑 《读后感》
  10. c# equals与==的区别