How do I create a function with a function in Python?

Simon

I want to create a function called jaccard that works something like this

def jaccard(doc1, doc2):
    inter = len(np.intersect1d(doc1, doc2))
    union = len(np.union1d(doc1, doc2))
    jaccard = float(inter)/union
    return jaccard

Except I only want it to take one parameter and have the other parameter hard coded into the function.

I want to write a function that will generate this function with the hard coded parameter because I need to use it with thousands of parameters.

def jaccard(doc2):
    inter = len(np.intersect1d(['Work with us --- The Missing Slate Magazine'], doc2))
    union = len(np.union1d(['Work with us --- The Missing Slate Magazine'], doc2))
    jaccard = float(inter)/union
    return jaccard

So I want to generate a function like this.

The reason I want this is because I want to apply it to a column of a Pandas DataFrame. The data frame contains a list of strings. I want to find the jaccard distance for each of them with the hard coded parameter in the function.

Thank you in advance!

Xingzhou Liu

You can create closures in python. For instance

def jacard(doc1):
    def _jacard(doc2):
        inter = len(np.intersect1d(doc1, doc2))
        union = len(np.union1d(doc1, doc2))
        return float(inter)/union
    return _jacard

then:

prepared_func = jacard(doc1)

afterwards:

results = map(prepared_func, some_array_of_doc2s)

partial argument binding using functools in this case is a shorthand for creating a closure with argument doc2 bound.

prepared_func = functools.partial(jaccard, doc1) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create a Python function with optional arguments?

How do I correct this function to create a label in Tkinter Python?

How do I create a variable accessible by any function in a class in python?

how do I create a function that randomly replaces vowels in a string with * in python

How do I create a print function in R?

How do I create a constructor function in R?

How do I create a HashMap with value of function?

How do I create an XPath function in Groovy

How do I create a noexcept function pointer?

How do I create a flatlist function in ML?

How do I create a jquery function with callbacks

How do I create this function correctly?

How do I create a Rust callback function to pass to a FFI function?

How do I create a function for separating parts of a function?

how do I create a function within a function to cycle?

How would I be able to create a function Python

How do I expose a function in a Python module?

How do I uncurry a function in Python?

How do I pass : as a function parameter in python?

How do I pickle a "memoized" Python function?

How do I change the representation of a Python function?

In python how do i use or in a while function

How do I evaluate a Sympy function in Python?

How do I call a function inside the function that you are calling to [PYTHON]

Python: How do I include a function in another function?

How do I get a list in to a function with an if else statement in python to create a variable?

how do i create list of list permutation with n elements using recursive function? | python

How do I create a function that calls a function when a certain function is called?

Protractor: How do I create a function that receives an HTML element as a parameter?