Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'algorithm': 0.04; 'subject:Question': 0.07; 'subject:code': 0.07; 'len(x)': 0.09; 'python': 0.11; 'def': 0.12; 'backward': 0.16; 'dict': 0.16; 'fine.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'observations': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'states)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'machine': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'query': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'code': 0.31; 'getting': 0.31; 'url:wiki': 0.31; '>>>>': 0.31; 'calculated': 0.31; 'minor': 0.31; 'url:wikipedia': 0.31; 'values.': 0.31; 'figure': 0.32; 'running': 0.33; 'problem': 0.35; 'received:84': 0.35; 'but': 0.35; 'url:org': 0.36; 'so,': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'then,': 0.60; 'new': 0.61; 'kindly': 0.61; 'email addr:gmail.com': 0.63; 'forward': 0.65; 'dear': 0.65; 'talking': 0.65; 'url:%1': 0.72; 'dict.': 0.84; 'esteemed': 0.91; 'states,': 0.91; 'room,': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MtHc6gqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=1oDRhzKAKukA:10 a=dp5bEypDzxEA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=8pif782wAAAA:8 a=zuLbfvP23PDpKuZzC3oA:9 a=ZFMamWKzPVnR2OWZ:21 a=Kb2E4YA9kJ7R-sap:21 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Sat, 10 May 2014 21:44:59 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Question on Debugging a code line References: <283e285b-4ab3-4ec7-a8be-4f1e047d9645@googlegroups.com> In-Reply-To: <283e285b-4ab3-4ec7-a8be-4f1e047d9645@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399754708 news.xs4all.nl 2931 [2001:888:2000:d::a6]:33278 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71274 On 2014-05-10 20:27, subhabangalore@gmail.com wrote: > Dear Room, > > I was trying to go through a code given in http://en.wikipedia.org/wiki/Forward%E2%80%93backward_algorithm[ Forward Backward is an algorithm of Machine Learning-I am not talking on that > I am just trying to figure out a query on its Python coding.] > > I came across the following codes. > >>>> states = ('Healthy', 'Fever') >>>> end_state = 'E' >>>> observations = ('normal', 'cold', 'dizzy') >>>> start_probability = {'Healthy': 0.6, 'Fever': 0.4} >>>> transition_probability = { > 'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01}, > 'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01}, > } >>>> emission_probability = { > 'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1}, > 'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6}, > } > > def fwd_bkw(x, states, a_0, a, e, end_st): > L = len(x) > fwd = [] > f_prev = {} #THE PROBLEM > # forward part of the algorithm > for i, x_i in enumerate(x): > f_curr = {} > for st in states: > if i == 0: > # base case for the forward part > prev_f_sum = a_0[st] > else: > prev_f_sum = sum(f_prev[k]*a[k][st] for k in states) ## > > f_curr[st] = e[st][x_i] * prev_f_sum > > fwd.append(f_curr) > f_prev = f_curr > > p_fwd = sum(f_curr[k]*a[k][end_st] for k in states) > > As this value was being called in prev_f_sum = sum(f_prev[k]*a[k][st] for k in states marked ## > I wanted to know what values it is generating. > So, I had made the following experiment, after > for i, x_i in enumerate(x): > I had put print f_prev > but I am not getting how f_prev is getting the values. > > Here, > x=observations, > states= states, > a_0=start_probability, > a= transition_probability, > e=emission_probability, > end_st= end_state > > Am I missing any minor aspect? > Code is running fine. > > If any one of the esteemed members may kindly guide me. > The values calculated in the inner loop are being put into the dict 'f_curr' and then, when that loop has completed, 'f_prev' is being bound to that dict. 'f_curr' is bound to a new dict just before the inner loop, ready for the new values.