python - Binarize an Array -


i have column vector signifies day of week

[1,2,2,3,4] 

i need binarise vector in sense every item in original vector must transformed vector number indicates index needs 1 , rest must 0.

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

do composing binary list zeroes except in given position in list comprehension gives nice one-liner:

w=[1,2,2,3,4] m = [[0]*(pos)+[1]+[0]*(9-pos-1) pos in w] 

result:

m = [[0, 1, 0, 0, 0, 0, 0, 0, 0],      [0, 0, 1, 0, 0, 0, 0, 0, 0],      [0, 0, 1, 0, 0, 0, 0, 0, 0],      [0, 0, 0, 1, 0, 0, 0, 0, 0],      [0, 0, 0, 0, 1, 0, 0, 0, 0]] 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -