diff --git a/src/synnax_shared/data_processing/scaler/PredefinedMinMaxScaler b/src/synnax_shared/data_processing/scaler/PredefinedMinMaxScaler new file mode 100644 index 0000000..e9cdc92 --- /dev/null +++ b/src/synnax_shared/data_processing/scaler/PredefinedMinMaxScaler @@ -0,0 +1,29 @@ +import pandas as pd + +class PredefinedMinMaxScaler: + '''Scaler that scales data to the user defined range [min_val, max_val]''' + def __init__(self, min_val: float, max_val: float) -> None: + self.min_val = min_val + self.max_val = max_val + + def scale_column( + self, column: pd.Series, min_val: float, max_val: float + ) -> pd.Series: + return (column - min_val) / (max_val - min_val) + + def unscale_column( + self, column: pd.Series, min_val: float, max_val: float + ) -> pd.Series: + return column * (max_val - min_val) + min_val + + def transform(self, data: pd.DataFrame) -> pd.DataFrame: + data = data.copy() + for column in data.select_dtypes(include=[np.number]): + data[column] = self.scale_column(data[column], self.min_val, self.max_val) + return data + + def inverse_transform(self, data: pd.DataFrame) -> pd.DataFrame: + data = data.copy() + for column in data.select_dtypes(include=[np.number]): + data[column] = self.unscale_column(data[column], self.min_val, self.max_val) + return data \ No newline at end of file