numpy數(shù)組坐標(biāo)軸問(wèn)題解決
不知道大家有沒(méi)有一種感覺(jué),每次當(dāng)使用numpy數(shù)組的時(shí)候坐標(biāo)軸總是傻傻分不清楚,然后就會(huì)十分的困惑,每次運(yùn)算都需要去嘗試好久才能得出想要的結(jié)果。這里我們來(lái)簡(jiǎn)單解釋一下numpy中一維,二維,三維數(shù)組的坐標(biāo)軸問(wèn)題。
首先我們討論一維的情況,代碼如下:
import numpy as np class Debug: ? ? def __init__(self): ? ? ? ? self.array1 = np.array([0, 1, 2]) ? ? ? ? self.array2 = np.array([[0], [1], [2]]) ? ? def mainProgram(self): ? ? ? ? print("The value of array1 is: ") ? ? ? ? print(self.array1) ? ? ? ? print("The shape of array1 is: ") ? ? ? ? print(self.array1.shape) ? ? ? ? print("The value of array2 is: ") ? ? ? ? print(self.array2) ? ? ? ? print("The shape of array2 is: ") ? ? ? ? print(self.array2.shape) if __name__ == '__main__': ? ? main = Debug() ? ? main.mainProgram() """ The value of array1 is:? [0 1 2] The shape of array1 is:? (3,) The value of array2 is:? [[0] ?[1] ?[2]] The shape of array2 is:? (3, 1) """
從上面的結(jié)果我們可以看到,一維橫數(shù)組沿著橫向排列,我們可以認(rèn)定為x軸向,它的數(shù)組大小為(3,),一維列數(shù)組沿著縱向排列,我們可以認(rèn)為是y軸方向,它的大小為(3, 1),我們可以從左向右,看出第二個(gè)參數(shù)代表的是橫向上的參數(shù)個(gè)數(shù),第一個(gè)參數(shù)代表的是縱向上的參數(shù)個(gè)數(shù),因此我們可以將橫向數(shù)組的大小(3,)理解為(,3)更為合適。
接下來(lái)我們研究一下二維數(shù)組,哪個(gè)參數(shù)對(duì)應(yīng)的是橫坐標(biāo),哪個(gè)參數(shù)對(duì)應(yīng)的是縱坐標(biāo)。
代碼如下:
import numpy as np class Debug: ? ? def __init__(self): ? ? ? ? self.array1 = np.ones((2, 3)) ? ? ? ? self.array2 = np.ones((3, 2)) ? ? def mainProgram(self): ? ? ? ? print("The value of array1 is: ") ? ? ? ? print(self.array1) ? ? ? ? ?print("The shape of array1 is: ") ? ? ? ? print(self.array1.shape) ? ? ? ? print("The value of array2 is: ") ? ? ? ? print(self.array2) ? ? ? ? print("The shape of array2 is: ") ? ? ? ? print(self.array2.shape) if __name__ == '__main__': ? ? main = Debug() ? ? main.mainProgram() """ The value of array1 is:? [[1. 1. 1.] ?[1. 1. 1.]] The shape of array1 is:? (2, 3) The value of array2 is:? [[1. 1.] ?[1. 1.] ?[1. 1.]] The shape of array2 is:? (3, 2) """
從上面的結(jié)果我們可以看出,從左向右,第一個(gè)參數(shù)代表的是(row), 第二個(gè)參數(shù)代表的是列(column)。我們知道numpy中默認(rèn)的是笛卡爾坐標(biāo)系,所以橫向?yàn)閤,縱向?yàn)閥,具體的請(qǐng)看坐標(biāo)系(超鏈接點(diǎn)擊跳轉(zhuǎn)查看)。所以對(duì)self.array1來(lái)說(shuō),定義時(shí)輸入的數(shù)組大小的(2, 3)代表沿著x軸擁有3個(gè)值,沿著y軸擁有2個(gè)值。對(duì)比上述得到的結(jié)果與我們?cè)谝痪S情況中推斷得到的結(jié)果,證明我們的理解是正確的。
接著我們討論三維的情況:代碼如下:
import numpy as np class Debug: ? ? def __init__(self): ? ? ? ? self.array1 = np.ones((2, 3, 4)) ? ? def mainProgram(self): ? ? ? ? print("The value of array1 is: ") ? ? ? ? print(self.array1) ? ? ? ? print("The shape of array1 is: ") ? ? ? ? print(self.array1.shape) if __name__ == '__main__': ? ? main = Debug() ? ? main.mainProgram() """ The value of array1 is:? [[[1. 1. 1. 1.] ? [1. 1. 1. 1.] ? [1. 1. 1. 1.]] ?[[1. 1. 1. 1.] ? [1. 1. 1. 1.] ? [1. 1. 1. 1.]]] The shape of array1 is:? (2, 3, 4) """
不難發(fā)現(xiàn),沿著x軸方向擁有4個(gè)值,沿著y軸方向擁有3個(gè)值,沿著z軸方向擁有2個(gè)值。
綜上所述,在numpy數(shù)組中,定義三維數(shù)組時(shí),從左向右, 第一個(gè)參數(shù)為z軸,第二個(gè)參數(shù)為y軸,第三個(gè)參數(shù)為x軸,即(z, y, x)。 對(duì)于各個(gè)坐標(biāo)軸在空間中的朝向問(wèn)題,建議閱讀numpy數(shù)組坐標(biāo)軸。之后我們會(huì)進(jìn)一步探討numpy模塊中的其他與坐標(biāo)軸相關(guān)的函數(shù)。
到此這篇關(guān)于numpy數(shù)組坐標(biāo)軸問(wèn)題解決的文章就介紹到這了,更多相關(guān)numpy數(shù)組坐標(biāo)軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python摳圖教程之使用OpenCV實(shí)現(xiàn)背景去除
這篇文章主要給大家介紹了關(guān)于Python摳圖教程之使用OpenCV實(shí)現(xiàn)背景去除的相關(guān)資料,背景去除是在很多視覺(jué)應(yīng)用里的主要預(yù)處理步驟,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用
csv文件是一種逗號(hào)分隔的純文本形式存儲(chǔ)的表格數(shù)據(jù),Python內(nèi)置了CSV模塊,可直接通過(guò)該模塊實(shí)現(xiàn)csv文件的讀寫(xiě)操作,下面這篇文章主要給大家介紹了關(guān)于Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用,需要的朋友可以參考下2022-09-09Python?hashlib模塊與subprocess模塊使用詳細(xì)介紹
這篇文章主要介紹了Python?hashlib模塊與subprocess模塊使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10python 使用MyQR和qrcode來(lái)制作二維碼
這篇文章主要介紹了python 如何使用MyQR和qrcode來(lái)制作二維碼,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-05-05在Python開(kāi)發(fā)環(huán)境中調(diào)用ChatGPT模型詳細(xì)過(guò)程
在開(kāi)發(fā)過(guò)程當(dāng)中時(shí)常需要使用 ChatGPT 來(lái)完成一些任務(wù),但總是使用網(wǎng)頁(yè)交互模式去 Web 端訪問(wèn) ChatGPT 是很麻煩的,這時(shí)候我們可以使用代碼來(lái)調(diào)用 ChatGPT 模型,本文將詳細(xì)介紹在 Python 開(kāi)發(fā)環(huán)境中調(diào)用 ChatGPT 模型過(guò)程,,需要的朋友可以參考下2023-05-05