Encoding
CPPLS.onehot — Method
onehot(labels::AbstractVector)Encode arbitrary labels such as strings, integers, or symbols into a one-hot matrix. The function removes duplicate labels, sorts the unique labels using Julia's default sort order for their type, and uses that sorted sequence as the column order of the encoded matrix. The result is a tuple containing the encoded matrix and the ordered labels, so predictions can be mapped back to the original domain.
See also nestedcv, nestedcvperm, sampleclasses
Examples
julia> matrix, classes = onehot(["dog", "cat", "cat"])
([0 1; 1 0; 1 0], ["cat", "dog"])CPPLS.onehot — Method
onehot(label_indices::AbstractVector{<:Integer}, n_labels::Integer)Convert 1-based integer label indices to a dense one-hot encoded matrix with n_labels columns. Each entry in label_indices is interpreted as the target column of the 1 in that sample's one-hot row, so the integer values are used directly as class indices rather than being renumbered from the observed data. The argument n_labels specifies the full size of the known class space and therefore the number of output columns, even if some classes do not appear in label_indices. This method returns only the encoded matrix.
See also sampleclasses
Examples
julia> onehot([4, 3, 2], 5)
3×5 Matrix{Int64}:
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0CPPLS.sampleclasses — Method
sampleclasses(one_hot_matrix::AbstractMatrix{<:Integer})Decode one-hot rows back into label indices by returning the column index selected in each row. An ArgumentError is thrown if one_hot_matrix contains values other than 0 and 1, or if any row contains either no 1 entry or more than one 1.
See also invfreqweights, onehot, nestedcv, nestedcvperm
Examples
julia> sampleclasses([1 0 0; 0 1 0; 0 0 1])
3-element Vector{Int64}:
1
2
3