freq function

Discussion in 'Python' started by Dirk Nachbar, Aug 22, 2010.

  1. Dirk Nachbar

    Dirk Nachbar Guest

    Here is a function which takes any list and creates a freq table,
    which can be printed unsorted, sorted by cases or items. It's supposed
    to mirror the proc freq in SAS.

    Dirk

    def freq(seq,order='unsorted',prin=True):
    #order can be unsorted, cases, items

    freq={}
    for s in seq:
    if s in freq:
    freq+=1
    else:
    freq=1
    if prin==True:
    print 'Items=',len(seq),'Cases=',len(freq)
    print '------------------------'
    if order=='unsorted':
    for k in freq.keys():
    print k,freq[k],float(freq[k])/len(seq)
    elif order=='cases':
    #http://blog.client9.com/2007/11/sorting-python-dict-by-
    value.html
    freq2=sorted(freq.iteritems(), key=lambda (k,v):
    (v,k),reverse=True)
    for f in freq2:
    print f[0],f[1],float(f[1])/len(seq)
    elif order=='items':
    for k in sorted(freq.iterkeys()):
    print k,freq[k],float(freq[k])/len(seq)
    print '------------------------'
    return freq

    #test

    import random

    rand=[]
    for i in range(10000):
    rand.append(str(int(100*random.random())))

    fr=freq(rand)
    fr2=freq(rand,order='items')
    fr2=freq(rand,order='cases')
    Dirk Nachbar, Aug 22, 2010
    #1
    1. Advertising

  2. Dirk Nachbar

    Chris Rebert Guest

    On Sun, Aug 22, 2010 at 1:16 AM, Shashwat Anand
    <> wrote:
    > On Sun, Aug 22, 2010 at 1:31 PM, Dirk Nachbar <> wrote:
    >> Here is a function which takes any list and creates a freq table,
    >> which can be printed unsorted, sorted by cases or items. It's supposed
    >> to mirror the proc freq in SAS.
    >>
    >> Dirk
    >>

    <snip>
    >>    freq={}
    >>    for s in seq:
    >>        if s in freq:
    >>            freq+=1
    >>        else:
    >>            freq=1

    >
    > The above code can be replaced with this:
    >  freq = {}
    >  for s in seq:
    >           freq = freq.get(s,0) + 1


    Which can be further replaced by:

    from collections import Counter
    freq = Counter(seq)

    Using collections.defaultdict is another possibility if one doesn't
    have Python 2.7.

    Cheers,
    Chris
    --
    It really bothers me that Counter isn't a proper Bag.
    http://blog.rebertia.com
    Chris Rebert, Aug 22, 2010
    #2
    1. Advertising

  3. Dirk Nachbar

    Peter Otten Guest

    Dirk Nachbar wrote:

    > Here is a function which takes any list and creates a freq table,
    > which can be printed unsorted, sorted by cases or items. It's supposed
    > to mirror the proc freq in SAS.
    >
    > Dirk
    >
    > def freq(seq,order='unsorted',prin=True):
    > #order can be unsorted, cases, items
    >
    > freq={}
    > for s in seq:
    > if s in freq:
    > freq+=1
    > else:
    > freq=1
    > if prin==True:
    > print 'Items=',len(seq),'Cases=',len(freq)
    > print '------------------------'
    > if order=='unsorted':
    > for k in freq.keys():
    > print k,freq[k],float(freq[k])/len(seq)
    > elif order=='cases':
    > #http://blog.client9.com/2007/11/sorting-python-dict-by-
    > value.html
    > freq2=sorted(freq.iteritems(), key=lambda (k,v):
    > (v,k),reverse=True)


    Sorting in two steps gives a slightly better result when there are items
    with equal keys. Compare

    >>> freq = {"a": 2, "b": 1, "c": 1, "d": 2}
    >>> sorted(freq.iteritems(), key=lambda (k, v): (v, k), reverse=True)

    [('d', 2), ('a', 2), ('c', 1), ('b', 1)]

    with

    >>> freq2 = sorted(freq.iteritems(), key=lambda (k, v): k)
    >>> freq2.sort(key=lambda (k, v): v, reverse=True)
    >>> freq2

    [('a', 2), ('d', 2), ('b', 1), ('c', 1)]

    Here the keys within groups of equal frequency are in normal instead of
    reversed order.
    Peter Otten, Aug 22, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,007
    Evan Carew
    Jan 21, 2004
  2. efrat
    Replies:
    1
    Views:
    713
    Ron Adam
    Apr 6, 2006
  3. Tim Golden
    Replies:
    1
    Views:
    868
    Michele Petrazzo
    Apr 6, 2006
  4. noroi
    Replies:
    0
    Views:
    545
    noroi
    Oct 18, 2007
  5. nivparsons

    Sync high freq to 100Hz?

    nivparsons, Feb 16, 2012, in forum: VHDL
    Replies:
    2
    Views:
    719
Loading...

Share This Page