Re: Question on Debugging a code line

2014-05-11 Thread subhabangalore
On Sunday, May 11, 2014 12:57:34 AM UTC+5:30, subhaba...@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.
 
 
 
 Regards,
 
 Subhabrata Banerjee.

Dear Sir,
Thank you for your kind reply. I will check. 
Regards,
Subhabrata Banerjee. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on Debugging a code line

2014-05-11 Thread subhabangalore
On Sunday, May 11, 2014 11:50:32 AM UTC+5:30, subhaba...@gmail.com wrote:
 On Sunday, May 11, 2014 12:57:34 AM UTC+5:30, subhaba...@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.
 
  
 
  
 
  
 
  Regards,
 
  
 
  Subhabrata Banerjee.
 
 
 
 Dear Sir,
 
 Thank you for your kind reply. I will check. 
 
 Regards,
 
 Subhabrata Banerjee.

Dear Sir,
Thank you. It worked. I made another similar statement over another set of 
values on your reply it went nice.
Regards,
Subhabrata Banerjee.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on Debugging a code line

2014-05-11 Thread Mark Lawrence

On 11/05/2014 08:45, subhabangal...@gmail.com wrote:

[268 lines snipped]

Would you please use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
https://mail.python.org/mailman/listinfo/python-list


Question on Debugging a code line

2014-05-10 Thread subhabangalore
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.

Regards,
Subhabrata Banerjee. 



 
   

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on Debugging a code line

2014-05-10 Thread MRAB

On 2014-05-10 20:27, subhabangal...@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.

--
https://mail.python.org/mailman/listinfo/python-list