0

I wish to create a numpy array of shape,

(205, 2) and it should look something like this for each tensor [1,0] x 205 times.

I tried to use np.ones([205,2]). However, the value is [1,1] for 205 times and not [1,0] for 205 times.

I am new to programming and would like to seek help from all big seniors here. I am just a baby programmer.

J...S
  • 5,079
  • 1
  • 20
  • 35
jjsonname
  • 3
  • 1
  • Possible duplicate of [Numpy - create matrix with rows of vector](https://stackoverflow.com/questions/33200625/numpy-create-matrix-with-rows-of-vector) – lakshayg Dec 19 '18 at 06:23

2 Answers2

1

There may be a better way (there is always a better way!), but here's what comes to my mind:

a = np.ones((205, 2)) - np.array((0, 1))

Alternatively:

a = np.ones((205, 2))
a[:, 1] = 0

Or:

a = np.zeros((205, 2))
a[:, 0] = 1

The latter two solutions are the fastest.

DYZ
  • 55,249
  • 10
  • 64
  • 93
0

And another way using np.tile():

np.tile([1,0], (205,1))

See here.

Yet another way is to use np.broadcast_to():

np.broadcast_to([1,0], (205, 2))

See here.

Edit:DYZ's solution using np.zeros() followed by the assignment of 1 to the other section seems to be the fastest solution here.

J...S
  • 5,079
  • 1
  • 20
  • 35