2

Let's say I have a 2d numpy array A:

A = [[0.3, 0.2],
     [1.0, 0.1],
     [0.3, 0.1],
     [1.0, 0.1]]

What I would like is something that maps rows of A to their empirical distribution:

f([0.3, 0.2]) = 0.25
f([1.0, 0.1]) = 0.50
f([-12, 140]) = 0.00

Is there a nice way to do this?

4
  • Could you share the implementation of f? Commented Jan 28, 2016 at 15:52
  • The implementation of f is what I am looking for. Commented Jan 28, 2016 at 15:53
  • Do you mean this empirical distribution: en.wikipedia.org/wiki/Empirical_distribution_function ? Commented Jan 28, 2016 at 15:57
  • Yes, with a caveat: I mean the empirical pdf - what they display there is the empirical cdf. Commented Jan 28, 2016 at 16:09

2 Answers 2

2

I suggest using numpy.allclose. You can choose a tolerance, here I put 1.e-10 :

import numpy as np

A = np.array([[0.3, 0.2],[1.0, 0.1],[0.3, 0.1], [1.0, 0.1]])

def f(x,tol=1.e-10):
  l = [np.allclose(x,row,tol) for row in A]
  return l.count(True)/float(A.shape[0])

print f(np.array([0.3,0.2]))
print f(np.array([1.0, 0.1]))
print f(np.array([-12, 140]))
Sign up to request clarification or add additional context in comments.

Comments

0

Here's my hacky closure-based method:

def pdf_maker(A, round_place=10):
    counts = {}
    for i in range(A.shape[0]):
        key = tuple([round(a,round_place) for a in A[i]])
        try:
            counts[key] += 1.0
        except KeyError:
            counts[key] = 1.0

    pdf = {}
    for key in counts:
        pdf[key] = counts[key] / A.shape[0]

    def f_pdf(row):
        key = tuple([round(a,round_place) for a in row])
        try:
            return pdf[key]
        except KeyError:
            return 0.0

    return f_pdf

I'm sure there's a cleaner way to do it, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.