Skip to content

Instantly share code, notes, and snippets.

View xychenunc's full-sized avatar

Xiaoyang Chen xychenunc

  • Chapel Hill, USA
View GitHub Profile
@qianyizhang
qianyizhang / one_hot.py
Last active June 6, 2018 22:48
numpy one_hot function
import numpy as np
def one_hot(nparray, depth = 0, on_value = 1, off_value = 0):
if depth == 0:
depth = np.max(nparray) + 1
assert np.max(nparray) < depth, "the max index of nparray: {} is larger than depth: {}".format(np.max(nparray), depth)
shape = nparray.shape
out = np.ones((*shape, depth)) * off_value
indices = []
for i in range(nparray.ndim):
tiles = [1] * nparray.ndim