comparison trafficintelligence/utils.py @ 1034:4069d8545922

updated mostCommong function
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 20 Jun 2018 12:04:22 -0400
parents 045cb04ad7b8
children 16575ca4537d
comparison
equal deleted inserted replaced
1033:8ffb3ae9f3d2 1034:4069d8545922
4 from datetime import time, datetime 4 from datetime import time, datetime
5 from argparse import ArgumentTypeError 5 from argparse import ArgumentTypeError
6 from pathlib import Path 6 from pathlib import Path
7 from math import sqrt, ceil, floor 7 from math import sqrt, ceil, floor
8 from copy import deepcopy, copy 8 from copy import deepcopy, copy
9 from collections import Counter
9 10
10 from scipy.stats import rv_continuous, kruskal, shapiro, lognorm, norm, t 11 from scipy.stats import rv_continuous, kruskal, shapiro, lognorm, norm, t
11 from scipy.spatial import distance 12 from scipy.spatial import distance
12 from scipy.sparse import dok_matrix 13 from scipy.sparse import dok_matrix
13 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit, float as npfloat 14 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, mean, median, percentile, isnan, ones, convolve, dtype, isnan, NaN, ma, isinf, savez, load as npload, log, polyfit, float as npfloat
745 746
746 ######################### 747 #########################
747 # iterable section 748 # iterable section
748 ######################### 749 #########################
749 750
750 def mostCommon(L): 751 def mostCommon(l):
751 '''Returns the most frequent element in a iterable 752 '''Returns the most frequent element in a iterable
752 753 The element must be hashable
753 taken from http://stackoverflow.com/questions/1518522/python-most-common-element-in-a-list''' 754
754 from itertools import groupby 755 new version from https://stackoverflow.com/questions/41612368/find-most-common-element
755 from operator import itemgetter 756 previous version from from http://stackoverflow.com/questions/1518522/python-most-common-element-in-a-list'''
756 # get an iterable of (item, iterable) pairs 757 return Counter(l).most_common(1)[0][0]
757 SL = sorted((x, i) for i, x in enumerate(L)) 758
758 # print 'SL:', SL
759 groups = groupby(SL, key=itemgetter(0))
760 # auxiliary function to get "quality" for an item
761 def _auxfun(g):
762 item, iterable = g
763 count = 0
764 min_index = len(L)
765 for _, where in iterable:
766 count += 1
767 min_index = min(min_index, where)
768 # print 'item %r, count %r, minind %r' % (item, count, min_index)
769 return count, -min_index
770 # pick the highest-count/earliest item
771 return max(groups, key=_auxfun)[0]
772
773 ######################### 759 #########################
774 # sequence section 760 # sequence section
775 ######################### 761 #########################
776 762
777 class LCSS(object): 763 class LCSS(object):