0
$\begingroup$

I have input like below:

input_list = [['Search' 'engines','using','machine','learning','pattern','detections'], 

        ['machine','learning','helped','Google','automatically','sift','pages']]

input_list1 = ['Machine','learning','ever','evolving','technology']

Expecting Jaccard similarity distance between input_list and input_list1.

$\endgroup$
1
$\begingroup$

Python lib textdistance is a "python library for comparing distance between two or more sequences by many algorithms." It includes the Jaccard index. Having the similarity, you can get the distance by $Jacc_{distance}(x,y) = 1 -Jacc_{similarity}(x,y)$.

Although installing and importing the whole module wouldn't make much sense if this is all you need, reading the module's README may give you new ideas on the various distances you could use in your project, and take advantage of having all of them already implemented, so you can test how each of them performs really quick.

$\endgroup$
0
$\begingroup$
input_list = [['Search', 'engines','using','machine','learning','pattern','detections'], ['machine','learning','helped','Google','automatically','sift','pages']]
flat_list = [item for sublist in input_list for item in sublist]
input_list1 = ['Machine','learning','ever','evolving','technology']

def jaccard_similarity(list1, list2):
    intersection = len(list(set(list1).intersection(list2)))
    union = (len(list1) + len(list2)) - intersection
    return float(intersection) / union

jaccard_similarity(flat_list, input_list1)

Output:

0.05555555555555555
$\endgroup$
0
$\begingroup$

Since u have tokenized them, the jaccard distance is simply:

size(input_list intersect input_list1)/size(input_list union input_list1)

Reference: https://en.wikipedia.org/wiki/Jaccard_index

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.