Nasty Python Things

March 27, 2009

So I seem to keep writing commands that look like this:

delta[t][q] = max(
    [delta[tau][j] +
        pylab.log(
            pylab.array([
                output_dist(Q=q,L=(t-tau),Y=Y[tau+1:t]),
                duration_dist(Q=q,L=(t-tau)),
                transition_dist[q,j]]).prod())            
    for j in self.state_range])

Is this bad? The above is the max of a list. The list is made up using a list comprehension, where each element is the log of a product of a 1D array plus a bit. Each element of each array is a call to a function associated with my model. The trouble is, if I break it down into some for loops, then I start having to invent temporary names for my variables, which seems clunky.

Any opinions?

M

Advertisements

2 Responses to “Nasty Python Things”

  1. James Hensman Says:

    I’m not sure why you bother creating an array, only to take the product of it?

    od = output_dist(q,t-tau,Y[tau+1:t])
    dd = duration_dist(q,t-tau)
    #tds = np.array([transition_dist[q,j] for j in range(T)])
    tds = transition_dist[q,:]#is ithis the same? it’s faster.
    options=delta[tau][j]+np.log(od*dd*tds)
    delta[t][q] = max(options)

    Which does indeed require temporary variable names. I’m pretty sure that my slice operator is faster than your list comp. though?

    P.S. I have inferred the dimensionality of your variables from their names: i.e. I’ve assumed that output_dist returns a scalar.

    • mikedewar Says:

      Erm I think you’re right. I don’t know why I do the list comprehension thing. I keep catching myself doing list comprehensions instead of just slicing. Even did something like:
      sum([ai*bi for ai,bi in zip(a,b)])
      the other day. No idea why. I looked at it again and re-wrote it as inner(a,b). I think I’m still excited by list comprehensions! It’s been a year!

      On the other hand i freaking hate the temporary variables, and am getting a little bit into this functional-style of programming. I think Gelman’s blog had something about this – and I think the best approach was to use a functional approach with no side effects etc. So maybe I should do something in the middle, namely:
      delta[t][q] = max(
      delta[tau] +
      pylab.log(
      output_dist(Q=q,L=(t-tau),Y=Y[tau+1:t]) *
      duration_dist(Q=q,L=(t-tau)) *
      transition_dist[q,:])
      )

      What do you think?


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: