本文整理汇总了Python中collections.Counter.most_common方法的典型用法代码示例。如果您正苦于以下问题:Python Counter.most_common方法的具体用法?Python Counter.most_common怎么用?Python Counter.most_common使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.Counter
的用法示例。
在下文中一共展示了Counter.most_common方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wanabeknn
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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)
示例2: addFakeData
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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)
示例3: _most_preferred
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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]
示例4: items
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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
示例5: most_common_viz
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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))
示例6: removeLeastCommonData
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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)
示例7: removeMostCommonData
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
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)
示例8: Counter
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
# print Counter(foundEmosAndSmilies)
# print("found smilies top50")
# print Counter.most_common(Counter(foundEmosAndSmilies), 50)
# print("total number of sentences with smilies:")
# print numOfTotalSentences
"""
Classify
"""
for message in messages:
messagecounter += 1
# for every sentence
score, words = emoCount.score(message)
foundEmosAndSmilies = foundEmosAndSmilies + words
if len(words) != 0:
numOfTotalSentences += 1
print(words)
# in the end
print("Messages total:")
print messagecounter
print("found smilies total:")
print sum(Counter(foundEmosAndSmilies).values())
print("found smilies examples ordered:")
print Counter(foundEmosAndSmilies)
print("found smilies top50")
print Counter.most_common(Counter(foundEmosAndSmilies), 50)
print("total number of sentences with smilies:")
print numOfTotalSentences
示例9: most_common
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
def most_common(self, n, conts):
"""Returns most frequent word"""
return Counter.most_common(conts)
示例10: Counter
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
from collections import Counter
text = "In February 2014, I made a recommendation to my co - founders at" \
"Ballistiq that I wanted to cancel development of ArtStation." \
"The project was in development hell. It wasn’t going anywhere." \
"I was unhappy with it and just couldn’t see a path for it to be a"\
"successful product. Two months later we managed to launch it," \
"and two years later it is the leading network for professional games."
words = text.split()
Counter = Counter(words)
top_three = Counter.most_common(3)
print(top_three)
示例11: open
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
from collections import Counter
hairy = open("RFand1R-2-2")
majcn = open("resultRF-0.72388.csv")
smotko = open("smotkoTopScore.csv")
zidar = open("result-0.36027.csv")
aaa = open("resultRF-0.85714.csv")
a = []
a.append([[int(j) for j in i.strip().split(" ")] for i in majcn.readlines()])
a.append([[int(j) for j in i.strip().split(" ")] for i in zidar.readlines()])
a.append([[int(j) for j in i.strip().split(" ")] for i in smotko.readlines()])
a.append([[int(j) for j in i.strip().split(" ")] for i in hairy.readlines()])
a.append([[int(j) for j in i.strip().split(" ")] for i in aaa.readlines()])
b = []
for i in xrange(len(a[0])):
c = Counter(a[0][i]+a[1][i]+a[2][i]+a[3][i]+a[4][i])
b.append([x[0] for x in Counter.most_common(c) if x[1]>1])
if len(x)==0 :
x = [40, 44, 18, 62, 41]
print "aa"
for i in b:
if len(i) == 1:
print "aaa"
print "bb"
f = file("RFand1R-2-2","w")
f.write("\n".join( [" ".join([str(x) for x in i]) for i in b ] ))
f.flush()
f.close()
示例12: open
# 需要导入模块: from collections import Counter [as 别名]
# 或者: from collections.Counter import most_common [as 别名]
from collections import Counter
files = [open("knn.txt"), open("knnm.txt"), open("RF200por.txt"), open("test.txt")]
a = []
for f in files:
a.append([[int(j) for j in i.strip().split(" ")] for i in f.readlines()])
b = []
for i in xrange(len(a[0])):
c = Counter(a[0][i]+a[1][i]+a[2][i])
x = [x[0] for x in Counter.most_common(c) if x[1]>2]
if len(x)==0 :
x = [40]
b.append(x)
print "done"
f = file("skupniNoTest.csv","w")
f.write("\n".join( [" ".join([str(x) for x in i]) for i in b ] ))
f.close()