[Python] Numpy - create ordinal categories embedding

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I've written the following code to one-hot encode a list of ints:

    import numpy as np
    a = np.array([1,2,3,4])

    targets = np.zeros((a.size, a.max()))
    targets[np.arange(a.size),a-1] = 1
    targets


    Output:

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


    I would like to change the code, to better fit my ordinal class problem, so that the output would be:

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


    How can I achieve this?

    Continue reading...

Compartilhe esta Página