#Implement one hot encoding of words or characters.
# Define a simple text
text = "hello"
# Get unique characters
chars = sorted(set(text)) # Unique characters
char_index = {char: i for i, char in enumerate(chars)}
# Create one-hot encoding matrix
one_hot_matrix = np.zeros((len(text), len(chars)))
for i, char in enumerate(text):
one_hot_matrix[i, char_index[char]] = 1
print("Character Index:", char_index)
print("One-Hot Encoded Matrix:\n", one_hot_matrix)
import numpy as np
Character Index: {'e': 0, 'h': 1, 'l': 2, 'o': 3}
One-Hot Encoded Matrix:
[[0. 1. 0. 0.]
[1. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]