當前位置: 首頁>>代碼示例>>Python>>正文


Python collections.Counter類代碼示例

本文整理匯總了Python中collections.Counter的典型用法代碼示例。如果您正苦於以下問題:Python Counter類的具體用法?Python Counter怎麽用?Python Counter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Counter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: update

    def update(self, *args, **kwargs):
        """D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
        If E present, does:  for k in E: D[k] = E[k]
        This is followed by: for k in F: D[k] = F[k]

        **Example:**

        >>> e = Exponents()
        >>> e.update(a=1)
        >>> e.update(dict(b=2), c=3)
        >>> e.update('1/d')
        >>> e # doctest: +SKIP
        Exponents({'a': 1, 'b': 2, 'c': 3, 'd': -1})

        .. testcleanup::
           >>> assert e == dict(a=1, b=2, c=3, d=-1)
        """
        try:
            # Assume args[0] is a string.
            arg = args[0].replace(' ', '')  # Remove spaces.
        except (IndexError, AttributeError):
            Counter.update(self, *args, **kwargs)
        else:
            if len(args) > 1:
                raise TypeError("update expected at most 1 arguments, got %i"
                                % len(args))
            Counter.update(self, Exponents.fromstr(arg), **kwargs)
開發者ID:joewa,項目名稱:natu,代碼行數:27,代碼來源:exponents.py

示例2: __setitem__

    def __setitem__(self, item, val):
        if len(self) > self._max * 2:
            print("Purging")
            to_delete = self.most_common()[self._max:]
            for ii in to_delete:
                del self[ii]

        Counter.__setitem__(self, item, val)
開發者ID:jankim,項目名稱:qb,代碼行數:8,代碼來源:inspect_features.py

示例3: __init__

 def __init__(self, _dict=None, **kwargs):
     if _dict is None:
         self.__init__(kwargs)
     else:
         if not all(isinstance(x, Rational) for x in _dict.values()):
             raise TypeError("powers of dimensions must be rational")
         Counter.__init__(self, _dict)
         self.clean()
開發者ID:speezepearson,項目名稱:units,代碼行數:8,代碼來源:dimensions.py

示例4: __init__

 def __init__(self, n, *args, **kwargs):
     assert n>=1
     self._size = n
     self._worst = None
     Counter.__init__(self, *args, **kwargs)
     if len(self)>=n:
         self._worst = self.most_common(n)[-1]
         for k,v in self.most_common()[n:]:
             del self[k]
開發者ID:chenguandan,項目名稱:pysupersensetagger,代碼行數:9,代碼來源:beam.py

示例5: __setitem__

 def __setitem__(self, k, v):
     if k in self:
         assert self[k]==v
         return
     if self._worst is not None: # the beam is full
         if v<=self._worst[1]:
             return
         else:
             del self[self._worst[0]]
             
     Counter.__setitem__(self, k, v)
     assert self._size>=len(self)
     if self._size==len(self):
         self._worst = min(self.items(), key=lambda (k1,v1): v1)
開發者ID:chenguandan,項目名稱:pysupersensetagger,代碼行數:14,代碼來源:beam.py

示例6: addFakeData

def addFakeData(oData,oLabels,count=100,low=10):
	data = oData[:]
	labels = oLabels[:]
	for iafsa in range(count):
		c = Counter(chain(*labels))
		lc = Counter.most_common(c)
	
		dlc = {}
		for l in lc: dlc[l[0]] = l[1]
	
		#teze = [sum([ dlc[y]**2 for y in x])  for x in labels]
		teze = [sum([ dlc[y] for y in x])  for x in labels]
		teze = sorted([(y,x) for x,y in enumerate(teze)])
		tt = teze[:max(low*10,200)]
		shuffle(tt)
		duplicate = [x[1] for x in tt[:low]]
		dLabels = [labels[i][:] for i in duplicate]
		dData = [data[i][:] for i in duplicate]
		for ii in range(1):
			for i in range(len(duplicate)):
				labels.append(dLabels[i])
				data.append(dData[i])
	#shuflamo vrstice da niso vec lepo, pa poskrbimo da labele ostanejo 
	#pri svojem primeru
	sd = []
	[sd.append((data[i],labels[i])) for i in xrange(len(data))]
	shuffle(sd)
	ll = []
	dd = []
	for x,y in sd:
		dd.append(x)
		ll.append(y)
	return (dd, ll)
開發者ID:zidarsk8,項目名稱:dataMining,代碼行數:33,代碼來源:data.py

示例7: wanabeknn

def wanabeknn(k=15):
	from collections import Counter
	ftrd = open("minidata/trainingData.csv")
	fted = open("minidata/testData.csv")
	flab = open("minidata/trainingLabels.csv")

	lab = [[int(j) for j in i.strip().split(",")]  for i in flab.readlines()]
	trd = [[int(j) for j in i.strip().split("\t")] for i in ftrd.readlines()]
	ted = [[int(j) for j in i.strip().split("\t")] for i in fted.readlines()]

	def dist(a,b): return sum([min(a[i], b[i]) for i in xrange(len(a))])

	rez = []
	for v in ted:
		print "hurej  %4d   %3d" % ( len(rez),len(rez[-1:]))
		t = []
		for trindex, train in enumerate(trd):
			t.append((dist(train, v), trindex))
		tt = sorted(t, reverse=True)
		ll = []
		for i in range(k): ll += lab[tt[i][1]]
		n = len(ll)
		for i in range(k/3): ll += lab[tt[i][1]]
		rez.append([x[0] for x in Counter.most_common(Counter(ll),n/k)])
		print rez
	cPickle.dump(rez, file("rezPickled/wnbknn%d.pickled" % k, "w"), -1)
開發者ID:eagle12td,項目名稱:dataMining,代碼行數:26,代碼來源:main.py

示例8: __init__

    def __init__(self, max_len, N_max = 5):
        '''
        @summary: NGramSet Constructor

        @param max_len: 最優序列長度
        @param N_max: CCS2012 對應最大n-gram

        '''
        Counter.__init__(self)
        
        self.N_max = N_max
        self.max_len = max_len
        
        self.alphabet_size = 0    # 記錄項總數
        self.all_record_num = 0   # 記錄數據集中記錄總數
        self.TERM = 0             # 序列結束符
開發者ID:luguoqing,項目名稱:Diff-FSPM,代碼行數:16,代碼來源:NGramSet.py

示例9: _most_preferred

 def _most_preferred(self, alternatives):
     """Applies funcnamei from each trait to the alternatives and return the most preferred."""
     prefs = [y for y in [getattr(x, funcnamei)(alternatives) for x in self.traits] if y is not None]
     if not prefs:
         return None
     if len(prefs) == 1:
         return prefs[0]
     return Counter.most_common(Counter(prefs), 1)[0][0]
開發者ID:Mitten-O,項目名稱:freeorion,代碼行數:8,代碼來源:character_module.py

示例10: items

 def items(self, relative=False):
     """ Returns a list of (key, value)-tuples sorted by value, highest-first.
         With relative=True, the sum of values is 1.0.
     """
     a = Counter.most_common(self)
     if relative:
         n = sum(v for k, v in a) or 1.
         a = [(k, v / n) for v, k in a]
     return a
開發者ID:markus-beuckelmann,項目名稱:pattern,代碼行數:9,代碼來源:metrics.py

示例11: __len__

 def __len__(self):
     '''Returns the total number of members, excluding repeated elements.
     
     >>> m = multiset('abb')
     >>> len(m)
     2
     
     '''
     return Counter.__len__(self)
開發者ID:jvb,項目名稱:infobiotics-dashboard,代碼行數:9,代碼來源:multiset.py

示例12: most_common_viz

def most_common_viz(output_dir: str, ints: collections.Counter) -> None:
    df = pd.DataFrame(ints.most_common(), columns=["Integer", "Frequency"])

    with open(os.path.join(output_dir, 'index.html'), 'w') as fh:
        fh.write('<html><body>\n')
        fh.write('<h3>Most common integers:</h3>\n')
        fh.write(df.to_html(index=False))
        fh.write('</body></html>')

    with open(os.path.join(output_dir, 'index.tsv'), 'w') as fh:
        fh.write(df.to_csv(sep='\t', index=False))
開發者ID:jairideout,項目名稱:qiime2,代碼行數:11,代碼來源:visualizer.py

示例13: __add__

    def __add__(self, other):
        """Add together, with recursion.  The basic idea is that the set of keys
        should be added together and then recurse the addition to the values where keys are shared
        otherwise just add the value.

        >>> m, m2 = Mdict('a'), Mdict('abb')
        >>> m + m2 == {'a':2, 'b':2}
        True
        >>> m + 1 == {None:1, 'a':1}
        True
        >>> m['a'] = m2  #now fractal
        >>> m == {'a': {'a':1, 'b':2}}
        True
        >>> m + m + 1 == {'a': {'a':2, 'b':4}, None: 1}
        True
        """
        try: # need to add (union) of keys, and then recurse into values of common keys
            return Mdict(Counter.__add__(self, other)) #Counter.__add__(self, other)
        except TypeError: #must've been an integer for other
            return Mdict(Counter.__add__(self, Counter({None: other})))
開發者ID:theProphet,項目名稱:Pangaia,代碼行數:20,代碼來源:metadict.py

示例14: removeLeastCommonData

def removeLeastCommonData(oData, oLabels, least=5):
	data = oData[:]
	labels = oLabels[:]
	c = Counter(chain(*labels))
	lc = Counter.most_common(c)
	bb = sorted(list(Set([j for i,j in lc])))
	a = [x[0] for x in lc if x[1] < bb[5]]
	rem = [i for i,j in enumerate(labels) if len(Set(j).intersection(Set(a))) > 0 ]
	[labels.pop(x) for x in sorted(rem, reverse=True)]
	[data.pop(x) for x in sorted(rem, reverse=True)]
	return (data, labels)
開發者ID:zidarsk8,項目名稱:dataMining,代碼行數:11,代碼來源:data.py

示例15: removeMostCommonData

def removeMostCommonData(oData, oLabels, count=20):
	data = oData[:]
	labels = oLabels[:]
	for iafsa in range(count):
		c = Counter(chain(*labels))
		lc = Counter.most_common(c)
		dlc = {}
		for l in lc: dlc[l[0]] = l[1]
		teze = [max([ dlc[y] for y in x])  for x in labels]
		teze = sorted([(y,x) for x,y in enumerate(teze)])
		rem = [x[1] for x in teze[-10:]]
		[labels.pop(x) for x in sorted(rem, reverse=True)]
		[data.pop(x) for x in sorted(rem, reverse=True)]
	return (data, labels)
開發者ID:zidarsk8,項目名稱:dataMining,代碼行數:14,代碼來源:data.py


注:本文中的collections.Counter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。