Python numpy.count_nonzero() Examples
The following are 30 code examples for showing how to use numpy.count_nonzero(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: OpenCV-Computer-Vision-Projects-with-Python Author: PacktPublishing File: classifiers.py License: MIT License | 6 votes |
def _accuracy(self, y_test, Y_vote): """Calculates accuracy This method calculates the accuracy based on a vector of ground-truth labels (y_test) and a 2D voting matrix (Y_vote) of size (len(y_test), num_classes). :param y_test: vector of ground-truth labels :param Y_vote: 2D voting matrix (rows=samples, cols=class votes) :returns: accuracy e[0,1] """ # predicted classes y_hat = np.argmax(Y_vote, axis=1) # all cases where predicted class was correct mask = y_hat == y_test return np.float32(np.count_nonzero(mask)) / len(y_test)
Example 2
Project: OpenCV-Computer-Vision-Projects-with-Python Author: PacktPublishing File: classifiers.py License: MIT License | 6 votes |
def _confusion(self, y_test, Y_vote): """Calculates confusion matrix This method calculates the confusion matrix based on a vector of ground-truth labels (y-test) and a 2D voting matrix (Y_vote) of size (len(y_test), num_classes). Matrix element conf[r,c] will contain the number of samples that were predicted to have label r but have ground-truth label c. :param y_test: vector of ground-truth labels :param Y_vote: 2D voting matrix (rows=samples, cols=class votes) :returns: confusion matrix """ y_hat = np.argmax(Y_vote, axis=1) conf = np.zeros((self.num_classes, self.num_classes)).astype(np.int32) for c_true in xrange(self.num_classes): # looking at all samples of a given class, c_true # how many were classified as c_true? how many as others? for c_pred in xrange(self.num_classes): y_this = np.where((y_test == c_true) * (y_hat == c_pred)) conf[c_pred, c_true] = np.count_nonzero(y_this) return conf
Example 3
Project: EXOSIMS Author: dsavransky File: test_KnownRVPlanets.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_mass(self): r"""Test gen_mass method. Approach: Ensures the output is set, of the correct type, length, and units. Check the range of the returned values. Check that, for this power law, there are more small than large masses (for n large). """ plan_pop = self.fixture n = 10000 # call the routine masses = plan_pop.gen_mass(n) # check the type self.assertEqual(type(masses), type(1.0 * u.kg)) # crude check on the shape (more small than large for this power law) midpoint = np.mean(plan_pop.Mprange) self.assertGreater(np.count_nonzero(masses < midpoint), np.count_nonzero(masses > midpoint)) # test some illegal "n" values n_list_bad = [-1, '100', 22.5] for n in n_list_bad: with self.assertRaises(AssertionError): masses = plan_pop.gen_mass(n)
Example 4
Project: EXOSIMS Author: dsavransky File: test_KeplerLike1.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_mass(self): r"""Test gen_mass method. Approach: Ensures the output is set, of the correct type, length, and units. Check that returned values are nonnegative. Check that, for this power law, there are more small than large masses (for n large). """ plan_pop = self.fixture n = 10000 masses = plan_pop.gen_mass(n) self.assertEqual(len(masses), n) self.assertTrue(np.all(masses.value >= 0)) self.assertTrue(np.all(np.isfinite(masses.value))) midpoint = np.mean(masses) self.assertGreater(np.count_nonzero(masses < midpoint), np.count_nonzero(masses > midpoint)) # test some illegal "n" values n_list_bad = [-1, '100', 22.5] for n in n_list_bad: with self.assertRaises(AssertionError): masses = plan_pop.gen_mass(n)
Example 5
Project: EXOSIMS Author: dsavransky File: test_KnownRVPlanetsUniverse.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_init_indexes(self): r"""Test of initialization and __init__ -- indexes. Method: Insure the plan2star and sInds indexes are present. Performs sanity check on the range of index values. TODO: More could be done to ensure the index values are correct. """ universe = self.fixture self.basic_validation(universe) # indexes present self.assertIn('plan2star', universe.__dict__) self.assertIn('sInds', universe.__dict__) # range: 0 <= sInds < nStars self.assertEqual(0, np.count_nonzero(universe.sInds < 0)) self.assertEqual(0, np.count_nonzero(universe.sInds >= universe.TargetList.nStars)) # domain: plan2star covers 0...nPlans-1 self.assertEqual(len(universe.plan2star), universe.nPlans) # range: 0 <= plan2star < nStars self.assertEqual(0, np.count_nonzero(universe.plan2star < 0)) self.assertEqual(0, np.count_nonzero(universe.plan2star >= universe.TargetList.nStars))
Example 6
Project: pyscf Author: pyscf File: khf.py License: Apache License 2.0 | 6 votes |
def canonicalize(mf, mo_coeff_kpts, mo_occ_kpts, fock=None): if fock is None: dm = mf.make_rdm1(mo_coeff_kpts, mo_occ_kpts) fock = mf.get_fock(dm=dm) mo_coeff = [] mo_energy = [] for k, mo in enumerate(mo_coeff_kpts): mo1 = np.empty_like(mo) mo_e = np.empty_like(mo_occ_kpts[k]) occidx = mo_occ_kpts[k] == 2 viridx = ~occidx for idx in (occidx, viridx): if np.count_nonzero(idx) > 0: orb = mo[:,idx] f1 = reduce(np.dot, (orb.T.conj(), fock[k], orb)) e, c = scipy.linalg.eigh(f1) mo1[:,idx] = np.dot(orb, c) mo_e[idx] = e mo_coeff.append(mo1) mo_energy.append(mo_e) return mo_energy, mo_coeff
Example 7
Project: pyscf Author: pyscf File: kmp2.py License: Apache License 2.0 | 6 votes |
def _frozen_sanity_check(frozen, mo_occ, kpt_idx): '''Performs a few sanity checks on the frozen array and mo_occ. Specific tests include checking for duplicates within the frozen array. Args: frozen (array_like of int): The orbital indices that will be frozen. mo_occ (:obj:`ndarray` of int): The occupuation number for each orbital resulting from a mean-field-like calculation. kpt_idx (int): The k-point that `mo_occ` and `frozen` belong to. ''' frozen = np.array(frozen) nocc = np.count_nonzero(mo_occ > 0) assert nocc, 'No occupied orbitals?\n\nnocc = %s\nmo_occ = %s' % (nocc, mo_occ) all_frozen_unique = (len(frozen) - len(np.unique(frozen))) == 0 if not all_frozen_unique: raise RuntimeError('Frozen orbital list contains duplicates!\n\nkpt_idx %s\n' 'frozen %s' % (kpt_idx, frozen)) if len(frozen) > 0 and np.max(frozen) > len(mo_occ) - 1: raise RuntimeError('Freezing orbital not in MO list!\n\nkpt_idx %s\n' 'frozen %s\nmax orbital idx %s' % (kpt_idx, frozen, len(mo_occ) - 1))
Example 8
Project: pyscf Author: pyscf File: kuhf.py License: Apache License 2.0 | 6 votes |
def _unpack(vo, mo_occ): za = [] zb = [] p1 = 0 for k, occ in enumerate(mo_occ[0]): no = numpy.count_nonzero(occ > 0) nv = occ.size - no p0, p1 = p1, p1 + no * nv za.append(vo[p0:p1].reshape(no,nv)) for k, occ in enumerate(mo_occ[1]): no = numpy.count_nonzero(occ > 0) nv = occ.size - no p0, p1 = p1, p1 + no * nv zb.append(vo[p0:p1].reshape(no,nv)) return za, zb
Example 9
Project: pyscf Author: pyscf File: hf.py License: Apache License 2.0 | 6 votes |
def canonicalize(mf, mo_coeff, mo_occ, fock=None): '''Canonicalization diagonalizes the Fock matrix within occupied, open, virtual subspaces separatedly (without change occupancy). ''' if fock is None: dm = mf.make_rdm1(mo_coeff, mo_occ) fock = mf.get_fock(dm=dm) coreidx = mo_occ == 2 viridx = mo_occ == 0 openidx = ~(coreidx | viridx) mo = numpy.empty_like(mo_coeff) mo_e = numpy.empty(mo_occ.size) for idx in (coreidx, openidx, viridx): if numpy.count_nonzero(idx) > 0: orb = mo_coeff[:,idx] f1 = reduce(numpy.dot, (orb.conj().T, fock, orb)) e, c = scipy.linalg.eigh(f1) mo[:,idx] = numpy.dot(orb, c) mo_e[idx] = e return mo_e, mo
Example 10
Project: pyscf Author: pyscf File: hf_symm.py License: Apache License 2.0 | 6 votes |
def _dump_mo_energy(mol, mo_energy, mo_occ, ehomo, elumo, orbsym, title='', verbose=logger.DEBUG): log = logger.new_logger(mol, verbose) 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 HOMO = %.15g', title, irname, nocc, e_ir[nocc-1]) else: log.debug('%s%s nocc = %d HOMO = %.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 HOMO %.15g > system LUMO %.15g', title, irname, e_ir[nocc-1], elumo) if e_ir[nocc] < ehomo+1e-3: log.warn('%s%s LUMO %.15g < system HOMO %.15g', title, irname, e_ir[nocc], ehomo) log.debug(' mo_energy = %s', e_ir)
Example 11
Project: pyscf Author: pyscf File: addons.py License: Apache License 2.0 | 6 votes |
def float_occ_(mf): ''' For UHF, allowing the Sz value being changed during SCF iteration. Determine occupation of alpha and beta electrons based on energy spectrum ''' from pyscf.scf import uhf assert(isinstance(mf, uhf.UHF)) def get_occ(mo_energy, mo_coeff=None): mol = mf.mol ee = numpy.sort(numpy.hstack(mo_energy)) n_a = numpy.count_nonzero(mo_energy[0]<(ee[mol.nelectron-1]+1e-3)) n_b = mol.nelectron - n_a if mf.nelec is None: nelec = mf.mol.nelec else: nelec = mf.nelec if n_a != nelec[0]: logger.info(mf, 'change num. alpha/beta electrons ' ' %d / %d -> %d / %d', nelec[0], nelec[1], n_a, n_b) mf.nelec = (n_a, n_b) return uhf.UHF.get_occ(mf, mo_energy, mo_coeff) mf.get_occ = get_occ return mf
Example 12
Project: pyscf Author: pyscf File: test_rhf.py License: Apache License 2.0 | 6 votes |
def test_uniq_var(self): mo_occ = mf.mo_occ.copy() nmo = mo_occ.size nocc = numpy.count_nonzero(mo_occ > 0) nvir = nmo - nocc numpy.random.seed(1) f = numpy.random.random((nmo,nmo)) f_uniq = scf.hf.pack_uniq_var(f, mo_occ) self.assertEqual(f_uniq.size, nocc*nvir) f1 = scf.hf.unpack_uniq_var(f_uniq, mo_occ) self.assertAlmostEqual(abs(f1 + f1.T).max(), 0, 12) mo_occ[4:7] = 1 ndocc = 4 nocc = 7 f_uniq = scf.hf.pack_uniq_var(f, mo_occ) self.assertEqual(f_uniq.size, nocc*(nmo-ndocc)-(nocc-ndocc)**2) f1 = scf.hf.unpack_uniq_var(f_uniq, mo_occ) self.assertAlmostEqual(abs(f1 + f1.T).max(), 0, 12)
Example 13
Project: pyscf Author: pyscf File: mp2.py License: Apache License 2.0 | 6 votes |
def get_nocc(mp): if mp._nocc is not None: return mp._nocc elif mp.frozen is None: nocc = numpy.count_nonzero(mp.mo_occ > 0) assert(nocc > 0) return nocc elif isinstance(mp.frozen, (int, numpy.integer)): nocc = numpy.count_nonzero(mp.mo_occ > 0) - mp.frozen assert(nocc > 0) return nocc elif isinstance(mp.frozen[0], (int, numpy.integer)): occ_idx = mp.mo_occ > 0 occ_idx[list(mp.frozen)] = False nocc = numpy.count_nonzero(occ_idx) assert(nocc > 0) return nocc else: raise NotImplementedError
Example 14
Project: pyscf Author: pyscf File: linalg_helper.py License: Apache License 2.0 | 6 votes |
def pick_real_eigs(w, v, nroots, envs): '''This function searchs the real eigenvalues or eigenvalues with small imaginary component. ''' threshold = 1e-3 abs_imag = abs(w.imag) # Grab `nroots` number of e with small(est) imaginary components max_imag_tol = max(threshold, numpy.sort(abs_imag)[min(w.size,nroots)-1]) real_idx = numpy.where((abs_imag <= max_imag_tol))[0] nbelow_thresh = numpy.count_nonzero(abs_imag[real_idx] < threshold) if nbelow_thresh < nroots and w.size >= nroots: warnings.warn('Only %d eigenvalues (out of %3d requested roots) with imaginary part < %4.3g.\n' % (nbelow_thresh, min(w.size,nroots), threshold)) # Guess whether the matrix to diagonalize is real or complex if envs.get('dtype') == numpy.double: w, v, idx = _eigs_cmplx2real(w, v, real_idx, real_eigenvectors=True) else: w, v, idx = _eigs_cmplx2real(w, v, real_idx, real_eigenvectors=False) return w, v, idx
Example 15
Project: pyscf Author: pyscf File: linalg_helper.py License: Apache License 2.0 | 6 votes |
def _sort_by_similarity(w, v, nroots, conv, vlast, emin=None, heff=None): if not any(conv) or vlast is None: return w[:nroots], v[:,:nroots] head, nroots = vlast.shape conv = numpy.asarray(conv[:nroots]) ovlp = vlast[:,conv].T.conj().dot(v[:head]) ovlp = numpy.einsum('ij,ij->j', ovlp, ovlp) nconv = numpy.count_nonzero(conv) nleft = nroots - nconv idx = ovlp.argsort() sorted_idx = numpy.zeros(nroots, dtype=int) sorted_idx[conv] = numpy.sort(idx[-nconv:]) sorted_idx[~conv] = numpy.sort(idx[:-nconv])[:nleft] e = w[sorted_idx] c = v[:,sorted_idx] return e, c
Example 16
Project: pyscf Author: pyscf File: gcisd.py License: Apache License 2.0 | 6 votes |
def from_ucisdvec(civec, nocc, orbspin): '''Convert the (spin-separated) CISD coefficient vector to GCISD coefficient vector''' nmoa = numpy.count_nonzero(orbspin == 0) nmob = numpy.count_nonzero(orbspin == 1) if isinstance(nocc, int): nocca = numpy.count_nonzero(orbspin[:nocc] == 0) noccb = numpy.count_nonzero(orbspin[:nocc] == 1) else: nocca, noccb = nocc nvira = nmoa - nocca if civec.size == nocca*nvira + (nocca*nvira)**2 + 1: # RCISD c0, c1, c2 = cisd.cisdvec_to_amplitudes(civec, nmoa, nocca) else: # UCISD c0, c1, c2 = ucisd.cisdvec_to_amplitudes(civec, (nmoa,nmob), (nocca,noccb)) c1 = spatial2spin(c1, orbspin) c2 = spatial2spin(c2, orbspin) return amplitudes_to_cisdvec(c0, c1, c2)
Example 17
Project: pyscf Author: pyscf File: gcisd.py License: Apache License 2.0 | 6 votes |
def to_fcivec(cisdvec, nelec, orbspin, frozen=None): assert(numpy.count_nonzero(orbspin == 0) == numpy.count_nonzero(orbspin == 1)) norb = len(orbspin) frozen_mask = numpy.zeros(norb, dtype=bool) if frozen is None: pass elif isinstance(frozen, (int, numpy.integer)): frozen_mask[:frozen] = True else: frozen_mask[frozen] = True frozen = (numpy.where(frozen_mask[orbspin == 0])[0], numpy.where(frozen_mask[orbspin == 1])[0]) nelec = (numpy.count_nonzero(orbspin[:nelec] == 0), numpy.count_nonzero(orbspin[:nelec] == 1)) orbspin = orbspin[~frozen_mask] nmo = len(orbspin) nocc = numpy.count_nonzero(~frozen_mask[:sum(nelec)]) ucisdvec = to_ucisdvec(cisdvec, nmo, nocc, orbspin) return ucisd.to_fcivec(ucisdvec, norb//2, nelec, frozen)
Example 18
Project: pyscf Author: pyscf File: gcisd.py License: Apache License 2.0 | 6 votes |
def from_fcivec(ci0, nelec, orbspin, frozen=None): if not (frozen is None or frozen == 0): raise NotImplementedError assert(numpy.count_nonzero(orbspin == 0) == numpy.count_nonzero(orbspin == 1)) norb = len(orbspin) frozen_mask = numpy.zeros(norb, dtype=bool) if frozen is None: pass elif isinstance(frozen, (int, numpy.integer)): frozen_mask[:frozen] = True else: frozen_mask[frozen] = True #frozen = (numpy.where(frozen_mask[orbspin == 0])[0], # numpy.where(frozen_mask[orbspin == 1])[0]) nelec = (numpy.count_nonzero(orbspin[:nelec] == 0), numpy.count_nonzero(orbspin[:nelec] == 1)) ucisdvec = ucisd.from_fcivec(ci0, norb//2, nelec, frozen) nocc = numpy.count_nonzero(~frozen_mask[:sum(nelec)]) return from_ucisdvec(ucisdvec, nocc, orbspin[~frozen_mask])
Example 19
Project: pyscf Author: pyscf File: test_tdrks.py License: Apache License 2.0 | 6 votes |
def test_ab_hf(self): mf = scf.RHF(mol).run() a, b = rhf.get_ab(mf) fock = mf.get_hcore() + mf.get_veff() ftda = rhf.gen_tda_operation(mf, fock, singlet=True)[0] ftdhf = rhf.gen_tdhf_operation(mf, singlet=True)[0] nocc = numpy.count_nonzero(mf.mo_occ == 2) nvir = numpy.count_nonzero(mf.mo_occ == 0) numpy.random.seed(2) x, y = xy = numpy.random.random((2,nocc,nvir)) ax = numpy.einsum('iajb,jb->ia', a, x) self.assertAlmostEqual(abs(ax - ftda([x]).reshape(nocc,nvir)).max(), 0, 6) ab1 = ax + numpy.einsum('iajb,jb->ia', b, y) ab2 =-numpy.einsum('iajb,jb->ia', b, x) ab2-= numpy.einsum('iajb,jb->ia', a, y) abxy_ref = ftdhf([xy]).reshape(2,nocc,nvir) self.assertAlmostEqual(abs(ab1 - abxy_ref[0]).max(), 0, 9) self.assertAlmostEqual(abs(ab2 - abxy_ref[1]).max(), 0, 9)
Example 20
Project: pyscf Author: pyscf File: test_tdrks.py License: Apache License 2.0 | 6 votes |
def test_ab_lda(self): mf = mf_lda a, b = rhf.get_ab(mf) ftda = rhf.gen_tda_operation(mf, singlet=True)[0] ftdhf = rhf.gen_tdhf_operation(mf, singlet=True)[0] nocc = numpy.count_nonzero(mf.mo_occ == 2) nvir = numpy.count_nonzero(mf.mo_occ == 0) numpy.random.seed(2) x, y = xy = numpy.random.random((2,nocc,nvir)) ax = numpy.einsum('iajb,jb->ia', a, x) self.assertAlmostEqual(abs(ax - ftda([x]).reshape(nocc,nvir)).max(), 0, 9) ab1 = ax + numpy.einsum('iajb,jb->ia', b, y) ab2 =-numpy.einsum('iajb,jb->ia', b, x) ab2-= numpy.einsum('iajb,jb->ia', a, y) abxy_ref = ftdhf([xy]).reshape(2,nocc,nvir) self.assertAlmostEqual(abs(ab1 - abxy_ref[0]).max(), 0, 9) self.assertAlmostEqual(abs(ab2 - abxy_ref[1]).max(), 0, 9)
Example 21
Project: pyscf Author: pyscf File: ccsd_slow.py License: Apache License 2.0 | 6 votes |
def __init__(self, cc, mo_coeff): nocc = numpy.count_nonzero(cc.mo_occ > 0) eri0 = ao2mo.full(cc._scf._eri, mo_coeff) eri0 = ao2mo.restore(1, eri0, mo_coeff.shape[1]) eri0 = eri0.reshape((mo_coeff.shape[1],)*4) self.oooo = eri0[:nocc,:nocc,:nocc,:nocc].copy() self.ooov = eri0[:nocc,:nocc,:nocc,nocc:].copy() self.ovoo = eri0[:nocc,nocc:,:nocc,:nocc].copy() self.oovo = eri0[:nocc,:nocc,nocc:,:nocc].copy() self.oovv = eri0[:nocc,:nocc,nocc:,nocc:].copy() self.ovov = eri0[:nocc,nocc:,:nocc,nocc:].copy() self.ovvo = eri0[:nocc,nocc:,nocc:,:nocc].copy() self.ovvv = eri0[:nocc,nocc:,nocc:,nocc:].copy() self.vvvv = eri0[nocc:,nocc:,nocc:,nocc:].copy() self.vvvo = eri0[nocc:,nocc:,nocc:,:nocc].copy() self.vovv = eri0[nocc:,:nocc,nocc:,nocc:].copy() self.vvov = eri0[nocc:,nocc:,:nocc,nocc:].copy() self.vvoo = eri0[nocc:,nocc:,:nocc,:nocc].copy() self.voov = eri0[nocc:,:nocc,:nocc,nocc:].copy() self.vooo = eri0[nocc:,:nocc,:nocc,:nocc].copy() self.mo_coeff = mo_coeff self.fock = numpy.diag(cc._scf.mo_energy)
Example 22
Project: pyscf Author: pyscf File: ccsd_slow.py License: Apache License 2.0 | 6 votes |
def _gamma1_intermediates(cc, t1, t2, l1, l2): d1 = ccsd_rdm._gamma1_intermediates(cc, t1, t2, l1, l2) if cc.frozen is None: return d1 nocc = numpy.count_nonzero(cc.mo_occ>0) nvir = cc.mo_occ.size - nocc OA, VA, OF, VF = index_frozen_active(cc) doo = numpy.zeros((nocc,nocc)) dov = numpy.zeros((nocc,nvir)) dvo = numpy.zeros((nvir,nocc)) dvv = numpy.zeros((nvir,nvir)) doo[OA[:,None],OA] = d1[0] dov[OA[:,None],VA] = d1[1] dvo[VA[:,None],OA] = d1[2] dvv[VA[:,None],VA] = d1[3] return doo, dov, dvo, dvv
Example 23
Project: pyscf Author: pyscf File: rhf.py License: Apache License 2.0 | 6 votes |
def make_pso(sscobj, mol, mo1, mo_coeff, mo_occ, nuc_pair=None): if nuc_pair is None: nuc_pair = sscobj.nuc_pair para = [] nocc = numpy.count_nonzero(mo_occ> 0) nvir = numpy.count_nonzero(mo_occ==0) atm1lst = sorted(set([i for i,j in nuc_pair])) atm2lst = sorted(set([j for i,j in nuc_pair])) atm1dic = dict([(ia,k) for k,ia in enumerate(atm1lst)]) atm2dic = dict([(ia,k) for k,ia in enumerate(atm2lst)]) mo1 = mo1.reshape(len(atm1lst),3,nvir,nocc) h1 = make_h1_pso(mol, mo_coeff, mo_occ, atm1lst) h1 = numpy.asarray(h1).reshape(len(atm1lst),3,nvir,nocc) for i,j in nuc_pair: # PSO = -Tr(Im[h1_ov], Im[mo1_vo]) + cc = 2 * Tr(Im[h1_vo], Im[mo1_vo]) e = numpy.einsum('xij,yij->xy', h1[atm1dic[i]], mo1[atm2dic[j]]) para.append(e*4) # *4 for +c.c. and double occupnacy return numpy.asarray(para) * nist.ALPHA**4
Example 24
Project: pyscf Author: pyscf File: dhf.py License: Apache License 2.0 | 6 votes |
def make_para(sscobj, mol, mo1, mo_coeff, mo_occ, nuc_pair=None): if nuc_pair is None: nuc_pair = sscobj.nuc_pair if sscobj.mb.upper().startswith('ST'): nmo = mo_occ.size mo_coeff = mo_coeff[:,nmo//2:] mo_occ = mo_occ[nmo//2:] nocc = numpy.count_nonzero(mo_occ> 0) nvir = numpy.count_nonzero(mo_occ==0) atm1lst = sorted(set([i for i,j in nuc_pair])) atm2lst = sorted(set([j for i,j in nuc_pair])) atm1dic = dict([(ia,k) for k,ia in enumerate(atm1lst)]) atm2dic = dict([(ia,k) for k,ia in enumerate(atm2lst)]) mo1 = mo1.reshape(len(atm1lst),3,nvir,nocc) h1 = make_h1(mol, mo_coeff, mo_occ, atm1lst) h1 = numpy.asarray(h1).reshape(len(atm1lst),3,nvir,nocc) para = [] for i,j in nuc_pair: e = numpy.einsum('xij,yij->xy', h1[atm1dic[i]], mo1[atm2dic[j]].conj()) * 2 para.append(e.real) return numpy.asarray(para) * nist.ALPHA**4
Example 25
Project: fuku-ml Author: fukuball File: SupportVectorMachine.py License: MIT License | 5 votes |
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 26
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: metric.py License: Apache License 2.0 | 5 votes |
def update(self, labels, preds): mx.metric.check_label_shapes(labels, preds) label_weight = labels[0].asnumpy() preds = preds[0].asnumpy() tmp = [] for i in range(preds.shape[0]): tmp.append((label_weight[i], preds[i])) tmp = sorted(tmp, key=itemgetter(1), reverse=True) label_sum = label_weight.sum() if label_sum == 0 or label_sum == label_weight.size: raise Exception("AUC with one class is undefined") label_one_num = np.count_nonzero(label_weight) label_zero_num = len(label_weight) - label_one_num total_area = label_zero_num * label_one_num height = 0 width = 0 area = 0 for a, _ in tmp: if a == 1.0: height += 1.0 else: width += 1.0 area += height self.sum_metric += area / total_area self.num_inst += 1
Example 27
Project: soccer-matlab Author: utra-robosoccer File: robot_locomotors.py License: BSD 2-Clause "Simplified" License | 5 votes |
def calc_state(self): j = np.array([j.current_relative_position() for j in self.ordered_joints], dtype=np.float32).flatten() # even elements [0::2] position, scaled to -1..+1 between limits # odd elements [1::2] angular speed, scaled to show -1..+1 self.joint_speeds = j[1::2] self.joints_at_limit = np.count_nonzero(np.abs(j[0::2]) > 0.99) body_pose = self.robot_body.pose() parts_xyz = np.array([p.pose().xyz() for p in self.parts.values()]).flatten() self.body_xyz = ( parts_xyz[0::3].mean(), parts_xyz[1::3].mean(), body_pose.xyz()[2]) # torso z is more informative than mean z self.body_rpy = body_pose.rpy() z = self.body_xyz[2] if self.initial_z == None: self.initial_z = z r, p, yaw = self.body_rpy self.walk_target_theta = np.arctan2(self.walk_target_y - self.body_xyz[1], self.walk_target_x - self.body_xyz[0]) self.walk_target_dist = np.linalg.norm( [self.walk_target_y - self.body_xyz[1], self.walk_target_x - self.body_xyz[0]]) angle_to_target = self.walk_target_theta - yaw rot_speed = np.array( [[np.cos(-yaw), -np.sin(-yaw), 0], [np.sin(-yaw), np.cos(-yaw), 0], [ 0, 0, 1]] ) vx, vy, vz = np.dot(rot_speed, self.robot_body.speed()) # rotate speed back to body point of view more = np.array([ z-self.initial_z, np.sin(angle_to_target), np.cos(angle_to_target), 0.3* vx , 0.3* vy , 0.3* vz , # 0.3 is just scaling typical speed into -1..+1, no physical sense here r, p], dtype=np.float32) return np.clip( np.concatenate([more] + [j] + [self.feet_contact]), -5, +5)
Example 28
Project: fine-lm Author: akzaidi File: train.py License: MIT License | 5 votes |
def add_noise_python(words, dropout=0.1, k=3): """Applies the noise model in input words. Args: words: A numpy vector of word ids. dropout: The probability to drop words. k: Maximum distance of the permutation. Returns: A noisy numpy vector of word ids. """ def _drop_words(words, probability): """Drops words with the given probability.""" length = len(words) keep_prob = np.random.uniform(size=length) keep = np.random.uniform(size=length) > probability if np.count_nonzero(keep) == 0: ind = np.random.randint(0, length) keep[ind] = True words = np.take(words, keep.nonzero())[0] return words def _rand_perm_with_constraint(words, k): """Randomly permutes words ensuring that words are no more than k positions away from their original position.""" length = len(words) offset = np.random.uniform(size=length) * (k + 1) new_pos = np.arange(length) + offset return np.take(words, np.argsort(new_pos)) words = _drop_words(words, dropout) words = _rand_perm_with_constraint(words, k) return words
Example 29
Project: OpenCV-Computer-Vision-Projects-with-Python Author: PacktPublishing File: classifiers.py License: MIT License | 5 votes |
def _precision(self, y_test, Y_vote): """Calculates precision This method calculates precision extended to multi-class classification by help of a confusion matrix. :param y_test: vector of ground-truth labels :param Y_vote: 2D voting matrix (rows=samples, cols=class votes) :returns: precision e[0,1] """ # predicted classes y_hat = np.argmax(Y_vote, axis=1) if self.mode == "one-vs-one": # need confusion matrix conf = self._confusion(y_test, Y_vote) # consider each class separately prec = np.zeros(self.num_classes) for c in xrange(self.num_classes): # true positives: label is c, classifier predicted c tp = conf[c, c] # false positives: label is c, classifier predicted not c fp = np.sum(conf[:, c]) - conf[c, c] if tp + fp != 0: prec[c] = tp * 1. / (tp + fp) elif self.mode == "one-vs-all": # consider each class separately prec = np.zeros(self.num_classes) for c in xrange(self.num_classes): # true positives: label is c, classifier predicted c tp = np.count_nonzero((y_test == c) * (y_hat == c)) # false positives: label is c, classifier predicted not c fp = np.count_nonzero((y_test == c) * (y_hat != c)) if tp + fp != 0: prec[c] = tp * 1. / (tp + fp) return prec
Example 30
Project: OpenCV-Computer-Vision-Projects-with-Python Author: PacktPublishing File: classifiers.py License: MIT License | 5 votes |
def _recall(self, y_test, Y_vote): """Calculates recall This method calculates recall extended to multi-class classification by help of a confusion matrix. :param y_test: vector of ground-truth labels :param Y_vote: 2D voting matrix (rows=samples, cols=class votes) :returns: recall e[0,1] """ # predicted classes y_hat = np.argmax(Y_vote, axis=1) if self.mode == "one-vs-one": # need confusion matrix conf = self._confusion(y_test, Y_vote) # consider each class separately recall = np.zeros(self.num_classes) for c in xrange(self.num_classes): # true positives: label is c, classifier predicted c tp = conf[c, c] # false negatives: label is not c, classifier predicted c fn = np.sum(conf[c, :]) - conf[c, c] if tp + fn != 0: recall[c] = tp * 1. / (tp + fn) elif self.mode == "one-vs-all": # consider each class separately recall = np.zeros(self.num_classes) for c in xrange(self.num_classes): # true positives: label is c, classifier predicted c tp = np.count_nonzero((y_test == c) * (y_hat == c)) # false negatives: label is not c, classifier predicted c fn = np.count_nonzero((y_test != c) * (y_hat == c)) if tp + fn != 0: recall[c] = tp * 1. / (tp + fn) return recall