In [1]:
import numpy as np
s = 'abcdabc'
mapping = {}
mapped_s = np.array([mapping.setdefault(c, len(mapping)) for c in s])
print(mapping)
print(mapped_s)
Build dense one-hot matrix:
In [2]:
x = np.zeros((len(s), len(mapping)))
x[np.arange(x.shape[0]), mapped_s] = 1
print(x)