Skip to content Skip to sidebar Skip to footer

Create Layer Of Any Shape In Numpy

I create a layered model like this: import numpy as np import pandas as pd import matplotlib.pyplot as plt a = np.full((9,10),1) a[:5,] = 1 a[5:10,] = 2 print(a) plt.imshow(a)

Solution 1:

Here is an example of how to index numpy arrays using iterables:

import numpy as np
import matplotlib.pyplotas plt    

a = np.full((10,10),1)

x = np.arange(10)
z = np.random.randint(0, 10, size=(1,10))

a[x,z] = 2

plt.imshow(a)
plt.show()

Note how I use astype(int) instead of round (EDIT: it is even better to use randint from the start -- thanks to kazemakase for the comment) and how I adjusted the range of a. I also replaced linspace with arange, as the latter is guaranteed to produce integers.

The result looks something like this:

result of code

Post a Comment for "Create Layer Of Any Shape In Numpy"