[Tutor] Clustering?
Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) Thanks in advance for your help, Carlos ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
2007/1/16, Carlos [EMAIL PROTECTED]: Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) First, this feels like a list of tuples rather than a list of lists; however, tuples and lists don't differ that much in their behaviour, so there's nothing really lost. And yes, it is possible. An inline if would be the way I would resolve that: def withinrange(list,xmin,xmax,ymin,ymax): # Get the elements of list for which the first part of the pair is between xmin and xmax # (inclusive) and the second between ymin and ymax. return [c for c in list if xmin = c[0] = xmax and ymin = c[1] = ymax] The longer but clearer method of doing the same would be: def withinrange(list,xmin,xmax,ymin,ymax): templist = [] for c in list: if xmin = c[0] = xmax and ymin = c[1] = ymax: templist.append(c) return templist -- Andre Engels, [EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
Hallo , Andre thanks a lot for your help, seems to me like my script can work with your function. I found this searching the internet: cluster 1.1.1b2 python-cluster is a simple package that allows to create several groups (clusters) of objects from a list from cluster import * data = [12,34,23,32,46,96,13] cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) cl.getlevel(10) # get clusters of items closer than 10 [96, 46, [12, 13, 23, 34, 32]] cl.getlevel(5) # get clusters of items closer than 5 [96, 46, [12, 13], 23, [34, 32]] I would like to give it a try because I have the impression that it can be helpful too. My problem now is the lambda function, I was wondering if someone could be so kind as to give me an example that could work in my list with nested tuples. Thanks again! Carlos Andre Engels wrote: 2007/1/16, Carlos [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]: Hello to everybody, I have a question that I think is a little related to clustering, I have a list of lists, like this: List = [[1,5],[6,8],[48,10],[99,56]] The list is composed by a large number of lists not just four, and each 'interior' list contains two numbers that are the location of an object in a plane, so they are X and Y coordinates, my question is: Can I use this list to evaluate how many points are in a given range? Thats is taking the highest and lowest values for X an Y and, lets say divide that range in 10 regions, then get the number of objects on each region. Is this possible? and if yes, how canI do it? I ask this because as you know my python skills are not that great :) First, this feels like a list of tuples rather than a list of lists; however, tuples and lists don't differ that much in their behaviour, so there's nothing really lost. And yes, it is possible. An inline if would be the way I would resolve that: def withinrange(list,xmin,xmax,ymin,ymax): # Get the elements of list for which the first part of the pair is between xmin and xmax # (inclusive) and the second between ymin and ymax. return [c for c in list if xmin = c[0] = xmax and ymin = c[1] = ymax] The longer but clearer method of doing the same would be: def withinrange(list,xmin,xmax,ymin,ymax): templist = [] for c in list: if xmin = c[0] = xmax and ymin = c[1] = ymax: templist.append(c) return templist -- Andre Engels, [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] ICQ: 6260644 -- Skype: a_engels No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.432 / Virus Database: 268.16.12/630 - Release Date: 1/15/2007 8:28 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Clustering?
Carlos [EMAIL PROTECTED] wrote from cluster import * data = [12,34,23,32,46,96,13] cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) cl.getlevel(10) # get clusters of items closer than 10 [96, 46, [12, 13, 23, 34, 32]] I would like to give it a try because I have the impression that it can be helpful too. My problem now is the lambda function, I was wondering if someone could be so kind as to give me an example that could work in my list with nested tuples. lambda is just a shorthand way of writing a simple function. You don't need to use lambda. The above line could have been done thisaway: def f(x,y): return abs(x-y) cl = HierarchicalClustering(data, f) So provided you can write a function to do what you want you don't need the lambda if it confuses you. In a general sense: def f(p): return expression is the same as f = lambda p: expression So anywhere that a function name is nmeeded you can put a lambda. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor