numpy 排列的 2D
2D of numpy arange
我想构建两个二维数组
a = [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5]]
b = [[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]
但是我不能使用for循环。我知道我可以使用 np.arange(5) 获得一个数组,但不确定如何将其转换为上面显示的 2 个二维数组。
任何帮助将不胜感激
您只需要 numpy.reshape
和 numpy.repeat
。使用这个:
import numpy as np
n_columns = 6
a = np.repeat(np.arange(6), n_columns)
a = a.reshape(6,n_columns)
array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5]])
b = a.T
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]])
此代码适用于任何 n_columns
值。
你可以试试np.repeat
:
>>> x = np.arange(6).reshape(1,-1)
>>> y = np.repeat(x,6,axis=0)
>>> y
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]])
>>> z = y.T
>>> z
array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0],
[2, 2, 2, 2, 2, 0],
[3, 3, 3, 3, 3, 0],
[4, 4, 4, 4, 4, 0],
[5, 5, 5, 5, 5, 0]])
您可以使用 numpy.mgrid
or numpy.meshgrid()
:
np.mgrid[0:6, 0:6]
# array([[[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]],
#
# [[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]])
np.meshgrid(np.arange(6), np.arange(6))
# [array([[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]),
# array([[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]])]
然后简单地解压结果
a, b = np.mgrid[0:6, 0:6]
还有np.indices
:
I,J = np.indices((6,6))
我想构建两个二维数组
a = [[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5]]
b = [[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]
但是我不能使用for循环。我知道我可以使用 np.arange(5) 获得一个数组,但不确定如何将其转换为上面显示的 2 个二维数组。 任何帮助将不胜感激
您只需要 numpy.reshape
和 numpy.repeat
。使用这个:
import numpy as np
n_columns = 6
a = np.repeat(np.arange(6), n_columns)
a = a.reshape(6,n_columns)
array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5]])
b = a.T
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]])
此代码适用于任何 n_columns
值。
你可以试试np.repeat
:
>>> x = np.arange(6).reshape(1,-1)
>>> y = np.repeat(x,6,axis=0)
>>> y
array([[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]])
>>> z = y.T
>>> z
array([[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0],
[2, 2, 2, 2, 2, 0],
[3, 3, 3, 3, 3, 0],
[4, 4, 4, 4, 4, 0],
[5, 5, 5, 5, 5, 0]])
您可以使用 numpy.mgrid
or numpy.meshgrid()
:
np.mgrid[0:6, 0:6]
# array([[[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]],
#
# [[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]])
np.meshgrid(np.arange(6), np.arange(6))
# [array([[0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5],
# [0, 1, 2, 3, 4, 5]]),
# array([[0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1],
# [2, 2, 2, 2, 2, 2],
# [3, 3, 3, 3, 3, 3],
# [4, 4, 4, 4, 4, 4],
# [5, 5, 5, 5, 5, 5]])]
然后简单地解压结果
a, b = np.mgrid[0:6, 0:6]
还有np.indices
:
I,J = np.indices((6,6))