Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59492

How to np.vectorize __call__ method

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <yasar11732@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'exercise': 0.04; 'error:': 0.07; 'problem:': 0.07; 'skip:` 10': 0.07; '===': 0.09; 'subject:method': 0.09; 'valueerror:': 0.09; 'way:': 0.09; 'subject:How': 0.10; 'def': 0.12; 'array.': 0.16; 'numpy': 0.16; 'operands': 0.16; 'skip:" 70': 0.16; 'url:html)': 0.16; 'trying': 0.19; 'question': 0.24; 'this:': 0.26; 'array': 0.29; 'specified': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'getting': 0.31; 'url:wiki': 0.31; '"",': 0.31; '50,': 0.31; 'probability': 0.31; 'url:wikipedia': 0.31; 'file': 0.32; 'class': 0.32; 'supposed': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; '(2)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'url:org': 0.36; 'implement': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'skip:n 10': 0.64; 'choose': 0.64; 'here': 0.66; 'broadcast': 0.68; 'from:charset:iso-8859-9': 0.84; 'x):': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=WVibQZ5IqR4oX7GaMLGZseUQ5ZI91FXQclPcFTeuYJs=; b=xQGO38ChELbn1W6DJArSsyPiQNWWFQoYNQV9kpI1+Smc/qPv8wB/i3TZmvrwgPXZKb LJof2mBoZSlNg5sJzq+1YMm4e7n/RE1yRB91TjfMHpa357WCUN5mTyQB2XOITBf2pODT h6k6kkxBqK9wbKmM/5qrrk5AGMRJsj7KFaFZZQOa8o7UnyifdZwbeoeCNmJehlqWHTHh KzRbLvncJWkmgWtxfbkLw5Q0vUF/kITsYvc5omhuUYe4nu1L8YCQYLd3kdcl+98WgToE /wiyhVwKiy32pmDfSfnX5eQmHmIhzbyoj+fKAIOgw9lA4r9Y0ZEh90Ev3HY4YKqLPiAx x7UQ==
MIME-Version 1.0
X-Received by 10.204.98.68 with SMTP id p4mr47970bkn.147.1384473527941; Thu, 14 Nov 2013 15:58:47 -0800 (PST)
Date Fri, 15 Nov 2013 01:58:47 +0200
Subject How to np.vectorize __call__ method
From Yaşar Arabacı <yasar11732@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2633.1384473861.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384473861 news.xs4all.nl 15963 [2001:888:2000:d::a6]:35811
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59492

Show key headers only | View raw


I am cross-posting from: http://stackoverflow.com/q/19990863/886669

I am following, [quant-econ](http://quant-econ.net/numpy.html)
tutorial. I am trying the exercise where I am supposed to implement a
[Empirical Cumulative Probability
Funcion](http://en.wikipedia.org/wiki/Empirical_distribution_function)
using vectorized numpy methods.

Here is the **correct** solution to problem:

    class ecdf:

        def __init__(self, observations):
            self.observations = np.asarray(observations)

        def __call__(self, x):
            return np.mean(self.observations <= x)

        def plot(self, a=None, b=None):

            # === choose reasonable interval if [a, b] not specified === #
            if not a:
                a = self.observations.min() - self.observations.std()
            if not b:
                b = self.observations.max() + self.observations.std()

            # === generate plot === #
            x_vals = np.linspace(a, b, num=100)
            f = np.vectorize(self.__call__)
            plt.plot(x_vals, f(x_vals))
            plt.show()

But I am **trying** to do it this way:

    class ecdf(object):

        def __init__(self, observations):
            self.observations = np.asarray(observations)
            self.__call__ = np.vectorize(self.__call__)

        def __call__(self, x):
            return np.mean(self.observations <= x)

So that, `__call__` method is vectorized and instance can be called
with an array and it returns an array of cumulative probabilities for
that array. However, when I try it like this:

    p = ecdf(uniform(0,1,500))
    p([0.2, 0.3])

I am getting this error:

    Traceback (most recent call last):

      File "<ipython-input-34-6a77f18aa54e>", line 1, in <module>
        p([0.2, 0.3])

      File "D:/Users/y_arabaci-ug/Desktop/quant-econ/programs/numpy_exercises.py",
line 50, in __call__
        return np.mean(self.observations <= x)

    ValueError: operands could not be broadcast together with shapes (500) (2)

My question is, how come author could vectorize `self.__call__` and it
works, while my method gives an error?


-- 
http://ysar.net/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

How to np.vectorize __call__ method Yaşar Arabacı <yasar11732@gmail.com> - 2013-11-15 01:58 +0200

csiph-web