0

I am trying to convert a numpy array

 np.array([1,3,2]) 

to

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

Any idea of how to do this efficiently? Thanks!

0

4 Answers 4

1

Create an bool array, and then fill it:

import numpy as np
a = np.array([1, 2, 3, 0, 3, 2, 1])
b = np.zeros((len(a), a.max() + 1), bool)
b[np.arange(len(a)), a] = 1
Sign up to request clarification or add additional context in comments.

1 Comment

This should be the fastest for all but the smallest operands.
0

It is also possible to just select the right values from np.eye or the identity matrix:

a =  np.array([1,3,2]) 
b = np.eye(max(a))[a-1]

This would probably be the most straight forward.

Comments

0

You can compare to [1, 2, 3] like so:

>>> a = np.array([1,3,2]) 
>>> np.equal.outer(a, np.arange(1, 4)).view(np.int8)
array([[1, 0, 0],
       [0, 0, 1],
       [0, 1, 0]], dtype=int8)

or equivalent but slightly faster

>>> (a[:, None] == np.arange(1, 4)).view(np.int8)

Comments

0

Try pandas's get dummy method.

import pandas as pd
import numpy as np
arr = np.array([1 ,3, 2])
df = pd.get_dummies(arr)

if what you need is numpy array object, do:

arr2 = df.values

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.