对numpy.append()里的axis的用法详解
作者:我爱阿鑫 时间:2022-03-20 18:19:59
如下所示:
def append(arr, values, axis=None):
"""
Append values to the end of an array.
Parameters
----------
arr : array_like
Values are appended to a copy of this array.
values : array_like
These values are appended to a copy of `arr`. It must be of the
correct shape (the same shape as `arr`, excluding `axis`). If
`axis` is not specified, `values` can be any shape and will be
flattened before use.
axis : int, optional
The axis along which `values` are appended. If `axis` is not
given, both `arr` and `values` are flattened before use.
Returns
-------
append : ndarray
A copy of `arr` with `values` appended to `axis`. Note that
`append` does not occur in-place: a new array is allocated and
filled. If `axis` is None, `out` is a flattened array.
numpy.append(arr, values, axis=None):
简答来说,就是arr和values会重新组合成一个新的数组,做为返回值。而axis是一个可选的值
当axis无定义时,是横向加成,返回总是为一维数组!
Examples
--------
>>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
当axis有定义的时候,分别为0和1的时候。(注意加载的时候,数组要设置好,行数或者列数要相同。不然会有error:all the input array dimensions except for the concatenation axis must match exactly)
当axis为0时,数组是加在下面(列数要相同):
import numpy as np
aa= np.zeros((1,8))
bb=np.ones((3,8))
c = np.append(aa,bb,axis = 0)
print(c)
[[ 0. 0. 0. 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1.]]
当axis为1时,数组是加在右边(行数要相同):
import numpy as np
aa= np.zeros((3,8))
bb=np.ones((3,1))
c = np.append(aa,bb,axis = 1)
print(c)
[[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
来源:https://blog.csdn.net/qq_35019361/article/details/79055991
标签:numpy,append,axis
0
投稿
猜你喜欢
国内外字体网站(font)的整理
2007-10-14 09:58:00
SQL Server数据库和Oracle行转列的特殊方案描述
2010-07-26 15:14:00
Python如何合并多个字典或映射
2023-11-07 17:20:51
微信小程序实现图片上传功能实例(前端+PHP后端)
2023-11-05 14:19:27
一文详解Go语言fmt标准库的常用占位符使用
2023-08-07 01:57:56
python字符串的常用操作方法小结
2023-11-29 03:01:37
如何将Python脚本打包成exe应用程序介绍
2021-12-26 20:42:07
python快排算法详解
2023-08-24 04:17:08
解析一个通过添加本地分区索引提高SQL性能的案例
2023-07-22 13:29:32
用python将word文档合并实例代码
2021-08-13 06:17:01
Python3enumrate和range对比及示例详解
2021-02-05 02:11:47
python 定义类时,实现内部方法的互相调用
2023-11-20 11:09:31
ASP基础知识介绍
2009-02-11 13:44:00
Python清空文件并替换内容的实例
2023-03-22 04:09:43
Python 中的反转字符串reversed(),切片
2021-05-27 15:03:06
Python和perl实现批量对目录下电子书文件重命名的代码分享
2022-01-28 02:51:48
Python 并行化执行详细解析
2021-09-23 22:20:52
asp下用fso和ado.stream写xml文件的方法
2011-04-07 10:55:00
Http头 Range、Content-Range
2010-06-25 19:19:00
python中的json数据和pyecharts模块入门示例教程
2023-02-22 04:58:37