编程语言
首页 > 编程语言> > python通过fancy indexing把数组转换为one hot编码的numpy array

python通过fancy indexing把数组转换为one hot编码的numpy array

作者:互联网

背景

实现一维numpy数组

a = array([1,0,3])

转换为2维的 1-hot数组

b = array([[0,1,0,0], [1,0,0,0], [0,0,0,1]])

python实现示例代码

import numpy as np

if __name__ == '__main__':
    ind = np.array([1, 0, 3])
    x = np.zeros((ind.size, ind.max() + 1))
    x[np.arange(ind.size), ind] = 1
	print(x)

结果展示

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

fancy indexing介绍

fancy indexing:传递索引数组来一次返回多个数组元素。

索引为一维数组

import numpy as np

if __name__ == '__main__':
	x = np.array([51, 92, 14, 71, 60, 20, 82, 86, 74, 74])
    ind = [3, 4, 5]
    print(x[ind])

结果展示:

[71 60 20]

索引为二维数组

import numpy as np

if __name__ == '__main__':
    x = np.array([51, 92, 14, 71, 60, 20, 82, 86, 74, 74])
    ind = np.array([[3, 7],
                    [4, 5]])
    print(x[ind])

结果展示:

[[71 86]
 [60 20]]

多个维度

import numpy as np

if __name__ == '__main__':
    x = np.array([[0, 1, 2, 3],
                  [4, 5, 6, 7],
                  [8, 9, 10, 11]])

    row = np.array([0, 1, 2])  # 行
    col = np.array([2, 1, 3])  # 列
    print(x[row, col])

结果展示:

[ 2  5 11]

示例代码解释

上面示例代码相当于多个维度情况即:

import numpy as np

if __name__ == '__main__':
    # ind = np.array([1, 0, 3])
    # x = np.zeros((ind.size, ind.max() + 1))
    # x[np.arange(ind.size), ind] = 1

    x = np.array([[0, 0, 0, 0],
                  [0, 0, 0, 0],
                  [0, 0, 0, 0]])

    row = np.array([0, 1, 2])
    col = np.array([1, 0, 3])
    x[row, col] = 1  # 相当于找好位置后,赋值为1
    print(x)

结果

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

标签:__,python,indexing,fancy,np,ind,import,array,numpy
来源: https://blog.csdn.net/qq_33873431/article/details/106669274