1
다음은 quant-econ 튜토리얼입니다. 나는 벡터화 된 numpy 메서드를 사용하여 Empirical Cumulative Probability Funcion을 구현하기로되어있는 곳에서 운동을 시도하고있다. 여기 `__call__` 메서드를 벡터화하는 방법
이 문제에 대한 올바른 솔루션입니다 :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()
그러나 나는 그것을 이런 식으로 할을 시도 오전 : 그래서
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)
을 __call__
방법은 벡터화 및 인스턴스는 할 수있다 배열로 호출하면 해당 배열에 대한 누적 확률의 배열이 반환됩니다. 그러나, 나는이 같은 그것을 시도 :
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)
내 질문은, 어떻게 저자가 self.__call__
를 벡터화 할 수 와서 내 방법이 오류를 제공하면서, 작동합니다
p = ecdf(uniform(0,1,500))
p([0.2, 0.3])
는이 오류는 무엇입니까?
는 나도 몰라,하지만 난 (물체 측에) __init__''시키지 않을 것임을 그것의 자신의 클래스에서 얻은 함수의 정의를 수정 : 여기 내 솔루션입니다 (유형쪽에 있음). IMHO, 이것은 메타 클래스를 통해 이루어져야합니다. – Cilyan