对numpy.append()里的axis的用法详解
如下所示:
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.]]
以上这篇对numpy.append()里的axis的用法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Pytorch中torch.nn.Softmax的dim参数用法说明
这篇文章主要介绍了Pytorch中torch.nn.Softmax的dim参数用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
PyCharm使用matplotlib报MatplotlibDeprecationWarning问题解决办法
这篇文章主要给大家介绍了关于PyCharm使用matplotlib报MatplotlibDeprecationWarning问题解决的相关资料,主要是 matplotlib版本过高导致的,文中通过图文介绍的非常详细,需要的朋友可以参考下2023-06-06
pycharm中下载的包但是import还是无法使用/报红的解决方法
用pycharm开发时,在导入自己写的python文件时出现模块名爆红的情况,下面这篇文章主要给大家介绍了关于pycharm中下载包但是import还是无法使用/报红的解决方法,需要的朋友可以参考下2023-02-02
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
这篇文章主要介绍了利用Pandas和Numpy按时间戳将数据以Groupby方式分组,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-07-07
pandas基础 Series与Dataframe与numpy对二进制文件输入输出
这篇文章主要介绍了pandas基础Series与Dataframe与numpy对二进制文件输入输出,series是一种一维的数组型对象,它包含了一个值序列和一个数据标签2022-07-07


最新评论