PythonUncategorized

Calculate Probability of Elements in Histogram

[sourcecode language=”python” wraplines=”false” collapse=”false”]# -*- 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()
[/sourcecode]