亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

np.meshgrid中的indexing參數(shù)問題解決

 更新時間:2023年03月13日 09:21:38   作者:勤奮的大熊貓  
本文主要介紹了np.meshgrid中的indexing參數(shù)問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

meshgrid函數(shù)在二維空間中可以簡單地理解為將x軸與y軸的每個位置的坐標關聯(lián)起來形成了一個網(wǎng)格,我們知道空間中的點是由坐標確定的,因此,當x與y關聯(lián)起來后,我們便可以給與某個點某個特定值并畫出對應的圖像。具體的可以百度一下,會有很多較為詳細的介紹。

這里我想要著重的說一下二維以及三維的meshgrid的參數(shù)indexing的問題。

二維meshgrid函數(shù)

import numpy as np


class Debug:
    def __init__(self):
        self.x = np.arange(5)
        self.y = np.arange(5)
        
    def grid(self):
        X, Y = np.meshgrid(self.x, self.y, indexing="xy")
        return X, Y
    

main = Debug()
X, Y = main.grid()
print("The X grid is:")
print(X)
print("The Y grid is:")
print(Y)
"""
The X grid is:
[[0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]]
The Y grid is:
[[0 0 0 0 0]
 [1 1 1 1 1]
 [2 2 2 2 2]
 [3 3 3 3 3]
 [4 4 4 4 4]]
"""

從上面的結果可以看出,所獲取的網(wǎng)格對應如下圖所示,橫向為x軸,縱向為y軸,類似于我們在幾何空間中使用的坐標系, 我們通常稱之為笛卡爾坐標系(Cartesian coordinate)。在二維meshgrid網(wǎng)格創(chuàng)建命令中,笛卡爾坐標系是默認的坐標系。

在這里插入圖片描述

然而在python編程中,還有一種較為常用的indexing取法,代碼如下:

import numpy as np


class Debug:
    def __init__(self):
        self.x = np.arange(5)
        self.y = np.arange(5)
        
    def grid(self):
        X, Y = np.meshgrid(self.x, self.y, indexing="ij")
        return X, Y
    

main = Debug()
i, j = main.grid()
print("The i grid is:")
print(i)
print("The j grid is:")
print(j)
"""
The i grid is:
[[0 0 0 0 0]
 [1 1 1 1 1]
 [2 2 2 2 2]
 [3 3 3 3 3]
 [4 4 4 4 4]]
The j grid is:
[[0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]
 [0 1 2 3 4]]
"""

此時從上面的結果我們可以看出,所獲取的網(wǎng)格對應如下圖所示,縱向為i軸,橫向為j軸,我們在編程中通常很少使用的這種坐標系。但是它也有自己的優(yōu)勢,這里不進一步說明。

在這里插入圖片描述

三維meshgrid函數(shù)

進一步我們討論三維的情況,代碼如下:

import numpy as np


class Debug:
    def __init__(self):
        self.x = np.arange(3)
        self.y = np.arange(3)
        self.z = np.arange(3)
        
    def grid(self):
        X, Y, Z = np.meshgrid(self.x, self.y, self.z)
        return X, Y, Z
    

main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X)
print("The Y grid is:")
print(Y)
print("The Z grid is:")
print(Z)
"""
The X grid is:
[[[0 0 0]
  [1 1 1]
  [2 2 2]]

 [[0 0 0]
  [1 1 1]
  [2 2 2]]

 [[0 0 0]
  [1 1 1]
  [2 2 2]]]
The Y grid is:
[[[0 0 0]
  [0 0 0]
  [0 0 0]]

 [[1 1 1]
  [1 1 1]
  [1 1 1]]

 [[2 2 2]
  [2 2 2]
  [2 2 2]]]
The Z grid is:
[[[0 1 2]
  [0 1 2]
  [0 1 2]]

 [[0 1 2]
  [0 1 2]
  [0 1 2]]

 [[0 1 2]
  [0 1 2]
  [0 1 2]]]
"""

由上面的結果我們可以看到,此時的坐標軸對應如下圖像:

在這里插入圖片描述

x軸向下,y軸向屏幕內側,z軸向右側,在三維圖像中不再根據(jù)indexing值來區(qū)分坐標軸了,而是統(tǒng)一規(guī)定了坐標軸的取法,只有對于這個坐標軸的取法深入理解,才能在之后的三維數(shù)據(jù)處理中游刃有余。

特別說明

但是這里有一個問題,來看一組代碼:

class Debug:
    def __init__(self):
        x = np.array([[[0],
                       [2]], [[4],
                              [6]], [[8],
                                     [10]]])
        print(x.shape)


main = Debug()
"""
(3, 2, 1)
"""

我們可以看到,輸出結果為(3, 2, 1),即沿著x1個元素,沿著y2個元素,沿著z3個元素。再來看一下我們使用meshgrid方法生成三維網(wǎng)格的情況。

import numpy as np


class Debug:
    def __init__(self):
        self.x = np.arange(1)
        self.y = np.arange(2)
        self.z = np.arange(3)

    def grid(self):
        X, Y, Z = np.meshgrid(self.x, self.y, self.z)
        return X, Y, Z


main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X.shape)
print("The Y grid is:")
print(Y.shape)
print("The Z grid is:")
print(Z.shape)
"""
The X grid is:
(2, 1, 3)
The Y grid is:
(2, 1, 3)
The Z grid is:
(2, 1, 3)
"""

我們可以看到,最終輸出的X,Y,Zshape均為(2, 1, 3),這對應的是沿著x3個元素,沿著y1個元素,沿著z2個元素。突然感覺有些混亂,不符合我們之前想要得到的x,y,z的排列順序,為了能夠得到正常的排列順序,我們可以使用如下代碼:

import numpy as np


class Debug:
    def __init__(self):
        self.x = np.arange(1)
        self.y = np.arange(2)
        self.z = np.arange(3)

    def grid(self):
        X, Y, Z = np.meshgrid(self.y, self.z, self.x)
        return X, Y, Z


main = Debug()
X, Y, Z = main.grid()
print("The X grid is:")
print(X.shape)
print("The Y grid is:")
print(Y.shape)
print("The Z grid is:")
print(Z.shape)
"""
The X grid is:
(3, 2, 1)
The Y grid is:
(3, 2, 1)
The Z grid is:
(3, 2, 1)
"""

可以看到運行后我們得到了符合Python默認坐標軸習慣的網(wǎng)格形式,這時對應的x軸向右側,y軸向下,z軸向屏幕里面。這個僅僅是為了理解需要,實際操作中無需進行這種坐標軸變換操作,直接使用默認的三維坐標軸方向即可。

到此這篇關于np.meshgrid中的indexing參數(shù)問題解決的文章就介紹到這了,更多相關np.meshgrid的indexing參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論