numpy數(shù)組坐標軸問題解決
不知道大家有沒有一種感覺,每次當使用numpy數(shù)組的時候坐標軸總是傻傻分不清楚,然后就會十分的困惑,每次運算都需要去嘗試好久才能得出想要的結(jié)果。這里我們來簡單解釋一下numpy中一維,二維,三維數(shù)組的坐標軸問題。
首先我們討論一維的情況,代碼如下:
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ù)組沿著橫向排列,我們可以認定為x軸向,它的數(shù)組大小為(3,),一維列數(shù)組沿著縱向排列,我們可以認為是y軸方向,它的大小為(3, 1),我們可以從左向右,看出第二個參數(shù)代表的是橫向上的參數(shù)個數(shù),第一個參數(shù)代表的是縱向上的參數(shù)個數(shù),因此我們可以將橫向數(shù)組的大小(3,)理解為(,3)更為合適。
接下來我們研究一下二維數(shù)組,哪個參數(shù)對應(yīng)的是橫坐標,哪個參數(shù)對應(yīng)的是縱坐標。
代碼如下:
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é)果我們可以看出,從左向右,第一個參數(shù)代表的是(row), 第二個參數(shù)代表的是列(column)。我們知道numpy中默認的是笛卡爾坐標系,所以橫向為x,縱向為y,具體的請看坐標系(超鏈接點擊跳轉(zhuǎn)查看)。所以對self.array1來說,定義時輸入的數(shù)組大小的(2, 3)代表沿著x軸擁有3個值,沿著y軸擁有2個值。對比上述得到的結(jié)果與我們在一維情況中推斷得到的結(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個值,沿著y軸方向擁有3個值,沿著z軸方向擁有2個值。
綜上所述,在numpy數(shù)組中,定義三維數(shù)組時,從左向右, 第一個參數(shù)為z軸,第二個參數(shù)為y軸,第三個參數(shù)為x軸,即(z, y, x)。 對于各個坐標軸在空間中的朝向問題,建議閱讀numpy數(shù)組坐標軸。之后我們會進一步探討numpy模塊中的其他與坐標軸相關(guān)的函數(shù)。
到此這篇關(guān)于numpy數(shù)組坐標軸問題解決的文章就介紹到這了,更多相關(guān)numpy數(shù)組坐標軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Python中request發(fā)送post請求傳遞json參數(shù)的問題
這篇文章主要介紹了Python中request發(fā)送post請求傳遞json參數(shù)的問題,在Python中需要傳遞dict參數(shù),利用json.dumps將dict轉(zhuǎn)為json格式用post方法發(fā)起請求,感興趣的朋友跟隨小編一起看看吧2022-08-08
Django搭建項目實戰(zhàn)與避坑細節(jié)詳解
這篇文章主要給大家介紹了關(guān)于Django搭建項目實戰(zhàn)與避坑細節(jié)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python SQLAlchemy基本操作和常用技巧(包含大量實例,非常好)
這篇文章主要介紹了Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量實例,非常好的一個學(xué)習(xí)SQLAlchemy的教程,需要的朋友可以參考下2014-05-05
Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析
這篇文章主要介紹了Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07

