对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
投稿
猜你喜欢
python求最大值,不使用内置函数的实现方法
2021-02-06 09:13:12
js设计模式之单例模式原理与用法详解
2024-04-29 14:09:13
SQL判断字段列是否存在的方法
2024-01-22 16:30:26
Python中线程threading.Thread的使用详解
2023-07-22 13:25:48
MySQL数据库常用命令小结
2024-01-15 22:16:31
Python使用docx模块实现刷题功能代码
2021-03-12 22:28:30
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2022-08-26 07:14:52
PHP下 Mongodb 连接远程数据库的实例代码
2024-06-05 09:22:29
pytorch查看torch.Tensor和model是否在CUDA上的实例
2023-06-16 16:41:22
在Python中使用gRPC的方法示例
2021-02-02 16:20:21
python实现用类读取文件数据并计算矩形面积
2022-05-16 21:11:38
javascript+css图片切换效果
2007-08-04 20:00:00
Python动态规划实现虚拟机部署的算法思想
2022-12-13 05:02:47
使用Go实现优雅重启服务功能
2024-04-25 15:06:06
基于vue的验证码组件的示例代码
2024-06-05 15:29:22
python scrapy框架中Request对象和Response对象的介绍
2021-04-02 07:29:59
c#生成excel示例sql数据库导出excel
2024-01-26 16:55:07
简单谈谈MySQL5.7 JSON格式检索
2024-01-22 09:07:03
Python面向对象编程之类的概念
2021-12-24 10:56:33
D3.js 实现带伸缩时间轴拓扑图的示例代码
2024-05-09 10:20:13