Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:code': 0.07; 'subject:help': 0.08; 'arrays': 0.09; 'function,': 0.09; 'throws': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '(1,': 0.16; 'cc:name:python list': 0.16; 'operates': 0.16; 'runtime.': 0.16; 'x[i]': 0.16; 'index': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'shape': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'convenient': 0.24; 'rid': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'array': 0.29; 'kevin': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; 'values.': 0.31; 'guess': 0.33; "can't": 0.35; 'convert': 0.35; 'operate': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'doing': 0.36; 'possible': 0.36; 'should': 0.36; 'january': 0.37; 'two': 0.37; 'form,': 0.38; 'generating': 0.39; 'how': 0.40; 'offer': 0.62; "you've": 0.63; 'skip:n 10': 0.64; 'subject:Need': 0.64; 'to:addr:gmail.com': 0.65; 'skip:w 30': 0.69; 'matrix.': 0.84; 'oscar': 0.84; 'broadcasting': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=+3y7G8wbneWpxPZbobf1Bj8YTpriqwLAUca1g5gknrg=; b=e6C+Th06CgJhNQ06NE4ZJFxjuuthdFaUZ9/TF6ReQQc/uNvvrJlFvi7gvyLw9mjhZ/ EDOkJ1bGrtGpudvURvonvuBTtl2Z+VPWpC6KqRaOFMrW/3hKtyETQZmbQQxAjjzsE8zB B83KXjioWI9CRYSqtv4dAVNunXD/Yx6dEP0Hqgvp5nm7E4bQV+GuwRit1uaG4wJNwB3K FfFBb0YcGBI3NabVDnL0x3yC7U9uFLIDHjLJtqKgw5R88+7+QJHijiXc0EOY7ZlqD0YL etAglL62kd1jXbyiTr8UavLQSa7Js5yYrTbfyXNh0vBXeCXq3qvqjacTtZ+B9L3DvDFC KmcQ== X-Received: by 10.68.172.196 with SMTP id be4mr13807000pbc.12.1390146423787; Sun, 19 Jan 2014 07:47:03 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <140f8aea-d3c5-4c0d-94f5-6aa064e353d1@googlegroups.com> References: <140f8aea-d3c5-4c0d-94f5-6aa064e353d1@googlegroups.com> From: Oscar Benjamin Date: Sun, 19 Jan 2014 15:46:43 +0000 Subject: Re: Need help vectorizing code To: Kevin K Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390146433 news.xs4all.nl 2864 [2001:888:2000:d::a6]:35507 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64295 On 18 January 2014 20:51, Kevin K wrote: > I have some code that I need help vectorizing. > I want to convert the following to vector form, how can I? I want to get rid of the inner loop - apparently, it's possible to do so. > X is an NxD matrix. y is a 1xD vector. > > def foo(X, y, mylambda, N, D, epsilon): > ... > for j in xrange(D): > aj = 0 > cj = 0 > for i in xrange(N): > aj += 2 * (X[i,j] ** 2) > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + w[j]*X[i,j])) As Peter said the y[i] above suggests that y has the shape (1, N) or (N, 1) or (N,) but not (1, D). Is that an error? Should it actually be y[j]? You don't give the shape of w but I guess that it is (1, D) since you index it with j. That means that w.transpose() is (D, 1). But then X[i] has the shape (D,). Broadcasting those two shapes gives a shape of (D, D) for cj. OTOH if w has the shape (D, 1) then cj has the shape (1, D). Basically your description is insufficient for me to know what your code is doing in terms of all the array shapes. So I can't really offer a vectorisation of it. > > ... > > If I call numpy.vectorize() on the function, it throws an error at runtime. You've misunderstood what the numpy.vectorize function is for. The vectorize function is a convenient way of generating a function that can operate on arrays of arbitrary shape out of a function that operates only on scalar values. Oscar