Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Hot Encoding #217

Open
brccabral opened this issue Sep 19, 2024 · 1 comment
Open

One Hot Encoding #217

brccabral opened this issue Sep 19, 2024 · 1 comment

Comments

@brccabral
Copy link

I am trying to do "one hot encoding". If value is 4, only column 4 should be set to 1, other columns remain 0.

In python I can do this. Each value in y is evaluated in each row.

y = np.array([5, 4, 3, 0, 7, 6, 5, 1, 3, 5])
one_hot = np.zeros((10,10))
one_hot[np.arange(y.size), y] = 1
print(one_hot)

Prints

[[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]  # 5
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]  # 4
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]  # 3
 [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]  # 0
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]  # 7
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]  # 6
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]  # 5
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]  # 1
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]  # 3
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]] # 5

But in NumCpp all values in y are evaluated for all rows.

nc::NdArray<int> y = {5, 4, 3, 0, 7, 6, 5, 1, 3, 5};
auto one_hot = nc::zeros<int>(10,10);
one_hot.put(nc::arange(y.size()), y, 1);
one_hot.print();

Prints

[[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]
[1, 1, 0, 1, 1, 1, 1, 1, 0, 0, ]]
@dpilger26
Copy link
Owner

Hmm, yeah they are different behaviors. I'll have to add an additional put overload to accomplish this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants