1

I currently have a numpy array with the following data:

array([0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
   1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0,
   0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0,
   1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1,
   0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1,
   0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])

I need to transform it into an array that looks as follows:

array([0., 1.],
      [0., 1.],
      [1., 0.],
      [1., 0.],
      [1., 0.],
      [0., 1.],
      [1., 0.],
      [1., 0.],
      [1., 0.],
      [1., 0.],
      ...
      [0., 1.])

I recognize the first step as obviously being a transform into a float, but I don't know where to start as far as the next step. Is there a map function that will work well? A lambda? A for loop? I feel like it would be simple if I already had it in a different array format, but I don't know how to conditionally add a dimension and then populate it with precisely the binary opposite of what is already there. Is all that based on an if-then conditional? Sorry, I'm just relatively new to Python so I don't know all the tools yet.

1
  • 1
    Looks like you want to reshape with fortran style ordering. Try a.reshape(-1, 2, order='F') Commented May 30, 2019 at 21:50

1 Answer 1

2

You can use advanced indexing into an appropriate template:

small_example = np.array([1,0,0,1,1,1,0,1])
(1-np.eye(2))[small_example]
# array([[1., 0.],
#        [0., 1.],
#        [0., 1.],
#        [1., 0.],
#        [1., 0.],
#        [1., 0.],
#        [0., 1.],
#        [1., 0.]])

The same idea can also be implemented using np.where:

np.where(small_example[:,None], *np.eye(2))
# array([[1., 0.],
#        [0., 1.],
#        [0., 1.],
#        [1., 0.],
#        [1., 0.],
#        [1., 0.],
#        [0., 1.],
#        [1., 0.]])

A more direct approach showing how to create column an row vectors, how to broadcast them together and how to cast the dtype:

(np.c_[small_example]^np.r_[:2]).astype(float)
# array([[1., 0.],
#        [0., 1.],
#        [0., 1.],
#        [1., 0.],
#        [1., 0.],
#        [1., 0.],
#        [0., 1.],
#        [1., 0.]])

We can also column stack the input and its "negative"; we use 1.0 to trigger type promotion:

np.c_[small_example,1.0-small_example]
# array([[1., 0.],
#        [0., 1.],
#        [0., 1.],
#        [1., 0.],
#        [1., 0.],
#        [1., 0.],
#        [0., 1.],
#        [1., 0.]])
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant use of np.eye(). I'm going to have to unpack the rest of the solutions as well, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.