1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use AHashMap;
use ;
/// Converts sparse categorical labels to categorical (one-hot encoded) format
///
/// This function takes a 1D array of integer labels and converts them to a 2D
/// one-hot encoded matrix where each row represents a sample and each column
/// represents a class. The value is 1.0 for the corresponding class and 0.0
/// for all other classes.
///
/// # Parameters
///
/// - `labels` - A 1D array of integer labels (e.g., \[0, 1, 2, 1, 0\])
/// - `num_classes` - Optional number of classes. If None, it will be inferred from the maximum label value + 1
///
/// # Returns
///
/// - `Array2<f64>` - A 2D one-hot encoded matrix of shape (n_samples, n_classes)
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::utility::label_encoding::to_categorical;
///
/// let labels = array![0, 1, 2, 1, 0];
/// let categorical = to_categorical(&labels, None);
/// // Result: [[1.0, 0.0, 0.0],
/// // [0.0, 1.0, 0.0],
/// // [0.0, 0.0, 1.0],
/// // [0.0, 1.0, 0.0],
/// // [1.0, 0.0, 0.0]]
/// ```
///
/// # Panics
///
/// Panics if any label is negative or if the specified num_classes is smaller
/// than the maximum label + 1.
/// Converts sparse categorical labels to categorical format with custom mapping
///
/// This function is useful when you have non-consecutive integer labels or
/// string labels that need to be mapped to consecutive integers first.
///
/// # Parameters
///
/// - `labels` - A slice of labels that can be compared and hashed
/// - `num_classes` - Optional number of classes. If None, it will be inferred from the number of unique labels
///
/// # Returns
///
/// - `(Array2<f64>, AHashMap<T, usize>)` - A tuple containing the one-hot encoded matrix and the mapping from original labels to class indices
///
/// # Examples
///
/// ```rust
/// use rustyml::utility::label_encoding::to_categorical_with_mapping;
///
/// let labels = vec!["cat", "dog", "bird", "dog", "cat"];
/// let (categorical, mapping) = to_categorical_with_mapping(&labels, None);
/// // categorical: one-hot encoded matrix
/// // mapping: {"cat": 0, "dog": 1, "bird": 2} (order may vary)
/// ```
/// Converts categorical (one-hot encoded) format to sparse categorical labels
///
/// This function performs the inverse operation of `to_categorical`, converting
/// a one-hot encoded matrix back to integer labels (sparse categorical format).
///
/// # Parameters
///
/// - `categorical` - A 2D one-hot encoded matrix where each row represents a sample
///
/// # Returns
///
/// - `Array1<i32>` - A 1D array of integer labels in sparse categorical format
///
/// # Examples
///
/// ```rust
/// use ndarray::array;
/// use rustyml::utility::label_encoding::to_sparse_categorical;
///
/// let categorical = array![[1.0, 0.0, 0.0],
/// [0.0, 1.0, 0.0],
/// [0.0, 0.0, 1.0]];
/// let sparse_labels = to_sparse_categorical(&categorical);
/// // Result: [0, 1, 2]
/// ```
///
/// # Note
///
/// This function finds the class with the highest probability for each sample,
/// making it suitable for converting model predictions back to class labels.