Skip to content

Instantly share code, notes, and snippets.

@jgoodie
Created January 19, 2025 05:36
Show Gist options
  • Save jgoodie/a7b6a36e22a7e195fa742c4976a0d792 to your computer and use it in GitHub Desktop.
Save jgoodie/a7b6a36e22a7e195fa742c4976a0d792 to your computer and use it in GitHub Desktop.
sentence one-hot-encoding
import numpy as np
from sklearn.preprocessing import OneHotEncoder
sentence = "the otter swam across the river to the other bank"
d = dict.fromkeys(sentence.split())
vocab = list(d.keys())
tokens = sentence.lower().split()
encoder = OneHotEncoder(categories=[vocab], sparse_output=False)
X = encoder.fit_transform(np.array(tokens).reshape(-1, 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment