您好,登錄后才能下訂單哦!
如下所示:
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的用法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。