np.meshgrid と ndarray.reshape の挙動

ちょっとしたことから挙動を確かめる必要があったので、簡単なコードで試してみた。 以下ではランダムな1次元配列 x, y を用意し、np.meshgrid で2次元配列にする。それを一旦 flatten というか reshape(nx*ny) で 1次元配列にしたあと、再び reshape(ny, nx) で元に戻るかどうかを検証した。np.allclose は 2つの配列のすべての要素が近い(許容範囲)か判定する。

$ ipython3
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import numpy as np

In [2]: nx = 10; ny = 20

In [3]: x = np.random.rand(nx)

In [4]: y = np.random.rand(ny)

In [5]: xx, yy = np.meshgrid(x,y)

In [6]: xx.shape
Out[6]: (20, 10)

In [7]: xx2 = xx.reshape(nx*ny).reshape(ny,nx)

In [8]: np.allclose(xx,xx2)
Out[8]: True

(追記)要素が等しいかどうかを判定するには np.array_equal というのもあるようだ。この場合でも、True が返ってきた。