Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '(i.e.,': 0.03; 'builtin': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrote:': 0.14; '0],': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'indices': 0.23; 'pair': 0.23; 'subject:group': 0.23; 'shared': 0.29; 'hi,': 0.29; 'list': 0.30; 'this.': 0.30; 'conversion.': 0.31; 'from:addr:web.de': 0.31; 'to:addr:python-list': 0.32; 'initial': 0.32; '...': 0.32; 'data.': 0.33; 'header:X-Complaints-To:1': 0.34; 'data': 0.37; 'some': 0.37; 'but': 0.38; 'members': 0.38; 'received:org': 0.38; 'help': 0.39; 'set': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'containing': 0.40; 'count': 0.40; 'sets': 0.40; 'header:Received:5': 0.40; 'organized': 0.63; 'share': 0.67; 'strategy': 0.71; 'incidence': 0.84; 'matrix': 0.84; 'comprised': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Pairwise count of frequency from an incidence matrix of group membership Date: Wed, 20 Apr 2011 10:30:28 +0200 Organization: None References: <367389.80532.qm@web65402.mail.ac4.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084a508.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 50 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303288235 news.xs4all.nl 81478 [::ffff:82.94.164.166]:42916 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3668 Shafique, M. (UNU-MERIT) wrote: > Hi, > I have a number of different groups g1, g2, … g100 in my data. Each group > is comprised of a known but different set of members from the population > m1, m2, …m1000. The data has been organized in an incidence matrix: > g1g2g3g4g5 > m111101 > m210010 > m301100 > m411011 > m500110 > > I need to count how many groups each possible pair of members share (i.e., > both are member of). > I shall prefer the result in a pairwise edgelist with weight/frequency in > a format like the following: > m1, m1, 4 > m1, m2, 1 > m1, m3, 2 > m1, m4, 3 > m1, m5, 1 > m2, m2, 2 > ... and so on. > > I shall highly appreciate if anybody could suggest/share some > code/tool/module which could help do this. Homework? What have you tried? One strategy is to create a list of sets containing the groups from the initial matrix matrix = [ [1, 1, 1, 0, 1], [1, 0, 0, 1, 0], ] sets = [ # zero-based indices set([0,1,2,4]), set([0,3]), ... ] The enumerate() builtin may help you with the conversion. You can then find the shared groups with set arithmetic: sets[0] & sets[1] #m1/m2