0

I require the following logic:

  • The output array has the same amount of rows as the number of elements in the input array
  • The values of the input array denote the column numbers of the output array (the number of columns equals the maximum number in the input array)
  • The output array's values should be 1 if the input array contains the specific combination, 0 otherwise

For example, I'd like to transform a numpy array such as

[2, 1, 1, 0]

into an array that looks like this

[[0, 0, 1],
 [0, 1, 0],
 [0, 1, 0],
 [1, 0, 0]]

The only thing I can come up with is filling an empty array with a for-loop. Any more "numpy-ish" way to do this?

Nico
  • 70
  • 6
  • Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. In particular, describe the logic that produces the given output from the given input. – Prune Nov 22 '19 at 20:42
  • how do to choose the number of columns? what you saif is not clear – seralouk Nov 22 '19 at 21:04
  • @makis I edited to make it more clear – Nico Nov 22 '19 at 21:07
  • got it yes. see my answer – seralouk Nov 22 '19 at 21:13
  • What do you mean by "specific combination"? Your example just looks like a one-hot encoding. If so, then this answer works: https://stackoverflow.com/a/42874726/8793975 – enumaris Nov 22 '19 at 22:18
  • @enumaris Yea you are right, one-hot encoding works. I didn't know there is a name for this. Thanks all for the help. – Nico Nov 23 '19 at 10:04

1 Answers1

1

Use this:

before = [2, 1, 1, 0]

n_rows = len(before)
n_columns = max(before) + 1

output = np.zeros((n_rows, n_columns))

#loop constructor
for idx,i in enumerate(before): output[idx,i] = 1

array([[0., 0., 1.],
       [0., 1., 0.],
       [0., 1., 0.],
       [1., 0., 0.]])
seralouk
  • 30,938
  • 9
  • 118
  • 133