Reputation: 28022
I have this piece of code and I am finding it difficult to understand what is advantage of defining the numpy.zeros
method the way it is as shown below.
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)
What is the significance of mentioning of x
and y
?
Upvotes: 3
Views: 2641
Reputation: 231375
The zeros
creates a (10,10)
array, where each element has dtype
defined by np.dtype([('x',float),('y',float)])
. That is, each element consists of 2 floats, one called 'x', the other 'y'.
Z = np.zeros((10,10), [('x',float),('y',float)])
In a sense this makes a (10,10,2)
array, except that there is a 'wall' between the 2
dimension, and the others. For example you can't swap it one other other dimensions. But it is possible to 'view' it as a (10,10,2) array:
Z.view('float').reshape(10,10,2)
The 2 fields of Z
are indexed with Z['x']
and Z['y']
, the resulting views are each (10,10)
arrays.
The 2nd line sets the values of these 2 fields
Z['x'], Z['y'] = np.meshgrid ...
Normally meshgrid
returns 2 arrays, X, Y = np.meshgrid...
. So this is just a normal Python assignment.
I haven't seen this pairing of a structured array and meshgrid before, but it makes sense. Whether it is all that useful in another matter.
I was going to add an example of what Z
looks like, but @AndreL has done that for us. Note that the elements Z
are displayed as tuples
, implying they are different from 2 element columns of a 3d array.
Upvotes: 3
Reputation: 5593
The secret of the output is at numpy.linspace(0,1,10)
, outputs an numpy.array, with:
[ 0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
0.66666667 0.77777778 0.88888889 1. ]
For 'x'
shape, as for 'y'
, where '0'
is where it starts, '1'
is where is stops, with 10
samples.
The numpy.zeros()
are defining a matrix shape (M, N) for ‘ij’ indexing, where M = N = 10
The numpy.meshgrid()
indexes into the matrix the values of linspace
results, like ai, aj
e.g.
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
Outputs:
[[(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)]]
Outputting a matrix ij
scalars.
Check the next url's:
Upvotes: 1
Reputation: 18627
This is actually defining two separate ndarrays, one named 'x'
and the other 'y'
. While, in this case, it is unnecessary to specify the dtype
s, it is a way of creating this type of double ndarray
.
While this usage is not explicitly included in the numpy.zeros
documentation, they do show an example using it.
Edit:
@WarrenWeckesser links some documentation for these structured arrays
Upvotes: 3