可以指定max,min大小的 归一化处理MinMaxScaler

class MinMaxScaler:
    def __init__(self, feature_range=(0, 1),max_val =None,min_val = None):
        self.feature_range = feature_range
        self.data_min_ = min_val
        self.data_max_ = max_val


    def fit(self, X):
        X = np.asarray(X)
        if self.data_min_ is None:
        	self.data_min_ = X.min(axis=0)
        if self.data_max_ is None:
        	self.data_max_ = X.max(axis=0)
        return self

    def transform(self, X):
        X = np.asarray(X)
        X_scaled = (X - self.data_min_) / (self.data_max_ - self.data_min_)
        scale = self.feature_range[1] - self.feature_range[0]
        X_scaled = X_scaled * scale + self.feature_range[0]
        return X_scaled

    def inverse_transform(self, X_scaled):
        X_scaled = np.asarray(X_scaled)
        scale = self.feature_range[1] - self.feature_range[0]
        X = (X_scaled - self.feature_range[0]) / scale  # 反向缩放
        X = X * (self.data_max_ - self.data_min_) + self.data_min_  # 反向转换到原始数据
        return X

    def fit_transform(self, X, y=None):
        return self.fit(X).transform(X)
posted @ 2024-09-17 22:13  华小电  阅读(10)  评论(0编辑  收藏  举报