Calculate Probability of Elements in Histogram

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 23 10:26:39 2016

@author: Shruti
"""
from random import randint
def hist(s):
 d=dict()
 for c in s:
 if c not in d:
 d[c] = 1
 else:
 d[c] += 1
 return d
def calcProb():
 listHist= [17, 18, 17, 20, 15, 25, 25, 19, 18, 19, 23, 20, 22, 22, 21]
 hDict = hist(listHist)
 print("Original Input: ",hDict)
 totalElements= sum(hDict.values())
 print("Total Elements in the Dict is: ", totalElements)
 for c in hDict:
 hDict[c]=hDict[c]/totalElements
 randNumber = randint(0,totalElements)
 print("Random Number is", randNumber)
 #print(hDict)
 listNumber= listHist[randNumber]
 print(listNumber,hDict[listNumber])
 #random.randrange(len(h.keys))
calcProb()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: