-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatascaler.py
More file actions
61 lines (53 loc) · 2.08 KB
/
Copy pathdatascaler.py
File metadata and controls
61 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler, MaxAbsScaler, RobustScaler, QuantileTransformer
class DataScaler:
"""
A class to handle scaling and unscaling of data features.
Methods
-------
scale(data: pd.DataFrame, features: list, method: str = 'standard') -> None
Scales the specified features in the data using the specified method.
unscale(data: pd.DataFrame, features: list) -> None
Unscales the specified features in the data.
"""
def __init__(self):
self.scalers = {}
def scale(self, data: pd.DataFrame, features: list, method: str = 'standard') -> None:
"""
Scales the specified features in the data using the specified method.
Parameters
----------
data : pd.DataFrame
The data containing the features to scale.
features : list
A list of feature names to scale.
method : str, optional
The scaling method to use (default is 'standard').
Options are 'standard', 'minmax', 'maxabs', 'robust', or 'quantile'.
"""
scaler = None
if method == 'standard':
scaler = StandardScaler()
elif method == 'minmax':
scaler = MinMaxScaler()
elif method == 'maxabs':
scaler = MaxAbsScaler()
elif method == 'robust':
scaler = RobustScaler()
elif method == 'quantile':
scaler = QuantileTransformer(output_distribution='normal')
if scaler:
data[features] = scaler.fit_transform(data[features])
self.scalers[method] = scaler
def unscale(self, data: pd.DataFrame, features: list) -> None:
"""
Unscales the specified features in the data.
Parameters
----------
data : pd.DataFrame
The data containing the features to unscale.
features : list
A list of feature names to unscale.
"""
for method, scaler in self.scalers.items():
data[features] = scaler.inverse_transform(data[features])