python – numpy.zeros方法中的这个参数是什么意思?

我有这段代码,我发现很难理解定义numpy.zeros方法的优点是如下所示.

Z = np.zeros((10,10), [('x',float),('y',float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10),
                         np.linspace(0,1,10))

print(Z)

提及x和y有什么意义?

最佳答案 输出的秘密在numpy.linspace(0,1,10),输出一个numpy.array,其中:

[ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
  0.66666667  0.77777778  0.88888889  1.        ]

对于’x’形状,对于’y’,其中’0’是开始的位置,’1’是停止的位置,有10个样本.

numpy.zeros()为’ij’索引定义矩阵形状(M,N),其中M = N = 10

numpy.meshgrid()将linspace结果的值索引到矩阵中,如ai,aj

例如

Z = np.zeros((10,10), [('x',int),('y',int)])
Z['x'], Z['y'] = np.meshgrid( np.linspace(0,10,10), np.linspace(0,10,10))
print Z

输出:

[[(0, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0) (8, 0) (10, 0)]
 [(0, 1) (1, 1) (2, 1) (3, 1) (4, 1) (5, 1) (6, 1) (7, 1) (8, 1) (10, 1)]
 [(0, 2) (1, 2) (2, 2) (3, 2) (4, 2) (5, 2) (6, 2) (7, 2) (8, 2) (10, 2)]
 [(0, 3) (1, 3) (2, 3) (3, 3) (4, 3) (5, 3) (6, 3) (7, 3) (8, 3) (10, 3)]
 [(0, 4) (1, 4) (2, 4) (3, 4) (4, 4) (5, 4) (6, 4) (7, 4) (8, 4) (10, 4)]
 [(0, 5) (1, 5) (2, 5) (3, 5) (4, 5) (5, 5) (6, 5) (7, 5) (8, 5) (10, 5)]
 [(0, 6) (1, 6) (2, 6) (3, 6) (4, 6) (5, 6) (6, 6) (7, 6) (8, 6) (10, 6)]
 [(0, 7) (1, 7) (2, 7) (3, 7) (4, 7) (5, 7) (6, 7) (7, 7) (8, 7) (10, 7)]
 [(0, 8) (1, 8) (2, 8) (3, 8) (4, 8) (5, 8) (6, 8) (7, 8) (8, 8) (10, 8)]
 [(0, 10) (1, 10) (2, 10) (3, 10) (4, 10) (5, 10) (6, 10) (7, 10) (8, 10)
  (10, 10)]]

输出矩阵ij标量.

检查下一个网址:

> numpy.linspace()
> numpy.zeros()
> numpy.meshgrid()

点赞