Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.04; 'base.': 0.05; 'element': 0.07; 'list?': 0.07; 'problem:': 0.07; 'skip:" 60': 0.07; '34,': 0.09; '[0,': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'python': 0.11; 'def': 0.12; '"from': 0.16; 'imports': 0.16; 'iterable': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typeerror:': 0.16; 'weird': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'issue.': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'environment': 0.24; 'sort': 0.25; 'equivalent': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'installed': 0.27; 'idea': 0.28; 'subject:list': 0.30; "i'm": 0.30; 'code': 0.31; 'away.': 0.31; 'probability': 0.31; 'writes:': 0.31; 'file': 0.32; 'probably': 0.32; 'run': 0.32; '(most': 0.33; 'bugs': 0.33; 'sense': 0.34; 'maybe': 0.34; 'problem': 0.35; "can't": 0.35; 'computing': 0.35; 'but': 0.35; 'version': 0.36; '(we': 0.36; 'next': 0.36; "didn't": 0.36; 'should': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'that,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'even': 0.60; 'expression': 0.60; 'then,': 0.60; 'skip:n 10': 0.64; '26,': 0.68; 'subject:over': 0.84; '8bit%:70': 0.91; 'that),': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: list comprehension return a list and sum over in loop Date: Fri, 12 Dec 2014 17:08:28 +0100 Organization: None References: <1531593e-8dac-4b85-b838-3783717249c2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd892c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418400523 news.xs4all.nl 2866 [2001:888:2000:d::a6]:42117 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82457 KK Sasa wrote: > Peter Otten於 2014年12月12日星期五UTC+8下午8時32分55秒寫道: >> Jussi Piitulainen wrote: >> >> > KK Sasa writes: >> > >> >> def p(x,t,point,z,obs): >> >> d = x[0] >> >> tau = [0]+[x[1:point]] >> >> a = x[point:len(x)] >> >> at = sum(i*j for i, j in zip(a, t)) >> >> nu = [exp(z[k]*(at-d)-sum(tau[k])) for k in xrange(point)] >> >> de = sum(nu, axis=0) >> >> probability = [nu[k]/de for k in xrange(point)] >> >> return probability[obs] >> > >> > I must be blind, but this looks like computing a whole probability >> > distribution and then throwing almost all of it away. >> > >> > Can't this just return nu[obs]/de? >> > >> > The expression for tau also seems weird to me. Isn't it equivalent to >> > [0, x[1:point]], a two-element list with the second element a list? >> > How can sum(tau[k]) work at all then, for any k > 1? >> >> Also, after adding >> >> from numpy.random import seed >> >> to the code I run into the next problem: >> >> Traceback (most recent call last): >> File "hessian.py", line 34, in >> re = [d2(x,t[k],2,z,1) for k in range_people] >> File "/home/petto/.local/lib/python2.7/site-packages/ad/__init__.py", >> line >> 1114, in hess >> return func(xa, *args).hessian([xa]) >> File "hessian.py", line 26, in p >> nu = [exp(z[k]*(at-d)-sum(tau[k])) for k in xrange(point)] >> TypeError: 'int' object is not iterable >> >> OP: Optimizations only make sense when you can start from a known-good >> base. You should sort out the logic of your problem (we probably can't >> help you with that), then fix the bugs in your code and only then revisit >> the "speed issue". > > I have no idea why you added "from numpy.random import seed" to this > syntax. Even I added that, my python didn't complain any bugs inside. > Maybe your ad package was not be installed correctly? It may be a version issue. I'm using >>> import ad, numpy >>> ad.__version__ '1.2.2' >>> numpy.__version__ '1.9.1' Or are you using an environment that does some imports automatically?