numpy.count_nonzero

Here are the examples of the python api numpy.count_nonzero taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

85 Examples 7

Page 1 Selected Page 2

Example 1

Project: Machine_Learning Source File: decisionTree.py
Function: gini
    def Gini(self, Vec, Tag, val, feature):
        # c1 + c2 == D
        if self.Discrete[feature] == True:
            c1 = 1.0 * numpy.count_nonzero(Vec == val)
            c2 = 1.0 * numpy.count_nonzero(Vec != val)
            D =  1.0 * Tag.size

            return c1 * pGini(Tag[ Vec == val]) / D + \
                   c2 * pGini(Tag[ Vec != val]) / D
        else:
            c1 = 1.0 * numpy.count_nonzero(Vec >= val)
            c2 = 1.0 * numpy.count_nonzero(Vec <  val)
            D =  1.0 * Tag.size

            return c1 * pGini(Tag[ Vec >= val]) / D + \
                   c2 * pGini(Tag[ Vec <  val]) / D

Example 2

Project: color-extractor Source File: selector.py
Function: ratio
    def _ratio(self, k, labels, centers):
        counts = [np.count_nonzero(labels == l) for l in range(k)]
        counts = np.array(counts, np.uint32)
        total = np.sum(counts)
        sort_idx = np.argsort(counts)[::-1]
        cuem_counts = np.cuemsum(counts[sort_idx])

        threshold = self._settings['ratio.threshold']
        for idx_stop in range(k):
            if cuem_counts[idx_stop] >= threshold * total:
                break
        sort_centers = centers[sort_idx]
        return sort_centers[:idx_stop + 1]

Example 3

Project: datasketch Source File: b_bit_minhash.py
Function: jaccard
    def jaccard(self, other):
        '''
        Estimate the Jaccard similarity (resemblance) between this b-bit
        MinHash and the other.
        '''
        if self.b != other.b:
            raise ValueError("Cannot compare two b-bit MinHashes with different\
                    b values")
        if self.seed != other.seed:
            raise ValueError("Cannot compare two b-bit MinHashes with different\
                    set of permutations")
        intersection = np.count_nonzero(self.hashvalues==other.hashvalues)
        raw_est = float(intersection) / float(self.hashvalues.size)
        a1 = self._calc_a(self.r, self.b)
        a2 = self._calc_a(other.r, other.b)
        c1, c2 = self._calc_c(a1, a2, self.r, other.r)
        return (raw_est - c1) / (1 - c2)

Example 4

Project: volumina Source File: brushingmodel_test.py
    def _checkBrushSize( self, size, should_diameter ):
        m = BrushingModel()

        def check( point, labels ):
            self.assertEqual(max((np.count_nonzero(labels[row,:]) for row in xrange(labels.shape[0]))), should_diameter)
            self.assertEqual(max((np.count_nonzero(labels[col,:]) for col in xrange(labels.shape[1]))), should_diameter)
        m.setBrushSize( size )
        m.brushStrokeAvailable.connect( check )
        m.beginDrawing( QPointF(size*2,size*2), (size*3,size*3) )
        m.endDrawing( QPointF(size*2, size*2) )

Example 5

Project: datasketch Source File: minhash.py
Function: jaccard
    def jaccard(self, other):
        '''
        Estimate the Jaccard similarity (resemblance) between this Minhash
        and the other.
        '''
        if other.seed != self.seed:
            raise ValueError("Cannot compute Jaccard given MinHash with\
                    different seeds")
        if len(self) != len(other):
            raise ValueError("Cannot compute Jaccard given MinHash with\
                    different numbers of permutation functions")
        return np.float(np.count_nonzero(self.hashvalues==other.hashvalues)) /\
                np.float(len(self))

Example 6

Project: Machine_Learning Source File: decisionTree.py
def pGini(labels):

    label_set = set(labels)
    summer = 0.0
    Total = labels.shape[0] * 1.0
    for i in label_set:
        summer += (numpy.count_nonzero(labels == i)/Total)**2

    return 1 - summer

Example 7

Project: fuku-ml Source File: SupportVectorMachine.py
    def get_marge(self):

        nonzero = np.count_nonzero(self.W[1:])

        if nonzero == 0:
            return 0
        else:
            return 1 / np.linalg.norm(self.W[1:])

Example 8

Project: datasketch Source File: hyperloglog.py
Function: count
    def count(self):
        num_zero = self.m - np.count_nonzero(self.reg)
        if num_zero > 0:
            # linear counting
            lc = self._linearcounting(num_zero)
            if lc <= self._get_threshold(self.p):
                return lc
        # Use HyperLogLog estimation function
        e = self.alpha * float(self.m ** 2) / np.sum(2.0**(-self.reg))
        if e <= 5 * self.m:
            return e - self._estimate_bias(e, self.p)
        else:
            return e

Example 9

Project: msmbuilder-legacy Source File: TrimAssignments.py
Function: run
def run(assignments, distances, cutoff):
    number = np.count_nonzero(distances > cutoff)
    logger.info('Discarding %d assignments', number)

    assignments[np.where(distances > cutoff)] = -1
    return assignments

Example 10

Project: menpofit Source File: stats.py
def compute_cuemulative_error(errors, bins):
    r"""
    Computes the values of the Cumulative Error Distribution (CED).

    Parameters
    ----------
    errors : `list` of `float`
        The `list` of errors per image.
    bins : `list` of `float`
        The values of the error bins centers at which the CED is evaluated.

    Returns
    -------
    ced : `list` of `float`
        The computed CED.
    """
    n_errors = len(errors)
    return [np.count_nonzero([errors <= x]) / n_errors for x in bins]

Example 11

Project: nupic Source File: tm_high_order.py
Function: accuracy
def accuracy(current, predicted):
  """
  Computes the accuracy of the TM at time-step t based on the prediction
  at time-step t-1 and the current active columns at time-step t.
  
  @param current (array) binary vector containing current active columns
  @param predicted (array) binary vector containing predicted active columns
  
  @return acc (float) prediction accuracy of the TM at time-step t
  """  
  accuracy = 0
  if np.count_nonzero(predicted) > 0:
    accuracy = float(np.dot(current, predicted))/float(np.count_nonzero(predicted))
  return accuracy

Example 12

Project: hope Source File: test_op_div.py
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_binary_div(dtype, shape):
    def fkt(a, b, c):
        c[:] = a / b
    hfkt = hope.jit(fkt)
    (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
    if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1
    if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1
    ro, rh = fkt(ao, bo, co),  hfkt(ah, bh, ch)
    if dtype in [np.float32, np.float64, float]:
        co[co < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
        ch[ch < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
    assert check(co, ch)

Example 13

Project: nupic.research Source File: neural_correlations_utils.py
Function: accuracy
def accuracy(current, predicted):
  """
  Computes the accuracy of the TM at time-step t based on the prediction
  at time-step t-1 and the current active columns at time-step t.
  
  @param current (array) binary vector containing current active columns
  @param predicted (array) binary vector containing predicted active columns  
  @return acc (float) prediction accuracy of the TM at time-step t
  """  
  acc = 0
  if np.count_nonzero(predicted) > 0:
    acc = float(np.dot(current, predicted))/float(np.count_nonzero(predicted))
  return acc   

Example 14

Project: hope Source File: test_op_div.py
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_augmented_floordiv(dtype, shape):
    def fkt(a, c):
        c[:] //= a
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    ro, rh = fkt(ao, co),  hfkt(ah, ch)
    if dtype in [np.float32, np.float64, float]:
        co[co < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
        ch[ch < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
    assert check(co.astype(dtype), ch.astype(dtype))

Example 15

Project: RLScore Source File: interactive_rls_classifier.py
    def claim_all_points_in_working_set(self, newclazz):
        #'''
        _interactive_rls_classifier.claim_all_points(self.Y_ws,
                     self.classcounts_ws,
                     self.classvec_ws,
                     self.size_ws,
                     self.DVTY,
                     self.sqrtRx2_ws,
                     self.sqrtR.shape[1],
                     newclazz)
        #Update global books DOES NOT WORK FOR RY or Y_Schur_RY!!!
        self.Y[self.working_set] = self.Y_ws
        #print np.sum(np.abs(np.multiply(self.D.T, self.svecs.T * self.Y) - self.DVTY))
        #self.RY[working_set] = self.RY_ws
        #self.Y_Schur_RY[working_set] = self.Y_Schur_RY_ws
        self.classvec[self.working_set] = self.classvec_ws
        for i in range(self.labelcount):
            self.classcounts[i] = self.size - np.count_nonzero(self.classvec - i)

Example 16

Project: deep-learning-samples Source File: simple_binary_classifier.py
def L01_loss(X, y, theta):
    """Computes the L0/1 loss for the data X using theta.

    X: (k, n) k rows of data items, each having n features; augmented.
    y: (k, 1) correct classifications (+1 or -1) for each item.
    theta: (n, 1) regression parameters.

    Returns the total L0/1 loss over the whole data set. The total L0/1 loss
    is the number of mispredicted items (where y doesn't match yhat).
    """
    results = predict_binary(X, theta)
    return np.count_nonzero(results != y)

Example 17

Project: postpic Source File: particles.py
Function: len
    def __len__(self):  # = number of particles
        # find a valid dataset to count number of paricles
        # return 0 if no valid dataset can be found
        ret = 0
        if self._compressboollist is not None:
            return np.count_nonzero(self._compressboollist)
        for key in self._atomicprops:
            try:
                # len(3) will yield a TypeError, len([3]) returns 1
                ret = len(self._readatomic(key))
                break
            except(TypeError, KeyError):
                pass
        return ret

Example 18

Project: xcs Source File: _numpy_bitstrings.py
Function: count
    def count(self):
        """Returns the number of bits set to True in the bit string.

        Usage:
            assert BitString('00110').count() == 2

        Arguments: None
        Return:
            An int, the number of bits with value 1.
        """
        return int(numpy.count_nonzero(self._bits))

Example 19

Project: Machine_Learning Source File: adaboost.py
Function: is_good_enough
    def is_good_enough(self):
        self.N += 1

        output = self.prediction(self._Mat)

        e = numpy.count_nonzero(output == self._Tag)/(self.SamplesNum*1.) 
        self.accuracy.append( e )

        self.detectionRate = numpy.count_nonzero(output[0:POSITIVE_SAMPLE] == 1) * 1./ POSITIVE_SAMPLE

        if output.tolist() == self._Tag.tolist():
            return True
        else:
            return False

Example 20

Project: pylearn2 Source File: test_conv2d_c01b.py
    def test_make_sparse_random_conv2D(self):
        """
        Make random sparse filters, count whether the number of
        non-zero elements is sensible
        """
        axes = ('c', 0, 1, 'b')
        input_space = Conv2DSpace((3, 3), 16, axes=axes)
        output_space = Conv2DSpace((3, 3), 16, axes=axes)
        num_nonzero = 2
        kernel_shape = (2, 2)

        conv2d = make_sparse_random_conv2D(num_nonzero, input_space,
                                           output_space, kernel_shape)
        f = theano.function([self.image_tensor],
                            conv2d.lmul(self.image_tensor))
        assert f(self.image).shape == (16, 2, 2, 1)
        assert conv2d.output_axes == axes
        assert numpy.count_nonzero(conv2d._filters.get_value()) >= 32

Example 21

Project: scikit-learn Source File: test_logistic.py
def test_logreg_cv_penalty():
    # Test that the correct penalty is passed to the final fit.
    X, y = make_classification(n_samples=50, n_features=20, random_state=0)
    lr_cv = LogisticRegressionCV(penalty="l1", Cs=[1.0], solver='liblinear')
    lr_cv.fit(X, y)
    lr = LogisticRegression(penalty="l1", C=1.0, solver='liblinear')
    lr.fit(X, y)
    assert_equal(np.count_nonzero(lr_cv.coef_), np.count_nonzero(lr.coef_))

Example 22

Project: nupic Source File: sp_tutorial.py
Function: percentoverlap
def percentOverlap(x1, x2, size):
  """
  Computes the percentage of overlap between vectors x1 and x2.

  @param x1   (array) binary vector
  @param x2   (array) binary vector
  @param size (int)   length of binary vectors
  
  @return percentOverlap (float) percentage overlap between x1 and x2
  """
  nonZeroX1 = np.count_nonzero(x1)
  nonZeroX2 = np.count_nonzero(x2)
  minX1X2 = min(nonZeroX1, nonZeroX2)
  percentOverlap = 0
  if minX1X2 > 0:
    percentOverlap = float(np.dot(x1, x2))/float(minX1X2)
  return percentOverlap

Example 23

Project: nupic.research Source File: neural_correlations_utils.py
Function: percentoverlap
def percentOverlap(x1, x2, numColumns):
  """
  Calculates the percentage of overlap between two SDRs
  
  @param x1 (array) SDR
  @param x2 (array) SDR  
  @return percentageOverlap (float) percentage overlap between x1 and x2
  """  
  nonZeroX1 = np.count_nonzero(x1)
  nonZeroX2 = np.count_nonzero(x2)
  sparseCols = min(nonZeroX1, nonZeroX2)  
  # transform input vector specifying columns into binary vector
  binX1 = np.zeros(numColumns, dtype="uint32")
  binX2 = np.zeros(numColumns, dtype="uint32")
  for i in range(sparseCols):
    binX1[x1[i]] = 1
    binX2[x2[i]] = 1
  return float(np.dot(binX1, binX2))/float(sparseCols)

Example 24

Project: theanolm Source File: numpybigramoptimizer.py
    def _class_size(self, class_id):
        """Calculates the number of words in a class.

        :type class_id: int
        :param class_id: ID of a class

        :rtype: int
        :returns: number of words in the class
        """

        return numpy.count_nonzero(self._word_to_class == class_id)

Example 25

Project: hope Source File: test_op_div.py
@pytest.mark.parametrize("dtype,shape", itertools.product(dtypes, shapes[1:]))
def test_binary_floordiv(dtype, shape):
    def fkt(a, b, c):
        c[:] = a // b
    hfkt = hope.jit(fkt)
    (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
    if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1
    if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1
    ro, rh = fkt(ao, bo, co),  hfkt(ah, bh, ch)
    if dtype in [np.float32, np.float64, float]:
        co[co < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
        ch[ch < 1. /  (np.finfo(dtype).max * np.finfo(dtype).resolution)] /= np.finfo(dtype).resolution
    assert check(co, ch)

Example 26

Project: scikit-learn Source File: test_omp.py
def test_n_nonzero_coefs():
    assert_true(np.count_nonzero(orthogonal_mp(X, y[:, 0],
                                 n_nonzero_coefs=5)) <= 5)
    assert_true(np.count_nonzero(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5,
                                               precompute=True)) <= 5)

Example 27

Project: heatmap Source File: numpy_example.py
def main():
    logging.basicConfig(format='%(relativeCreated)8d ms  // %(message)s')
    description = 'generate random points, save them in a numpy array'
    parser = ArgumentParser(description=description,
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('--output', metavar='FILE', default='/tmp/out.png')
    parser.add_argument('-v', '--verbose', action='store_true')
    parser.add_argument('--debug', action='store_true')
    parser.add_argument('count', type=int)
    args = parser.parse_args()
    if args.verbose:
        logging.getLogger().setLevel(logging.INFO)
    if args.debug:
        logging.getLogger().setLevel(logging.DEBUG)

    logging.debug('python version %s' % str(sys.version))

    config = setup_config(args.count)
    matrix = hm.process_shapes(config)
    matrix = matrix.finalized()
    arr = matrix_to_numpy(config, matrix)
    print('shape: ' + str(arr.shape))
    print('max value: %f' % arr.max())
    nonzero = np.count_nonzero(arr)
    print('nonzero cells: %d / %d (%d%%)' % (nonzero, arr.size,
                                             int(100.0 * nonzero / arr.size)))

Example 28

Project: pyscf Source File: hf_symm.py
def _dump_mo_energy(mol, mo_energy, mo_occ, ehumo, elumo, orbsym, title='',
                    verbose=logger.DEBUG):
    if isinstance(verbose, logger.Logger):
        log = verbose
    else:
        log = logger.Logger(mol.stdout, verbose)
    nirrep = mol.symm_orb.__len__()
    for i, ir in enumerate(mol.irrep_id):
        irname = mol.irrep_name[i]
        ir_idx = (orbsym == ir)
        nso = numpy.count_nonzero(ir_idx)
        nocc = numpy.count_nonzero(mo_occ[ir_idx])
        e_ir = mo_energy[ir_idx]
        if nocc == 0:
            log.debug('%s%s nocc = 0', title, irname)
        elif nocc == nso:
            log.debug('%s%s nocc = %d  HUMO = %.15g',
                      title, irname, nocc, e_ir[nocc-1])
        else:
            log.debug('%s%s nocc = %d  HUMO = %.15g  LUMO = %.15g',
                      title, irname, nocc, e_ir[nocc-1], e_ir[nocc])
            if e_ir[nocc-1]+1e-3 > elumo:
                log.warn('!! %s%s HUMO %.15g > system LUMO %.15g',
                         title, irname, e_ir[nocc-1], elumo)
            if e_ir[nocc] < ehumo+1e-3:
                log.warn('!! %s%s LUMO %.15g < system HUMO %.15g',
                         title, irname, e_ir[nocc], ehumo)
        log.debug('   mo_energy = %s', e_ir)

Example 29

Project: director Source File: logReporter.py
Function: plot_results
    def plotResults(self):
        tmin = self.jointVelocityTimes[0]
        tmax = self.jointVelocityTimes[-1]
        totalMicroseconds = float(tmax-tmin)
        average_dt =  totalMicroseconds/len(self.jointVelocityNorms)
        smoothed = self.movingAverage(self.jointVelocityNorms)
        movement = smoothed > self.movementThreshold 
        movementSeconds = float(np.count_nonzero(movement) * average_dt / 1e6)
        totalSeconds = float((tmax-tmin)/1e6)
        print("%.2f / %.2f seconds of movement ( %.2f %% continuous motion) " % (movementSeconds, totalSeconds , movementSeconds / totalSeconds * 1e2))        
        
        minChargePercent = np.ndarray.min(np.asarray(self.batteryPercentage))
        print("Battery fell from %.2f %% to %.2f %% (Used %.2f %%)" % (self.batteryPercentage[0], minChargePercent , self.batteryPercentage[0] - minChargePercent))
        print 'plotting results'
        plt.figure(1)
        plt.suptitle('LCM Log Battery/Movement Analysis')
        plt.subplot(311)
        self.plotMovement(smoothed, movement, tmin)
        plt.subplot(312)
        self.plotPump(tmin)
        plt.subplot(313)
        self.plotBattery(tmin)

        plt.xlabel('Time (s)')
        plt.show()      

Example 30

Project: scikit-learn Source File: test_omp.py
def test_estimator():
    omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
    omp.fit(X, y[:, 0])
    assert_equal(omp.coef_.shape, (n_features,))
    assert_equal(omp.intercept_.shape, ())
    assert_true(np.count_nonzero(omp.coef_) <= n_nonzero_coefs)

    omp.fit(X, y)
    assert_equal(omp.coef_.shape, (n_targets, n_features))
    assert_equal(omp.intercept_.shape, (n_targets,))
    assert_true(np.count_nonzero(omp.coef_) <= n_targets * n_nonzero_coefs)

    omp.set_params(fit_intercept=False, normalize=False)

    omp.fit(X, y[:, 0])
    assert_equal(omp.coef_.shape, (n_features,))
    assert_equal(omp.intercept_, 0)
    assert_true(np.count_nonzero(omp.coef_) <= n_nonzero_coefs)

    omp.fit(X, y)
    assert_equal(omp.coef_.shape, (n_targets, n_features))
    assert_equal(omp.intercept_, 0)
    assert_true(np.count_nonzero(omp.coef_) <= n_targets * n_nonzero_coefs)

Example 31

Project: tardis Source File: base.py
    def get_convergence_status(self, t_rad, w, t_inner, estimated_t_rad, estimated_w,
                               estimated_t_inner):
        convergence_section = self.tardis_config.montecarlo.convergence_strategy
        no_of_shells = self.tardis_config.structure.no_of_shells

        convergence_t_rad = (abs(t_rad - estimated_t_rad) /
                             estimated_t_rad).value
        convergence_w = (abs(w - estimated_w) / estimated_w)
        convergence_t_inner = (abs(t_inner - estimated_t_inner) /
                               estimated_t_inner).value

        if convergence_section.type == 'specific':
            fraction_t_rad_converged = (
                np.count_nonzero(
                    convergence_t_rad < convergence_section.t_rad.threshold)
                / no_of_shells)

            t_rad_converged = (
                fraction_t_rad_converged > convergence_section.t_rad.threshold)

            fraction_w_converged = (
                np.count_nonzero(
                    convergence_w < convergence_section.w.threshold)
                / no_of_shells)

            w_converged = (
                fraction_w_converged > convergence_section.w.threshold)

            t_inner_converged = (
                convergence_t_inner < convergence_section.t_inner.threshold)

            if np.all([t_rad_converged, w_converged, t_inner_converged]):
                return True
            else:
                return False

        else:
            return False

Example 32

Project: tvb-framework Source File: allen_creator.py
def AreasVolumeTreshold (projmaps,vol_thresh,resolution,Vol,ontology):
    threshold=(vol_thresh)/(resolution**3)
    id_ok=[]
    for ID in projmaps.keys():
        tot_voxels = np.count_nonzero(Vol==ID)
        if tot_voxels>threshold:
            id_ok.append(ID)
        else: #I will check for child
            child=list(ontology.get_child_ids( ontology[ID].id ))
            while len(child)!=0:
                child_voxel_count = np.count_nonzero(Vol==child[0])
                if child_voxel_count > 0:
                    tot_voxels += child_voxel_count
                    if tot_voxels>threshold:
                        id_ok.append(ID)
                        break
                else:
                    if len(ontology.get_child_ids(ontology[child[0]].id))!=0:
                        child.extend(ontology.get_child_ids(ontology[child[0]].id ))
                child.remove(child[0])
     #Remove areas that are not in id_ok from the injection list
    for checkid in projmaps.keys():
        if checkid not in id_ok:
            projmaps.pop(checkid,None)
    #Remove areas that are not in id_ok from target list (columns+matrix)
    for inj_id in range(len(projmaps.values())):
        targ_id=-1
        while len(projmaps.values()[inj_id]['columns'])!=(len(id_ok)*3): #I have 3 hemispheres
            targ_id+=1
            if projmaps.values()[inj_id]['columns'][targ_id]['structure_id'] not in id_ok:
                del projmaps.values()[inj_id]['columns'][targ_id]
                projmaps.values()[inj_id]['matrix']=np.delete(projmaps.values()[inj_id]['matrix'],targ_id,1)
                targ_id=-1
    return projmaps

Example 33

Project: imbalanced-learn Source File: imbalance.py
def make_imbalance(X, y, ratio, min_c_=None, random_state=None):
    """Turns a dataset into an imbalanced dataset at specific ratio.
    A simple toy dataset to visualize clustering and classification
    algorithms.

    Parameters
    ----------
    X : ndarray, shape (n_samples, n_features)
        Matrix containing the data to be imbalanced.

    y : ndarray, shape (n_samples, )
        Corresponding label for each sample in X.

    ratio : float,
        The desired ratio given by the number of samples in
        the minority class over the the number of samples in
        the majority class. Thus the ratio should be in the interval [0., 1.]

    min_c_ : str or int, optional (default=None)
        The identifier of the class to be the minority class.
        If None, min_c_ is set to be the current minority class.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by np.random.

    Returns
    -------
    X_resampled : ndarray, shape (n_samples_new, n_features)
        The array containing the imbalanced data.

    y_resampled : ndarray, shape (n_samples_new)
        The corresponding label of `X_resampled`

    """
    if isinstance(ratio, float):
        if ratio > 1:
            raise ValueError('Ration cannot be greater than one.')
        elif ratio <= 0:
            raise ValueError('Ratio cannot be negative.')
    else:
        raise ValueError('Ratio must be a float between 0.0 < ratio < 1.0')

    X, y = check_X_y(X, y)

    random_state = check_random_state(random_state)

    stats_c_ = Counter(y)

    LOGGER.info('The original target distribution in the dataset is: %s',
                stats_c_)

    if min_c_ is None:
        min_c_ = min(stats_c_, key=stats_c_.get)

    n_min_samples = int(np.count_nonzero(y != min_c_) * ratio)
    if n_min_samples > stats_c_[min_c_]:
        raise ValueError('Current imbalance ratio of data is lower than'
                         ' desired ratio!')
    if n_min_samples == 0:
        raise ValueError('Not enough samples for desired ratio!')

    mask = y == min_c_

    idx_maj = np.where(~mask)[0]
    idx_min = np.where(mask)[0]
    idx_min = random_state.choice(idx_min, size=n_min_samples, replace=False)
    idx = np.concatenate((idx_min, idx_maj), axis=0)

    X_resampled, y_resampled = X[idx, :], y[idx]

    LOGGER.info('Make the dataset imbalanced: %s', Counter(y_resampled))

    return X_resampled, y_resampled

Example 34

Project: tvb-framework Source File: allen_creator.py
def ParentsAndGrandParentsFinder(order, key_ord, Vol, ontology):
    parents=[] #Here it will be the id of the parents of the areas in the parcellation
    grandparents=[] #Here it will be the id of the grandparents of the areas in the parcellation
    vol_areas=[] #Here it will be the volume of the areas in the parcellation
    vec_index=[] #Here it will be the index of the vector of the areas in the parcellation
    index=0
    for graph_ord_inj in key_ord:
        ID=order[graph_ord_inj][0]
        ont=ontology[ID]
        parent_id=ont.loc[ID]['parent_structure_id']
        parents.append(parent_id)
        parent_id=int(parent_id)
        ont=ontology[parent_id]
        grandparents.append(ont.loc[parent_id]['parent_structure_id'])
        vec_index.append(index)
        index+=1
        tot_voxels = np.count_nonzero(Vol==ID)
        if tot_voxels > 0:
            vol_areas.append(tot_voxels)
        else: #if ID not in annotation vol, check for child
            tot_voxels=0
            child=list(ontology.get_child_ids( ontology[ID].id ))
            while len(child)!=0:
                print child
                child_voxel_count = np.count_nonzero(Vol == child[0])
                if child_voxel_count > 0:
                    tot_voxels+=child_voxel_count
                else:
                    if len(ontology.get_child_ids(ontology[child[0]].id))!=0:
                        child.extend(ontology.get_child_ids( ontology[child[0]].id ))
                child.remove(child[0])
            vol_areas.append(tot_voxels)
    #I will order parents, grandparents, vec_index according to the volume of the areas
    parents=[parents for (vv,parents) in sorted(zip(vol_areas,parents))]
    grandparents=[grandparents for (vv,grandparents) in sorted(zip(vol_areas,grandparents))]
    vec_index=[iid for (vv,iid) in sorted(zip(vol_areas,vec_index))]
    k=len(parents)
    unique_parents={}#Unique parents will be a dictionary with keys the parent id and as value the index vec of the region in parcellation which has that parent id
    for p in reversed(parents):
        k-=1
        if p not in unique_parents.keys():
            unique_parents[p]=vec_index[k]
    k=len(grandparents)
    unique_gradparents={}#Unique parents will be a dictionary with keys the parent id and as value the index vec of the region in my parcellation that has that parent id
    for p in reversed(grandparents):
        k-=1
        if np.isnan(p)==0:
            if p not in unique_gradparents.keys():
                unique_gradparents[p]=vec_index[k]
    return unique_parents, unique_gradparents

Example 35

Project: deep_recommend_system Source File: nn_test.py
Function: test_dropout
  def testDropout(self):
    # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
    # that it is producing approximately the right number of ones over a large
    # number of samples, based on the keep probability.
    x_dim = 40
    y_dim = 30
    num_iter = 10
    for keep_prob in [0.1, 0.5, 0.8]:
      with self.test_session():
        t = tf.constant(1.0, shape=[x_dim, y_dim], dtype=tf.float32)
        dropout = tf.nn.dropout(t, keep_prob)
        final_count = 0
        self.assertEqual([x_dim, y_dim], dropout.get_shape())
        for _ in xrange(0, num_iter):
          value = dropout.eval()
          final_count += np.count_nonzero(value)
          # Verifies that there are only two values: 0 and 1/keep_prob.
          sorted_value = np.unique(np.sort(value))
          self.assertEqual(0, sorted_value[0])
          self.assertAllClose(1 / keep_prob, sorted_value[1])
      # Check that we are in the 15% error range
      expected_count = x_dim * y_dim * keep_prob * num_iter
      rel_error = math.fabs(final_count - expected_count) / expected_count
      print(rel_error)
      self.assertTrue(rel_error < 0.15)

Example 36

Project: rlpy Source File: FlipBoard.py
Function: is_terminal
    def isTerminal(self):
        return np.count_nonzero(self.state) == self.BOARD_SIZE ** 2

Example 37

Project: scikit-learn Source File: bench_sparsify.py
def sparsity_ratio(X):
    return np.count_nonzero(X) / float(n_samples * n_features)

Example 38

Project: scipy Source File: data.py
Function: count_nonzero
    def count_nonzero(self):
        return np.count_nonzero(self._deduped_data())

Example 39

Project: deep_recommend_system Source File: nn_test.py
  def testShapedDropout(self):
    # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
    # that it is producing approximately the right number of ones over a large
    # number of samples, based on the keep probability. This time with shaped
    # noise.
    x_dim = 40 * 30
    y_dim = 3
    num_iter = 10
    for keep_prob in [0.1, 0.5, 0.8]:
      with self.test_session():
        t = tf.constant(1.0, shape=[x_dim, y_dim], dtype=tf.float32)
        dropout = tf.nn.dropout(t, keep_prob, noise_shape=[x_dim, 1])
        self.assertEqual([x_dim, y_dim], dropout.get_shape())
        final_count = 0
        for _ in xrange(0, num_iter):
          value = dropout.eval()
          final_count += np.count_nonzero(value)
          # Verifies that there are only two values: 0 and 1/keep_prob.
          sorted_value = np.unique(np.sort(value))
          self.assertEqual(0, sorted_value[0])
          self.assertAllClose(1 / keep_prob, sorted_value[1])
      # Check that we are in the 15% error range
      expected_count = x_dim * y_dim * keep_prob * num_iter
      rel_error = math.fabs(final_count - expected_count) / expected_count
      print(rel_error)
      self.assertTrue(rel_error < 0.15)

Example 40

Project: deep_recommend_system Source File: nn_test.py
  def testDropoutPlaceholderKeepProb(self):
    # Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
    # that it is producing approximately the right number of ones over a large
    # number of samples, based on the keep probability.
    x_dim = 40
    y_dim = 30
    num_iter = 10
    for keep_prob in [0.1, 0.5, 0.8]:
      with self.test_session():
        t = tf.constant(1.0, shape=[x_dim, y_dim], dtype=tf.float32)
        keep_prob_placeholder = tf.placeholder(tf.float32)
        dropout = tf.nn.dropout(t, keep_prob_placeholder)
        final_count = 0
        self.assertEqual([x_dim, y_dim], dropout.get_shape())
        for _ in xrange(0, num_iter):
          value = dropout.eval(feed_dict={keep_prob_placeholder: keep_prob})
          final_count += np.count_nonzero(value)
          # Verifies that there are only two values: 0 and 1/keep_prob.
          sorted_value = np.unique(np.sort(value))
          self.assertEqual(0, sorted_value[0])
          self.assertAllClose(1 / keep_prob, sorted_value[1])
      # Check that we are in the 15% error range
      expected_count = x_dim * y_dim * keep_prob * num_iter
      rel_error = math.fabs(final_count - expected_count) / expected_count
      print(rel_error)
      self.assertTrue(rel_error < 0.15)

Example 41

Project: scipy Source File: dia.py
Function: count_nonzero
    def count_nonzero(self):
        mask = self._data_mask()
        return np.count_nonzero(self.data[mask])

Example 42

Project: activitysim Source File: activitysim.py
Function: check_for_variability
def _check_for_variability(model_design, trace_label):
    """
    This is an internal method which checks for variability in each
    expression - under the assumption that you probably wouldn't be using a
    variable (in live simulations) if it had no variability.  This is a
    warning to the user that they might have constructed the variable
    incorrectly.  It samples 1000 rows in order to not hurt performance -
    it's likely that if 1000 rows have no variability, the whole dataframe
    will have no variability.
    """

    if trace_label is None:
        trace_label = '_check_for_variability'

    l = min(1000, len(model_design))

    sample = random_rows(model_design, l)

    no_variability = has_missing_vals = 0
    for i in range(len(sample.columns)):
        v = sample.iloc[:, i]
        if v.min() == v.max():
            col_name = sample.columns[i]
            logger.info("%s: no variability (%s) in: %s" % (trace_label, v.iloc[0], col_name))
            no_variability += 1
        # FIXME - how could this happen? Not sure it is really a problem?
        if np.count_nonzero(v.isnull().values) > 0:
            col_name = sample.columns[i]
            logger.info("%s: missing values in: %s" % (trace_label, v.iloc[0], col_name))
            has_missing_vals += 1

    if no_variability > 0:
        logger.warn("%s: %s columns have no variability" % (trace_label, no_variability))

    if has_missing_vals > 0:
        logger.warn("%s: %s columns have missing values" % (trace_label, has_missing_vals))

Example 43

Project: CommPy Source File: convcode.py
    def _generate_edges(self, trellis_length, grid, state_order, state_radius, edge_colors):
        """ Private method """
        edge_patches = []

        for current_time_index in range(trellis_length-1):
            grid_subset = grid[:,self.number_states * current_time_index:]
            for state_count_1 in range(self.number_states):
                input_count = 0
                for state_count_2 in range(self.number_states):
                    dx = grid_subset[0, state_count_2+self.number_states] - grid_subset[0,state_count_1] - 2*state_radius
                    dy = grid_subset[1, state_count_2+self.number_states] - grid_subset[1,state_count_1]
                    if np.count_nonzero(self.next_state_table[state_order[state_count_1],:] == state_order[state_count_2]):
                        found_index = np.where(self.next_state_table[state_order[state_count_1],:] ==
                                                state_order[state_count_2])
                        edge_patch = mpatches.FancyArrow(grid_subset[0,state_count_1]+state_radius,
                                grid_subset[1,state_count_1], dx, dy, width=0.005,
                                length_includes_head = True, color = edge_colors[found_index[0][0]])
                        edge_patches.append(edge_patch)
                        input_count = input_count + 1

        return edge_patches

Example 44

Project: scikit-learn Source File: plot_model_complexity_influence.py
def _count_nonzero_coefficients(estimator):
    a = estimator.coef_.toarray()
    return np.count_nonzero(a)

Example 45

Project: scipy Source File: lil.py
Function: count_nonzero
    def count_nonzero(self):
        return sum(np.count_nonzero(rowvals) for rowvals in self.data)

Example 46

Project: PyFR Source File: gimmik.py
    def mul(self, a, b, out, alpha=1.0, beta=0.0):
        # Ensure the matrices are compatible
        if a.nrow != out.nrow or a.ncol != b.nrow or b.ncol != out.ncol:
            raise ValueError('Incompatible matrices for out = a*b')

        # Check that A is constant
        if 'const' not in a.tags:
            raise NotSuitableError('GiMMiK requires a constant a matrix')

        # Check that A is reasonably sparse
        if np.count_nonzero(a.get()) > self.max_nnz:
            raise NotSuitableError('Matrix too dense for GiMMiK')

        # Generate
        src = generate_mm(a.get(), dtype=a.dtype, platform='cuda',
                          alpha=alpha, beta=beta)

        # Build
        fun = self._build_kernel('gimmik_mm', src, 'iPiPi')

        # Determine the grid/block
        block = (128, 1, 1)
        grid = get_grid_for_block(block, b.ncol)

        class MulKernel(ComputeKernel):
            def run(self, queue):
                fun.prepared_async_call(grid, block, queue.cuda_stream_comp,
                                        b.ncol, b, b.leaddim, out,
                                        out.leaddim)

        return MulKernel()

Example 47

Project: rlpy Source File: GeneralTools.py
Function: sparsity
def sparsity(A):
    """ Returns the percentage of nonzero elements in ``A``. """
    return (1 - np.count_nonzero(A) / (np.prod(A.shape) * 1.)) * 100

Example 48

Project: PyFR Source File: gimmik.py
    def mul(self, a, b, out, alpha=1.0, beta=0.0):
        # Ensure the matrices are compatible
        if a.nrow != out.nrow or a.ncol != b.nrow or b.ncol != out.ncol:
            raise ValueError('Incompatible matrices for out = a*b')

        # Check that A is constant
        if 'const' not in a.tags:
            raise NotSuitableError('GiMMiK requires a constant a matrix')

        # Check that A is reasonably sparse
        if np.count_nonzero(a.get()) > self.max_nnz:
            raise NotSuitableError('Matrix too dense for GiMMiK')

        # Generate
        src = generate_mm(a.get(), dtype=a.dtype, platform='opencl',
                          alpha=alpha, beta=beta)

        # Build
        fun = self._build_kernel('gimmik_mm', src,
                                 [np.int32] + [np.intp, np.int32]*2)

        class MulKernel(ComputeKernel):
            def run(self, queue):
                fun(queue.cl_queue_comp, (b.ncol,), None, b.ncol,
                    b.data, b.leaddim, out.data, out.leaddim)

        return MulKernel()

Example 49

Project: iris Source File: message.py
    def __getitem__(self, keys):
        # NB. Currently assumes that the validity of this interpretation
        # is checked before this proxy is created.
        message = self.recreate_raw()
        sections = message.sections
        bitmap_section = sections[6]
        bitmap = self._bitmap(bitmap_section)
        data = sections[7]['codedValues']

        if bitmap is not None:
            # Note that bitmap and data are both 1D arrays at this point.
            if np.count_nonzero(bitmap) == data.shape[0]:
                # Only the non-masked values are included in codedValues.
                _data = np.empty(shape=bitmap.shape)
                _data[bitmap.astype(bool)] = data
                # `np.ma.masked_array` masks where input = 1, the opposite of
                # the behaviour specified by the GRIB spec.
                data = np.ma.masked_array(_data, mask=np.logical_not(bitmap))
            else:
                msg = 'Shapes of data and bitmap do not match.'
                raise TranslationError(msg)

        data = data.reshape(self.shape)
        return data.__getitem__(keys)

Example 50

Project: PyFR Source File: gimmik.py
    def mul(self, a, b, out, alpha=1.0, beta=0.0):
        # Ensure the matrices are compatible
        if a.nrow != out.nrow or a.ncol != b.nrow or b.ncol != out.ncol:
            raise ValueError('Incompatible matrices for out = a*b')

        # Check that A is constant
        if 'const' not in a.tags:
            raise NotSuitableError('GiMMiK requires a constant a matrix')

        # Check that A is reasonably sparse
        if np.count_nonzero(a.get()) > self.max_nnz:
            raise NotSuitableError('Matrix too dense for GiMMiK')

        # Generate the GiMMiK kernel
        src = generate_mm(a.get(), dtype=a.dtype, platform='c-omp',
                          alpha=alpha, beta=beta)
        gimmik_mm = self._build_kernel('gimmik_mm', src,
                                       [np.int32] + [np.intp, np.int32]*2)

        class MulKernel(ComputeKernel):
            def run(self, queue):
                gimmik_mm(b.ncol, b, b.leaddim, out, out.leaddim)

        return MulKernel()
See More Examples - Go to Next Page
Page 1 Selected Page 2