jjsonname
jjsonname

Reputation: 3

create indices with numpy for zeros and ones

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.

Upvotes: 0

Views: 289

Answers (2)

J...S
J...S

Reputation: 5207

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.

Upvotes: 0

DYZ
DYZ

Reputation: 57033

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.

Upvotes: 1

Related Questions