コード例 #1
0
def test_shape_transformer_2d_2d_array(axis):
    st = ShapeTransformerTo2D(axis=axis)
    x = numpy.arange(6).reshape((2, 3))
    xt = st.transform(x)
    xit = st.inverse_transform(xt)
    if axis == 0:
        assert numpy.allclose(xt.array, numpy.array([[0, 3], [1, 4], [2, 5]]))
    elif axis == 1 or axis == -1:
        assert numpy.allclose(x, xt.array)

    assert numpy.allclose(x, xit.array)
コード例 #2
0
 def transform(self, x, axis=1):
     is_array = not isinstance(x, Variable)
     if self.mean is None:
         raise AttributeError('[Error] mean is None, call fit beforehand!')
     x = format_x(x)
     shape_transformer = ShapeTransformerTo2D(axis=axis)
     x = shape_transformer.transform(x)
     mean_all, std_all = self._compute_mean_std_all(x.shape[1])
     x = (x - mean_all[None, :]) / std_all[None, :]
     x = shape_transformer.inverse_transform(x)
     if is_array:
         x = x.array
     return x
コード例 #3
0
def test_shape_transformer_2d_3d_array(axis):
    st = ShapeTransformerTo2D(axis=axis)
    x = numpy.arange(12).reshape((2, 3, 2))
    xt = st.transform(x)
    xit = st.inverse_transform(xt)
    if axis == 0:
        assert numpy.allclose(
            xt.array,
            numpy.array([[0, 6], [1, 7], [2, 8], [3, 9], [4, 10], [5, 11]]))
    elif axis == 1:
        assert numpy.allclose(
            xt.array,
            numpy.array([[0, 2, 4], [1, 3, 5], [6, 8, 10], [7, 9, 11]]))
    elif axis == 2 or axis == -1:
        assert numpy.allclose(xt.array, x.reshape(6, 2))
    assert numpy.allclose(x, xit.array)
コード例 #4
0
    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
コード例 #5
0
    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
コード例 #6
0
def test_shape_transformer_2d_error():
    st = ShapeTransformerTo2D(axis=1)
    x = numpy.arange(6).reshape(2, 3)
    with pytest.raises(AttributeError):
        # call `inverse_transform` before `transform`
        xt = st.inverse_transform(x)