예제 #1
0
 def test_trim_old(self):
     x = ma.arange(100)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x,tail='r').count(), 80)
     x[50:70] = masked
     trimx = mstats.trimboth(x)
     assert_equal(trimx.count(), 48)
     assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16)
     x._mask = nomask
     x.shape = (10,10)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x).count(), 80)
예제 #2
0
 def test_trim_old(self):
     x = ma.arange(100)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x,tail='r').count(), 80)
     x[50:70] = masked
     trimx = mstats.trimboth(x)
     assert_equal(trimx.count(), 48)
     assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16)
     x._mask = nomask
     x.shape = (10,10)
     assert_equal(mstats.trimboth(x).count(), 60)
     assert_equal(mstats.trimtail(x).count(), 80)
예제 #3
0
파일: math.py 프로젝트: cpcloud/span
    def trimmean(x, alpha, inclusive=(False, False), axis=None):
        """Compute the `alpha`-trimmed mean of an array `x`.

        Parameters
        ----------
        x : array_like
            The array on which to operate.

        alpha : float or int
            A number between 0 and 100, left inclusive indicating the
            percentage of values to cut from `x`.

        inclusive : tuple of bools, optional
            Whether to round (True, True) or truncate the values
            (False, False). Defaults to truncation. Note that this is different
            from ``scipy.stats.mstats.trimboth``'s default.

        axis : int or None, optional
            The axis over which to operate. None flattens the array

        Returns
        -------
        m : Series
            The `alpha`-trimmed mean of `x` along axis `axis`.
        """
        assert 0 <= alpha < 100, 'alpha must be in the interval [0, 100)'
        assert len(inclusive) == 2, 'inclusive must have only 2 elements'

        if isinstance(x, (numbers.Number)) or (hasattr(x, 'size') and
                                               x.size == 1):
            return float(x)

        assert axis is None or 0 <= axis < x.ndim, \
            'axis must be None or less than x.ndim: {0}'.format(x.ndim)

        trimmed = trimboth(x, alpha / 100.0, inclusive, axis).mean(axis)

        index = None
        if isinstance(x, DataFrame):
            index = {0: x.columns, 1: x.index, None: None}[axis]

        return Series(trimmed, index=index)
 def forward(self, bottom, top):
     """Forward: either using max pooling or average pooling
     """
     num_ = bottom[0].data.shape[0]
     if DEBUG:
         print("Number of samples input: {}".format(num_))
     bottom_data_ = bottom[0].data.reshape(num_, -1)
     # pooling mask is of the same size as input
     # then backward could be calculated as matrix multiplication
     self._pooling_mask = np.zeros(bottom[0].data.shape)
     # max pooling
     if self._pooling == 'MAX':
         # perform max pooling
         pooled_data_ = np.max(bottom_data_, axis=0)
         self._max_ids = np.argmax(bottom_data_, axis=0).reshape(
             bottom[0].shape[1:])
         # set the pooling_mask
         for i in range(len(self._max_ids)):
             self._pooling_mask[self._max_ids[i], i] = 1.0
     elif self._pooling == 'AVE':
         # perform average pooling
         trim_ = float(self._layer_params.get('trim', 0.0))
         if trim_ == 0.0:
             pooled_data_ = np.mean(bottom_data_, axis=0)
             self._pooling_mask = np.ones(self._pooling_mask.shape)
             self._pooling_mask *= (1.0 / float(num_))
         else:
             # trimmed mean, with percentage of trim_
             trimmed_data_ = trimboth(
                 bottom_data_, proportiontocut=trim_, axis=0)
             pooled_data_ = trimmed_data_.mean(axis=0)
             trimmed_count_ \
                 = trimmed_data_.mask.astype(np.float32)[:, 1].sum()
             self._pooling_mask \
                 = (1 - trimmed_data_.mask).astype(np.float32)
             if DEBUG:
                 print self._pooling_mask
             self._pooling_mask /= trimmed_count_
     elif self._pooling == 'GAUSSIAN':
         # Fit a gaussian distribution
         sigma_clip = 0.05
         bottom_mean_ = np.mean(bottom_data_, axis=0)
         bottom_var_ = np.cov(bottom_data_.transpose())
         self._gaussian_weights = multivariate_normal.pdf(
             bottom_data_, bottom_mean_, bottom_var_, allow_singular=True)
         if DEBUG:
             print("Before clipping: {}".format(
                 (self._gaussian_weights > 0).sum()))
             # print(self._gaussian_weights)
         # clip samples far away from the mean
         self._gaussian_weights /= self._gaussian_weights.max()
         if DEBUG:
             print self._gaussian_weights
         self._gaussian_weights[
             np.where(self._gaussian_weights < sigma_clip)] = 0
         if DEBUG:
             print('Number of bbox activated: {}'.format(
                 (self._gaussian_weights > 0).sum()))
             # print(self._gaussian_weights)
         self._gaussian_weights /= self._gaussian_weights.sum()
         self._pooling_mask = np.ones(self._pooling_mask.shape)
         pooled_data_ = bottom_data_ * self._gaussian_weights[:, np.newaxis]
         pooled_data_ = pooled_data_.sum(axis=0)
         for i in range(len(self._gaussian_weights)):
             self._pooling_mask[i, ...] *= self._gaussian_weights[i]
     else:
         print("WRONG POOLING TYPE")
         pooled_data_ = np.zeros((1, bottom_data_.shape[1]))
     if DEBUG:
         print('Pooling Mask: {}'.format(self._pooling_mask))
     top[0].data[0, ...] = pooled_data_.reshape(bottom[0].data.shape[1:])