Skip to content

Instantly share code, notes, and snippets.

@shamilnabiyev
Last active August 15, 2022 08:18
Show Gist options
  • Save shamilnabiyev/72507869b000f9be9aa50b78a4f068e9 to your computer and use it in GitHub Desktop.
Save shamilnabiyev/72507869b000f9be9aa50b78a4f068e9 to your computer and use it in GitHub Desktop.
OneHotEncoder
import numpy as np
from sklearn.preprocessing import OneHotEncoder

# Load the npz file
data = np.load('data.npz')
X, y = data["x"], data["y"]

print(y.shape)
# Output: (1519,)

print(y[0:5])
# Output
# array([1, 1, 0, 0, 1])

print(np.unique(y))
# Output
# array([0, 1])

# Encode the target variable
enc = OneHotEncoder()
y_binary = enc.fit_transform(y.reshape(-1, 1))
y_binary = y_binary.toarray()

print(y_binary.shape)
# Output: (1519, 2)

print(y[0:5])
# Output
# array([[0., 1.],
#        [0., 1.],
#        [1., 0.],
#        [1., 0.],
#        [0., 1.]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment