Ошибка настроенного метода в связи записей - PullRequest
0 голосов
/ 20 января 2020

Я хочу применить пользовательский метод в связке записей, следуя официальной документации . Мой код:

import recordlinkage as rl
from recordlinkage.base import BaseCompareFeature



   def compute_wmd( s1, s2):

       word2vec_file = "C:\\Users\\users\\Desktop\\GoogleNews-vectors-negative300.bin"
       word2vec = gensim.models.KeyedVectors.load_word2vec_format(word2vec_file, binary=True)           
       word2vec.init_sims(replace=True)  # Normalizes the vectors in the word2vec class
       score = word2vec.wmdistance(s1, s2)

       return score


comparer = rl.Compare()

comparer.add(compute_wmd('Description US', 'Description US', label='Description'))
comparer.compute(pairs, csv)

Я получаю эту ошибку:

TypeError: compute_wmd () получил неожиданный аргумент ключевого слова 'label'

и если Я удаляю метку и получаю эту ошибку:

AttributeError: у объекта 'float' нет атрибута 'tags_left'

1 Ответ

0 голосов
/ 03 марта 2020

Вам нужно будет создать пользовательский класс, который наследует «BaseCompareFeature», а затем добавить метод к классу с именем _compute_vectorized (self, s1, s2). Только тогда Record Linkage примет ее в качестве допустимой пользовательской функции

Пример кода:

импорт recordlinkage как rl из recordlinkage.base import BaseCompareFeature

класс CompareZipCodes (BaseCompareFeature):

def __init__(self, left_on, right_on, partial_sim_value, *args, **kwargs):
    super(CompareZipCodes, self).__init__(left_on, right_on, *args, **kwargs)

    self.partial_sim_value = partial_sim_value

def _compute_vectorized(self, s1, s2):
    """Compare zipcodes.

    If the zipcodes in both records are identical, the similarity
    is 0. If the first two values agree and the last two don't, then
    the similarity is 0.5. Otherwise, the similarity is 0.
    """

    # check if the zipcode are identical (return 1 or 0)
    sim = (s1 == s2).astype(float)

    # check the first 2 numbers of the distinct comparisons
    sim[(sim == 0) & (s1.str[0:2] == s2.str[0:2])] = self.partial_sim_value

    return sim
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...