-
Hello, I wonder if there's xarray functionality to tile the xarray "vector" to expand it into new dimensions and keep output of
with output:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
I might be wrong, but I don't think we have a function to do this tiling or repeating specifically in our API at the moment. This seems like a reasonable feature to add though - perhaps you're interested in submitting a PR @mliukis ?
That must mean that Alternatively you could get similar behaviour by using |
Beta Was this translation helpful? Give feedback.
-
What is the tiled array going to be used for? Usually when I have wanted to use tile it's to do some kind of cartesian product but xarray handles that just fine because the dimensions have been labeled: ds = xr.tutorial.scatter_example_dataset()
# Just do the equation, without the annoying broadcasting that tile is often used for:
ds.x * ds.z
Out[19]:
<xarray.DataArray (x: 3, z: 4)>
array([[0, 0, 0, 0],
[0, 1, 2, 3],
[0, 2, 4, 6]])
Coordinates:
* x (x) int32 0 1 2
* z (z) int32 0 1 2 3
# If you want tiling, one can use a math-y approach:
ds.x * (1 + 0*ds.z)
Out[20]:
<xarray.DataArray (x: 3, z: 4)>
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2]])
Coordinates:
* x (x) int32 0 1 2
* z (z) int32 0 1 2 3
# If you want tiling, one can use ones_like and multiply that:
ds.x * xr.ones_like(ds.z)
Out[23]:
<xarray.DataArray (x: 3, z: 4)>
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2]])
Coordinates:
* x (x) int32 0 1 2
* z (z) int32 0 1 2 3
# If you want tiling, broadcasting way:
xr.broadcast(ds.x, ds.z)[0]
Out[25]:
<xarray.DataArray 'x' (x: 3, z: 4)>
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2]])
Coordinates:
* x (x) int32 0 1 2
* z (z) int32 0 1 2 3
Attributes:
units: xunits |
Beta Was this translation helpful? Give feedback.
-
I agree with @Illviljan that the same can be achieved using broadcasting: In [11]: data = xr.DataArray(np.arange(0, 5), [("t", np.arange(0, 5))])
...: data
Out[11]:
<xarray.DataArray (t: 5)>
array([0, 1, 2, 3, 4])
Coordinates:
* t (t) int64 0 1 2 3 4
In [12]: data * xr.DataArray(np.ones([5, 3]), dims=("x", "y"))
Out[12]:
<xarray.DataArray (t: 5, x: 5, y: 3)>
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]],
[[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.]],
[[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.]]])
Coordinates:
* t (t) int64 0 1 2 3 4
Dimensions without coordinates: x, y Actually, we don't even need the multiplication: In [13]: tiled, _ = xr.broadcast(data, xr.DataArray(np.ones([5, 3]), dims=("x", "y")))
...: tiled
Out[13]:
<xarray.DataArray (t: 5, x: 5, y: 3)>
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]],
[[2, 2, 2],
[2, 2, 2],
[2, 2, 2],
[2, 2, 2],
[2, 2, 2]],
[[3, 3, 3],
[3, 3, 3],
[3, 3, 3],
[3, 3, 3],
[3, 3, 3]],
[[4, 4, 4],
[4, 4, 4],
[4, 4, 4],
[4, 4, 4],
[4, 4, 4]]])
Coordinates:
* t (t) int64 0 1 2 3 4
Dimensions without coordinates: x, y this is not a good API, though, so I think having something like |
Beta Was this translation helpful? Give feedback.
-
@Illviljan @keewis Thank you for the clues! I need to expand the vector for least square fit computations and filtering which is done before LSQ fit.
|
Beta Was this translation helpful? Give feedback.
I agree with @Illviljan that the same can be achieved using broadcasting: