[Tutor] Question about style

Jose Amoreira ljmamoreira at gmail.com
Wed Jul 16 23:50:44 CEST 2014


Hi!

On 07/16/2014 10:14 PM, Wolfgang Maier wrote:
>
> careful here: you just stored a tuple instead of a list; doesn't matter
> for your current implementation, but may bite you at some point.

Oh, you're right. Silly mistake, even if harmless for the application I 
have in mind.
>
>>          else:
>>              # x does not belong to any cluster, create a new one
>>              clusters.append([x,1])
>>      # return list with centers
>>      return [center for center, _ in clusters]
>>
>
> Your building of the close_to list with Trues and Falses, then using it
> to find the original element again makes me think that you're a regular
> R user ? Using such a logical vector (I guess that's what you'd call it
> in R) should (almost) never be required in Python.

No, I use mainly python and fortran (but I'm not a "real" programmer). I 
arrived at this particular function from a previous version which only 
took one single line and was quite readable, but it didn't compute the 
centers of the clusters; the first value added to a new cluster would be 
the cluster value, regardless of other added values. Then I decided that 
I wanted the centroids of each cluster and this was the result. It 
smelled bad, but I was trying to hang on to my (supposedly) smart one 
liner...

> Here's an implementation of this idea demonstrating how, in Python, you
> typically use the builtin enumerate function to avoid "logical vectors"
> or other kinds of index juggling:
>
> def aglomerate(x_lst, delta=1.e-5):
>      centers = []
>      sizes = []
>      for x in x_lst:
>          for i, center in enumerate(centers):
>              if abs(x - center) < delta:
>                  # x is close to a cluster
>                  #update the cluster center including the new value,
>                  #and increment dimension of cluster
>                  n = sizes[i]
>                  centers[i] = (n * center + x)/(n+1)
>                  sizes[i] = n+1
>                  break
>          else:
>              # this block is executed only when the break in the preceeding
>              # block wasn't reached =>
>              # x does not belong to any cluster, create a new one
>              centers.append(x)
>              sizes.append(1)
>      # return list with centers
>      return centers
>
Thanks, Wolfgang. You were very helpful.



More information about the Tutor mailing list