简体   繁体   中英

Convert indices to vectors in Numpy

Suppose that we have this data:

import numpy as np
data = np.array([1,0,1,2,1,2])

I want convert it to this:

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

How can I do this in Numpy (or other related packages)? We have ind2vec function in MATLAB for doing this.

A fairly common way to do this in NumPy is to compare data with arange and cast the boolean array to integer type:

>>> (np.arange(3) == data[:,None]).astype(int)
array([[0, 1, 0],
       [1, 0, 0],
       [0, 1, 0],
       [0, 0, 1],
       [0, 1, 0],
       [0, 0, 1]])

More generally, if you want to specify N columns (similar to Matlab's function) here's a function wrapping the necessary steps. You can pass the list of indices ind as a Python list or a NumPy array:

def ind2vec(ind, N=None):
    ind = np.asarray(ind)
    if N is None: 
        N = ind.max() + 1
    return (np.arange(N) == ind[:,None]).astype(int)

Then for example:

>>> ind2vec([4,0,2])
array([[0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0]])

>>> ind2vec([1,3,2,1,0], N=10)
array([[0, 1, 0, 0, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

One way would be:

import numpy as np
data = np.array([1,0,1,2,1,2])

idx = np.zeros((data.size, data.max()+1))
idx[np.arange(data.size), data] = 1
def ind_to_vec(data):
    maxd = max(data)
    mind = min(data)
    b = np.zeros([len(data),maxd-mind+1])
    for ii in range(mind,maxd+1):
        for jj in range(len(data)):
            if data[jj]==ii:
                b[jj,ii]=1
    return b

This steps through every possible value in the input array, and checks to see what value it is. It then populates an otherwise zeroed array with a 1 in the column that is for that value. Output for your input data is

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM