OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Appropriate applications for the as_strided function

  • Thread starter Thread starter Jeff Boker
  • Start date Start date
J

Jeff Boker

Guest
I am practicing on figuring out how to use the as_strided function in numpy. I began with the following example of my own where I generate 5 images of 3 x 3 where each image is full of 1s, the next full of 2s, and so on till 5. If I want convert the (5,3,3) volume to place all images into a single row, I can do the following which works:

Code:
import numpy as np
from numpy.lib.stride_tricks import as_strided

array_3d = np.empty((5, 3, 3))

for i in range(5):
    array_3d[i] = np.full((3, 3), i+1)

print(array_3d.itemsize)

array_2d = as_strided(array_3d, shape=(5, 9), strides=(8*9, 8))

print(array_2d)

This outputs the following:

Code:
8
[[1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [2. 2. 2. 2. 2. 2. 2. 2. 2.]
 [3. 3. 3. 3. 3. 3. 3. 3. 3.]
 [4. 4. 4. 4. 4. 4. 4. 4. 4.]
 [5. 5. 5. 5. 5. 5. 5. 5. 5.]]

Now I wanted to try harder example of placing the (3, 3) images side by side in the following manner using the as_strided function:

Code:
[[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]
 [1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]
 [1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]]

However, I cannot think of a way to do this just using the as_strided function. So I am wondering if this is just not possible only with the as_strided function and how can I know when it is appropriate to use the as_strided function apart from sliding window operations.
<p>I am practicing on figuring out how to use the as_strided function in numpy. I began with the following example of my own where I generate 5 images of 3 x 3 where each image is full of 1s, the next full of 2s, and so on till 5. If I want convert the (5,3,3) volume to place all images into a single row, I can do the following which works:</p>
<pre><code>import numpy as np
from numpy.lib.stride_tricks import as_strided

array_3d = np.empty((5, 3, 3))

for i in range(5):
array_3d = np.full((3, 3), i+1)

print(array_3d.itemsize)

array_2d = as_strided(array_3d, shape=(5, 9), strides=(8*9, 8))

print(array_2d)
</code></pre>
<p>This outputs the following:</p>
<pre><code>8
[[1. 1. 1. 1. 1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4. 4. 4. 4. 4.]
[5. 5. 5. 5. 5. 5. 5. 5. 5.]]
</code></pre>
<p>Now I wanted to try harder example of placing the (3, 3) images side by side in the following manner using the as_strided function:</p>
<pre><code>[[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]
[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]
[1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3. 4. 4. 4. 4. 5. 5. 5. 5.]]
</code></pre>
<p>However, I cannot think of a way to do this just using the as_strided function. So I am wondering if this is just not possible only with the as_strided function and how can I know when it is appropriate to use the as_strided function apart from sliding window operations.</p>
 
Top