對(duì)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):
簡(jiǎn)答來說,就是arr和values會(huì)重新組合成一個(gè)新的數(shù)組,做為返回值。而axis是一個(gè)可選的值
當(dāng)axis無定義時(shí),是橫向加成,返回總是為一維數(shù)組!
Examples -------- >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, 4, 5, 6, 7, 8, 9])
當(dāng)axis有定義的時(shí)候,分別為0和1的時(shí)候。(注意加載的時(shí)候,數(shù)組要設(shè)置好,行數(shù)或者列數(shù)要相同。不然會(huì)有error:all the input array dimensions except for the concatenation axis must match exactly)
當(dāng)axis為0時(shí),數(shù)組是加在下面(列數(shù)要相同):
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.]]
當(dāng)axis為1時(shí),數(shù)組是加在右邊(行數(shù)要相同):
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.]]
以上這篇對(duì)numpy.append()里的axis的用法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用APScheduler3.0.1 實(shí)現(xiàn)定時(shí)任務(wù)的方法
今天小編就為大家分享一篇使用APScheduler3.0.1 實(shí)現(xiàn)定時(shí)任務(wù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Pytorch中torch.nn.Softmax的dim參數(shù)用法說明
這篇文章主要介紹了Pytorch中torch.nn.Softmax的dim參數(shù)用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
PyCharm使用matplotlib報(bào)MatplotlibDeprecationWarning問題解決辦法
這篇文章主要給大家介紹了關(guān)于PyCharm使用matplotlib報(bào)MatplotlibDeprecationWarning問題解決的相關(guān)資料,主要是 matplotlib版本過高導(dǎo)致的,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
pycharm中下載的包但是import還是無法使用/報(bào)紅的解決方法
用pycharm開發(fā)時(shí),在導(dǎo)入自己寫的python文件時(shí)出現(xiàn)模塊名爆紅的情況,下面這篇文章主要給大家介紹了關(guān)于pycharm中下載包但是import還是無法使用/報(bào)紅的解決方法,需要的朋友可以參考下2023-02-02
利用Pandas和Numpy按時(shí)間戳將數(shù)據(jù)以Groupby方式分組
這篇文章主要介紹了利用Pandas和Numpy按時(shí)間戳將數(shù)據(jù)以Groupby方式分組,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
pandas基礎(chǔ)?Series與Dataframe與numpy對(duì)二進(jìn)制文件輸入輸出
這篇文章主要介紹了pandas基礎(chǔ)Series與Dataframe與numpy對(duì)二進(jìn)制文件輸入輸出,series是一種一維的數(shù)組型對(duì)象,它包含了一個(gè)值序列和一個(gè)數(shù)據(jù)標(biāo)簽2022-07-07

