np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn)
引言
這里對我們之前------np.concatenate()函數(shù)做一個補充說明。
我們知道對于 np.concatenate() 函數(shù),其第一個參數(shù)為需要被合并的數(shù)組對象集合,這里我們以兩個輸入數(shù)組 a1 和 a2 序列舉例,根據(jù)我們之前提到的,第一個參數(shù)的數(shù)組需要使用 () 或者 [] 符號括起來,否則會報錯。這里我們舉例進行說明。
示例1------無 () 或者 [] 符號
import numpy as np x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) y = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) z = np.concatenate(x, y) print(z) """ result: Traceback (most recent call last): ? File "D:/python/scientificCalculation/Interference/dug.py", line 14, in <module> ? ? z = np.concatenate(x, y) ? File "<__array_function__ internals>", line 5, in concatenate TypeError: only integer scalar arrays can be converted to a scalar index """
可以看到,當(dāng)我們不使用 () 或者 [] 符號將需要被級聯(lián)(拼接)的數(shù)組括起來時,會得到一個錯誤提示,翻譯過來就是,類型錯誤,僅整數(shù)標(biāo)量數(shù)組能夠被轉(zhuǎn)換為一個標(biāo)量索引。也就是說輸入進 np.concatenate() 函數(shù)的第一個數(shù)據(jù)應(yīng)該是一個數(shù)組形式的。顯然上述輸入不符合。
示例2------使用 () 符號
import numpy as np x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) y = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) z = np.concatenate((x, y)) print(z) """ result: [[[1 2] ? [3 4]] ?[[5 6] ? [7 8]] ?[[1 2] ? [3 4]] ?[[5 6] ? [7 8]]] """
可以看到,當(dāng)使用 () 符號時,我們得到了結(jié)果。
示例3------使用 [] 符號
import numpy as np x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) y = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) z = np.concatenate([x, y]) print(z) """ result: [[[1 2] ? [3 4]] ?[[5 6] ? [7 8]] ?[[1 2] ? [3 4]] ?[[5 6] ? [7 8]]] """
可以看到,當(dāng)使用 [] 符號時,我們也得到了結(jié)果。
總結(jié)
輸入 np.concatenate() 函數(shù)的第一個數(shù)據(jù)應(yīng)該是一個數(shù)組形式的,所以必須用 () 或者 [] 符號括起來。
到此這篇關(guān)于np.concatenate()函數(shù)數(shù)組序列參數(shù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)np.concatenate 數(shù)組序列參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch的torch.utils.data中Dataset以及DataLoader示例詳解
torch.utils.data?是?PyTorch?提供的一個模塊,用于處理和加載數(shù)據(jù),該模塊提供了一系列工具類和函數(shù),用于創(chuàng)建、操作和批量加載數(shù)據(jù)集,這篇文章主要介紹了Pytorch的torch.utils.data中Dataset以及DataLoader等詳解,需要的朋友可以參考下2023-08-08
Python api構(gòu)建tensorrt加速模型的步驟詳解
小編個人認(rèn)為python比c++更容易讀并且已經(jīng)有很多包裝很好的科學(xué)運算庫(numpy,scikit等),今天通過本文給大家分享Python api構(gòu)建tensorrt加速模型的步驟,感興趣的朋友一起看看吧2021-09-09
Python中urllib2模塊的8個使用細(xì)節(jié)分享
這篇文章主要介紹了Python中urllib2模塊的8個使用細(xì)節(jié)分享,本文講解了Proxy設(shè)置、Timeout設(shè)置、加入特定Header、Redirect、Cookie、PUT和DELETE方法等內(nèi)容,需要的朋友可以參考下2015-01-01
基于Python實現(xiàn)拆分和合并GIF動態(tài)圖
這篇文章主要介紹了Python拆分和合并GIF動態(tài)圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10

