def fit(self, x, indices=None, axis=1):
        """Fitting parameter.

        Args:
            x (numpy.ndarray or cupy.ndarray or Variable):
            indices (list or tuple or None):
                indices for applying standard scaling.
            axis (int): axis to calculate mean & std.

        Returns:
            self (StandardScaler): this instance.
        """
        x = to_array(x)
        x = format_x(x)
        x = ShapeTransformerTo2D(axis=axis).transform(x).array

        if indices is None:
            pass
        elif isinstance(indices, (list, tuple)):
            indices = numpy.asarray(indices)
        self.indices = indices
        if self.indices is not None:
            x = x[:, self.indices]

        xp = self.xp
        if xp is numpy:
            x = cuda.to_cpu(x)
            self.mean = xp.nanmean(x, axis=0)
            self.std = xp.nanstd(x, axis=0)
        else:
            x = cuda.to_gpu(x)
            if int(xp.sum(xp.isnan(x))) > 0:
                raise NotImplementedError(
                    "StandardScaling with nan value on GPU is not supported.")
            # cupy.nanmean, cupy.nanstd is not implemented yet.
            self.mean = xp.mean(x, axis=0)
            self.std = xp.std(x, axis=0)

        # result consistency check
        if xp.sum(self.std == 0) > 0:
            logger = getLogger(__name__)
            ind = numpy.argwhere(cuda.to_cpu(self.std) == 0)[:, 0]
            logger.warning('fit: std was 0 at indices {}'.format(ind))
        return self
Esempio n. 2
0
    def fit(self, x, indices=None):
        """Fitting parameter.

        Args:
            x:
            indices (list or tuple or None):
                indices for applying standard scaling.

        Returns:
            self (StandardScaler): this instance.
        """
        x = to_array(x)
        x = format_x(x)

        if indices is None:
            pass
        elif isinstance(indices, (list, tuple)):
            indices = numpy.asarray(indices)
        self.indices = indices
        if self.indices is not None:
            x = x[:, self.indices]

        xp = self.xp
        if xp is numpy:
            self.mean = xp.nanmean(x, axis=0)
            self.std = xp.nanstd(x, axis=0)
        else:
            if int(xp.sum(xp.isnan(x))) > 0:
                raise NotImplementedError(
                    "StandardScaling with nan value on GPU is not supported.")
            # cupy.nanmean, cupy.nanstd is not implemented yet.
            self.mean = xp.mean(x, axis=0)
            self.std = xp.std(x, axis=0)

        # result consistency check
        if xp.sum(self.std == 0) > 0:
            logger = getLogger(__name__)
            ind = numpy.argwhere(cuda.to_cpu(self.std) == 0)[:, 0]
            logger.warning('fit: std was 0 at indices {}'.format(ind))
        return self
    def fit(self, x, indices=None, axis=1):
        """Fitting parameter.

        Args:
            x (numpy.ndarray or cupy.ndarray or Variable):
            indices (list or tuple or None):
                indices for applying standard scaling.
            axis (int): axis to calculate min & max.

        Returns:
            self (MinMaxScaler): this instance.
        """
        x = to_array(x)
        x = format_x(x)
        x = ShapeTransformerTo2D(axis=axis).transform(x).array

        if indices is None:
            pass
        elif isinstance(indices, (list, tuple)):
            indices = numpy.asarray(indices)
        self.indices = indices
        if self.indices is not None:
            x = x[:, self.indices]

        xp = self.xp
        if xp is numpy:
            x = cuda.to_cpu(x)
        else:
            x = cuda.to_gpu(x)
        self.min = xp.nanmin(x, axis=0)
        self.max = xp.nanmax(x, axis=0)

        # result consistency check
        if xp.sum(self.max - self.min == 0) > 0:
            logger = getLogger(__name__)
            ind = numpy.argwhere(cuda.to_cpu(self.max-self.min) == 0)[:, 0]
            logger.warning('fit: max-min was 0 at indices {}'.format(ind))
        return self