-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
29 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
src/synnax_shared/data_processing/scaler/PredefinedMinMaxScaler
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import TypedDict | ||
from numpy import number | ||
from pandas import DataFrame, Series | ||
|
||
|
||
class LinearScalerDto(TypedDict): | ||
offset: float | ||
divisor: float | ||
|
||
|
||
class LinearScaler: | ||
|
||
def __init__(self, offset: float, divisor: float) -> None: | ||
""" | ||
Initializes the LinearScaler. | ||
E.g. To scale data from -100 to 100 to 0 to 1, the offset would be 100.0 and the divisor would be 200.0. | ||
Parameters: | ||
offset (float): | ||
The offset to be applied to the data | ||
divisor (float): | ||
The divisor to be applied to the data | ||
""" | ||
if divisor == 0: | ||
raise ValueError("Divisor cannot be 0") | ||
self.offset = offset | ||
self.divisor = divisor | ||
|
||
def transform_series(self, series: Series) -> Series: | ||
float_series = series.astype(float) | ||
return (float_series + self.offset) / self.divisor | ||
|
||
def inverse_transform_series(self, series: Series) -> Series: | ||
float_series = series.astype(float) | ||
return (float_series * self.divisor) - self.offset | ||
|
||
def transform_dataframe(self, dataframe: DataFrame) -> DataFrame: | ||
dataframe_copy = dataframe.copy() | ||
for column in dataframe_copy.select_dtypes(include=[number]): | ||
dataframe_copy[column] = self.transform_series(dataframe_copy[column]) | ||
return dataframe_copy | ||
|
||
def inverse_transform_dataframe(self, dataframe: DataFrame) -> DataFrame: | ||
dataframe_copy = dataframe.copy() | ||
for column in dataframe_copy.select_dtypes(include=[number]): | ||
dataframe_copy[column] = self.inverse_transform_series( | ||
dataframe_copy[column] | ||
) | ||
return dataframe_copy | ||
|
||
def toDto(self) -> LinearScalerDto: | ||
return {"offset": self.offset, "divisor": self.divisor} | ||
|
||
@staticmethod | ||
def fromDto(dto: LinearScalerDto): | ||
return LinearScaler(dto["offset"], dto["divisor"]) |