From dd39433ff8ddea5445e3b1ca27db2474c002885d Mon Sep 17 00:00:00 2001 From: gaxler Date: Sat, 30 Jan 2021 18:48:23 -0800 Subject: [PATCH] Categorizable trait defines logic of turning floats into hashable categorical variables. Since we only support RealNumbers for now, the idea is to treat round numbers as ordinal (or nominal if user chooses to ignore order) categories. --- src/preprocessing/data_traits.rs | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/preprocessing/data_traits.rs diff --git a/src/preprocessing/data_traits.rs b/src/preprocessing/data_traits.rs new file mode 100644 index 0000000..04b534e --- /dev/null +++ b/src/preprocessing/data_traits.rs @@ -0,0 +1,43 @@ +//! Traits to indicate that float variables can be viewed as categorical +//! This module assumes + +pub type CategoricalFloat = u16; + +// pub struct CategoricalFloat(u16); + +pub trait Categorizable { + type A; + + fn to_category(self) -> CategoricalFloat; + + fn is_valid(self) -> bool; + +} + +impl Categorizable for f32 { + + type A = CategoricalFloat; + + fn to_category(self) -> CategoricalFloat { + self as CategoricalFloat + } + + fn is_valid(self) -> bool { + let a = self.to_category(); + a as f32 == self + } +} + +impl Categorizable for f64 { + + type A = CategoricalFloat; + + fn to_category(self) ->CategoricalFloat { + self as CategoricalFloat + } + + fn is_valid(self) -> bool { + let a = self.to_category(); + a as f64 == self + } +} \ No newline at end of file