Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59492 > unrolled thread
| Started by | Yaşar Arabacı <yasar11732@gmail.com> |
|---|---|
| First post | 2013-11-15 01:58 +0200 |
| Last post | 2013-11-15 01:58 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
How to np.vectorize __call__ method Yaşar Arabacı <yasar11732@gmail.com> - 2013-11-15 01:58 +0200
| From | Yaşar Arabacı <yasar11732@gmail.com> |
|---|---|
| Date | 2013-11-15 01:58 +0200 |
| Subject | How to np.vectorize __call__ method |
| Message-ID | <mailman.2633.1384473861.18130.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web