Custom version of MinMaxScaler #19501
-
I am looking to modify the functionality of I have created a custom scaler and have put it's code below. It seems to work for me, but I am not sure if I've done everything correctly. In my code class CustomMinMaxScaler(sklearn.preprocessing.MinMaxScaler):
def _handle_zeros_in_scale(scale, copy=True):
# if we are fitting on 1D arrays, scale might be a scalar
if np.isscalar(scale):
if scale == .0:
scale = 1.
return scale
elif isinstance(scale, np.ndarray):
if copy:
# New array to avoid side-effects
scale = scale.copy()
scale[scale == 0.0] = 1.0
return scale
def __init__(self, common_feats=None, **kwargs):
super().__init__(**kwargs)
self.common_feats = common_feats
def partial_fit(self, X, y=None):
super().partial_fit(X, y)
if self.common_feats:
for group in self.common_feats:
# calculate min and max across all features within a group, instead of calculating for individual features
group = np.array(group)
self.data_min_[group] = np.min(self.data_min_[group])
self.data_max_[group] = np.max(self.data_max_[group])
self.data_range_[group] = self.data_max_[group] - self.data_min_[group]
self.scale_[group] = ((self.feature_range[1] - self.feature_range[0]) / CustomMinMaxScaler._handle_zeros_in_scale(self.data_range_[group]))
self.min_[group] = self.feature_range[0] - self.data_min_[group] * self.scale_[group]
return self Could someone please let me know if this is correct? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Write a couple of unit tests to ensure that you have the expected behaviour. |
Beta Was this translation helpful? Give feedback.
-
I think this would be an interesting option to add to the Feel free to open a pull request if you are interested, modifying the existing |
Beta Was this translation helpful? Give feedback.
Write a couple of unit tests to ensure that you have the expected behaviour.