1use ndarray::{Array2, ArrayView2};
25use solow_core::{Error, Result};
26
27fn monomials(d: usize, degree: usize, include_bias: bool, interaction_only: bool) -> Vec<Vec<u32>> {
33 let mut out: Vec<Vec<u32>> = Vec::new();
34 for total in 0..=degree {
35 if total == 0 {
36 if include_bias {
37 out.push(vec![0u32; d]);
38 }
39 continue;
40 }
41 let mut current = vec![0u32; d];
43 enumerate(d, total as u32, 0, &mut current, &mut out, interaction_only);
44 }
45 out
46}
47
48fn enumerate(
49 d: usize,
50 remaining: u32,
51 idx: usize,
52 current: &mut Vec<u32>,
53 out: &mut Vec<Vec<u32>>,
54 interaction_only: bool,
55) {
56 if idx == d - 1 {
57 current[idx] = remaining;
58 if !interaction_only || current.iter().all(|&e| e <= 1) {
59 out.push(current.clone());
60 }
61 current[idx] = 0;
62 return;
63 }
64 let cap = if interaction_only { 1 } else { remaining };
65 for k in (0..=cap.min(remaining)).rev() {
69 current[idx] = k;
70 enumerate(d, remaining - k, idx + 1, current, out, interaction_only);
71 }
72 current[idx] = 0;
73}
74
75#[derive(Clone, Debug, PartialEq)]
80pub struct PolynomialFeatures {
81 pub degree: usize,
83 pub interaction_only: bool,
86 pub include_bias: bool,
88 exponents: Option<Vec<Vec<u32>>>,
90}
91
92impl PolynomialFeatures {
93 pub fn new(degree: usize) -> Self {
96 Self {
97 degree,
98 interaction_only: false,
99 include_bias: true,
100 exponents: None,
101 }
102 }
103
104 pub fn interaction_only(mut self, flag: bool) -> Self {
106 self.interaction_only = flag;
107 self
108 }
109
110 pub fn include_bias(mut self, flag: bool) -> Self {
112 self.include_bias = flag;
113 self
114 }
115
116 pub fn fit(&mut self, x: ArrayView2<'_, f64>) -> Result<()> {
118 if x.ncols() == 0 {
119 return Err(Error::Value(
120 "PolynomialFeatures::fit: x must have at least one column".into(),
121 ));
122 }
123 self.exponents = Some(monomials(
124 x.ncols(),
125 self.degree,
126 self.include_bias,
127 self.interaction_only,
128 ));
129 Ok(())
130 }
131
132 pub fn output_columns(&self, d: usize) -> usize {
134 monomials(d, self.degree, self.include_bias, self.interaction_only).len()
135 }
136
137 pub fn transform(&self, x: ArrayView2<'_, f64>) -> Result<Array2<f64>> {
139 let d = x.ncols();
140 let exps = monomials(d, self.degree, self.include_bias, self.interaction_only);
141 let m = exps.len();
142 let mut out = Array2::<f64>::zeros((x.nrows(), m));
143 for i in 0..x.nrows() {
144 for (k, exp) in exps.iter().enumerate() {
145 let mut acc = 1.0_f64;
146 for j in 0..d {
147 let e = exp[j];
148 if e == 0 {
149 continue;
150 }
151 let v = x[[i, j]];
152 let mut p = 1.0;
154 for _ in 0..e {
155 p *= v;
156 }
157 acc *= p;
158 }
159 out[[i, k]] = acc;
160 }
161 }
162 Ok(out)
163 }
164
165 pub fn fit_transform(&self, x: ArrayView2<'_, f64>) -> Result<Array2<f64>> {
167 let mut clone = self.clone();
170 clone.fit(x)?;
171 clone.transform(x)
172 }
173
174 pub fn exponents(&self) -> Option<&[Vec<u32>]> {
179 self.exponents.as_deref()
180 }
181
182 pub fn exponents_for(&self, d: usize) -> Vec<Vec<u32>> {
185 monomials(d, self.degree, self.include_bias, self.interaction_only)
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192 use approx::assert_abs_diff_eq;
193 use ndarray::array;
194
195 #[test]
196 fn degree_2_matches_hand_derivation() {
197 let x = array![[2.0, 3.0]];
198 let poly = PolynomialFeatures::new(2).include_bias(true);
199 let out = poly.fit_transform(x.view()).unwrap();
200 assert_eq!(out.dim(), (1, 6));
202 assert_abs_diff_eq!(out[[0, 0]], 1.0, epsilon = 1e-12);
203 assert_abs_diff_eq!(out[[0, 1]], 2.0, epsilon = 1e-12);
204 assert_abs_diff_eq!(out[[0, 2]], 3.0, epsilon = 1e-12);
205 assert_abs_diff_eq!(out[[0, 3]], 4.0, epsilon = 1e-12);
206 assert_abs_diff_eq!(out[[0, 4]], 6.0, epsilon = 1e-12);
207 assert_abs_diff_eq!(out[[0, 5]], 9.0, epsilon = 1e-12);
208 }
209
210 #[test]
211 fn interaction_only_drops_pure_powers() {
212 let x = array![[2.0, 3.0, 5.0]];
213 let poly = PolynomialFeatures::new(2)
214 .include_bias(false)
215 .interaction_only(true);
216 let out = poly.fit_transform(x.view()).unwrap();
217 assert_eq!(out.dim(), (1, 6));
219 assert_abs_diff_eq!(out[[0, 3]], 6.0, epsilon = 1e-12); assert_abs_diff_eq!(out[[0, 4]], 10.0, epsilon = 1e-12); assert_abs_diff_eq!(out[[0, 5]], 15.0, epsilon = 1e-12); }
223
224 #[test]
225 fn column_count_matches_binomial_identity() {
226 for d in 1..6 {
227 for degree in 0..5 {
228 let full = PolynomialFeatures::new(degree)
229 .include_bias(true)
230 .output_columns(d);
231 let expected: usize = (1..=degree).fold(1usize, |acc, k| acc * (d + k) / k);
233 assert_eq!(full, expected, "d = {d}, p = {degree}");
234 }
235 }
236 }
237}