Python math formula syntax

I am trying to implement a mathematical formula in Python, and I'm pretty new to this, so please work with me.

I have two files with section separators on three columns:

For example: InputA:

abandonment-n about-bring-v 32.5890
abandonment-n about-complaint-n 5.5112
abandonment-n about-concern-n 10.6714
abandonment-n among-1-crowd-n 11.4496

InputB:

aardvark-n about-fact-n 7.4328
aardvark-n about-information-n 6.5145
aardvark-n about-know-v 6.4239
aardvark-n among-1-crowd-n 9.9085

InputB:

The formula I'm trying to implement should take both files into account as input.

Mathematically, the formula is as follows:

Similarity measureas described in this article

where, f = function, Fx = function vector, w = weight of the function.

This is what I came up with:

Import both inputs as a dict, where [feature: weight].

Suppose inputA = x and inputB = y.

Then the syntax that I developed for the formula is as follows:

score = sum(i for i in x if i in y) * w(i) / sum(i for i in x)* w(i)

In this case *w(i), the length of the corresponding function should be multiplied.

- Python ( , ), , ?

+4
2

, . - :

from __future__ import division # this must be the very first import statement
score = sum(i*w(i) for i in x if i in y) / sum(i * w(i) for i in x)

, w(i) , ; , w(i) , i .

. :

from __future__ import division # this must be the very first import statement
xx = set(x)
yy = set(y)
score = sum(i*w(i) for i in xx & yy) / sum(i * w(i) for i in x)

xx & yy - Python xx.intersection(yy). , x y , , , , -, .

+4

, . doctests, .

def score(x, y, w):
    """
    Calcutates directional distributional similarity http://dl.acm.org/citation.cfm?id=1897650

    >>> score([1, 2], [1, 3], {1:2, 2:1, 3:1})
    0.42857142857142855
    """
    return sum(i for i in x if i in y) * w[i] / sum(i for i in x)* w[i]

pip install nose
nosetests  --with-doctests

Failed example:
    score([1, 2], [1, 3], {1:2, 2:1, 3:1})
Exception raised:
    Traceback (most recent call last):
       ...
    NameError: global name 'i' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.531s

FAILED (failures=1)

. @misha

def score(x, y, w):
    """
    Calcutates directional distributional similarity http://dl.acm.org/citation.cfm?id=1897650

    >>> score([1, 2], [1, 3], {1:1.5, 2:1.0, 3:1.0})
    0.42857142857142855
    """
    xx = set(x)
    yy = set(y)
    return 1.0 * sum(i*w[i] for i in xx & yy) / sum(i * w[i] for i in x) 

.
----------------------------------------------------------------------
Ran 1 test in 0.016s

OK

1.0*, :

Failed example:
    score([1, 2], [1, 3], {1:2, 2:1, 3:1})
Expected:
    0.42857142857142855
Got:
    0

.

+2

All Articles