Skip to content

Commit

Permalink
Update to LinearScaler
Browse files Browse the repository at this point in the history
  • Loading branch information
cheziyi committed Aug 30, 2024
1 parent 7411a18 commit fad991a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
29 changes: 0 additions & 29 deletions src/synnax_shared/data_processing/scaler/PredefinedMinMaxScaler

This file was deleted.

56 changes: 56 additions & 0 deletions src/synnax_shared/data_processing/scaler/linear_scaler.py
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"])

0 comments on commit fad991a

Please sign in to comment.