3

This is what I am doing now to achieve what I want.

In: 
a=numpy.zeros((3,2))
a[range(a.shape[0]),[0,0,1]] = 1
a
Out:
array([[ 1.,  0.],
       [ 1.,  0.],
       [ 0.,  1.]])

As you can see, I used range function to select all the rows in a. Is there any other cleaner way to select every row?

Thoran
  • 8,884
  • 7
  • 41
  • 50
  • 1
    *Cleaner* as in less characters? – Divakar Oct 16 '17 at 06:36
  • no, I mean like built-in mechanism. For example when we use `:` without numbers around, it can select the whole dimension. I couldn't make it work here. Is there other operator for this purpose? – Thoran Oct 16 '17 at 06:40
  • Nope, there isn't any. – Divakar Oct 16 '17 at 06:41
  • 1
    This is the normal way of selecting these 3 items ( (0,0),(1,0) and (2,1) ). It's pure simple `advanced` indexing, with 2 matching lists. – hpaulj Oct 16 '17 at 06:50

1 Answers1

0

Doing this specific thing can be done more cleanly, as you're just constructing a one-hot array, and there are good answers for "clean" ways to do this in numpy. I recommend this one by @MartinThoma:

a = np.eye(2)[[0,0,1]]

Also, most machine learning packages have their own one-hot encoding method that will be even more efficient and "clean" than this one, as machine learning is the most common use of one-hot encoding.

However, in general eliminating the range coordinate when doing this sort of fancy indexing is not possible. There really isn't a clear, explicit way to represent that operation that wouldn't be confusing when you want to extend the representation beyond 2 dimensions. And working in more than 2 dimensions at a time is the whole point of numpy

Daniel F
  • 13,620
  • 2
  • 29
  • 55