TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

One-Hot Encoding (Multi-class)

Feature EngineeringData Processing
Medium

Convert NNN integer class labels into an N×KN\times KN×K one-hot matrix. Row iii is defined by:

Yij={1,j=yi0,j≠yiY_{ij} = \begin{cases}1, & j=y_i \\ 0, & j\ne y_i\end{cases}Yij​={1,0,​j=yi​j=yi​​

Here, yiy_iyi​ is the label for sample iii and KKK is num_classes. When num_classes is None, set K=max⁡(y)+1K=\max(y)+1K=max(y)+1. Return a floating-point NumPy array.

Loading visualization...

Examples

Input: y = [0, 2, 1], num_classes = None

Output: [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]

Explanation: The largest label implies three columns, and each row marks its label index.

Input: y = [1, 1, 0], num_classes = 4

Output: [[0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0]]

Hint 1

Create np.zeros((y.size, num_classes), dtype=float).

Hint 2

Assign ones with encoded[np.arange(y.size), y] = 1.0.

Requirements

  • Infer the class count when num_classes is None
  • Assign one active entry per row with vectorized indexing
  • Return a floating-point NumPy array of shape (N,K)

Constraints

  • y is a nonempty one-dimensional list of nonnegative integer labels
  • num_classes is None or greater than the largest label
  • Use NumPy only
Try Similar Problems
Minmax NormalizationStreaming MinmaxOrdinal EncodingTarget EncodingFrequency Encoding

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: any

You must run your code first.
PrevNext

One-Hot Encoding (Multi-class)

Feature EngineeringData Processing
Medium

Convert NNN integer class labels into an N×KN\times KN×K one-hot matrix. Row iii is defined by:

Yij={1,j=yi0,j≠yiY_{ij} = \begin{cases}1, & j=y_i \\ 0, & j\ne y_i\end{cases}Yij​={1,0,​j=yi​j=yi​​

Here, yiy_iyi​ is the label for sample iii and KKK is num_classes. When num_classes is None, set K=max⁡(y)+1K=\max(y)+1K=max(y)+1. Return a floating-point NumPy array.

Loading visualization...

Examples

Input: y = [0, 2, 1], num_classes = None

Output: [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0]]

Explanation: The largest label implies three columns, and each row marks its label index.

Input: y = [1, 1, 0], num_classes = 4

Output: [[0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0]]

Hint 1

Create np.zeros((y.size, num_classes), dtype=float).

Hint 2

Assign ones with encoded[np.arange(y.size), y] = 1.0.

Requirements

  • Infer the class count when num_classes is None
  • Assign one active entry per row with vectorized indexing
  • Return a floating-point NumPy array of shape (N,K)

Constraints

  • y is a nonempty one-dimensional list of nonnegative integer labels
  • num_classes is None or greater than the largest label
  • Use NumPy only
Try Similar Problems
Minmax NormalizationStreaming MinmaxOrdinal EncodingTarget EncodingFrequency Encoding

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: any

You must run your code first.