如何将一维数组转换为逻辑矩阵[复制]

时间:2022-06-24 21:26:47

This question already has an answer here:

这个问题已经有了答案:

Is there any bulid-in function in python/numpy to convert an array = [1, 3, 1, 2] to something like this:

python/numpy中是否有插入函数将数组=[1,3,1,1,1,2]转换为如下形式:

array = [[0, 1, 0, 0], 
        [0, 0, 0, 1], 
        [0, 1, 0, 0], 
        [0, 0, 1, 0]]

2 个解决方案

#1


12  

You can create an identity matrix and then use the indices to create a new re-ordered matrix:

您可以创建一个单位矩阵,然后使用索引创建一个新的重新排序的矩阵:

>>> a = np.eye(4)
[Out]: array([[1., 0., 0., 0.],
              [0., 1., 0., 0.],
              [0., 0., 1., 0.],
              [0., 0., 0., 1.]])

>>> indices = [1, 3, 1, 2]
>>> a[indices]
[Out]: array([[0., 1., 0., 0.],
              [0., 0., 0., 1.],
              [0., 1., 0., 0.],
              [0., 0., 1., 0.]])

#2


3  

Probably the fastest would be to allocate zeros and then set the ones:

可能最快的方法是分配0,然后设置1:

>>> def f_preall(a):
...    m, n = a.size, a.max()+1
...    out = np.zeros((m, n))
...    out[np.arange(m), a] = 1
...    return out
... 
>>> from timeit import timeit
>>>
>>> a = np.random.randint(0, 10, (10,))
>>> timeit(lambda: f_preall(a), number=10000)
0.04905537283048034
>>> timeit(lambda: np.eye(a.max()+1)[a], number=10000)
0.09359440207481384
>>>
>>> a = np.random.randint(0, 100, (100,))
>>> timeit(lambda: f_preall(a), number=10000)
0.10055362526327372
>>> timeit(lambda: np.eye(a.max()+1)[a], number=10000)
0.16951417503878474

#1


12  

You can create an identity matrix and then use the indices to create a new re-ordered matrix:

您可以创建一个单位矩阵,然后使用索引创建一个新的重新排序的矩阵:

>>> a = np.eye(4)
[Out]: array([[1., 0., 0., 0.],
              [0., 1., 0., 0.],
              [0., 0., 1., 0.],
              [0., 0., 0., 1.]])

>>> indices = [1, 3, 1, 2]
>>> a[indices]
[Out]: array([[0., 1., 0., 0.],
              [0., 0., 0., 1.],
              [0., 1., 0., 0.],
              [0., 0., 1., 0.]])

#2


3  

Probably the fastest would be to allocate zeros and then set the ones:

可能最快的方法是分配0,然后设置1:

>>> def f_preall(a):
...    m, n = a.size, a.max()+1
...    out = np.zeros((m, n))
...    out[np.arange(m), a] = 1
...    return out
... 
>>> from timeit import timeit
>>>
>>> a = np.random.randint(0, 10, (10,))
>>> timeit(lambda: f_preall(a), number=10000)
0.04905537283048034
>>> timeit(lambda: np.eye(a.max()+1)[a], number=10000)
0.09359440207481384
>>>
>>> a = np.random.randint(0, 100, (100,))
>>> timeit(lambda: f_preall(a), number=10000)
0.10055362526327372
>>> timeit(lambda: np.eye(a.max()+1)[a], number=10000)
0.16951417503878474