当数据集增加时,sklearn匹配结果变得不对齐

2024-09-29 23:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用sklearn nearestneights进行名称匹配,在某一点上,结果变得不一致。我的标准名单是1亿。我要匹配的名字列表要小得多,但仍可能在250k到500k之间。在某一点之后,索引开始移动1或更多

nbrs = NearestNeighbors(n_neighbors=1, n_jobs=-1).fit(tfidf) 
unique_org = set(names['VariationName'].values) # set used for increased performance
#matching query:
def getNearestN(query):
  queryTFIDF_ = vectorizer.transform(query)
  distances, indices = nbrs.kneighbors(queryTFIDF_)
  return distances, indices

print('Getting nearest n...')
distances, indices = getNearestN(unique_org)

unique_org = list(unique_org) #need to convert back to a list
print('Finding matches...')
matches = []
for i,j in enumerate(indices):
  temp = [round(distances[i][0],2), clean_org_names.values[j][0][0],unique_org[i]]
  matches.append(temp)

print('Building data frame...')  
matches = pd.DataFrame(matches, columns=['Match confidence (lower is better)','Matched name','Original name'])
print('Data frame built') 

似乎一旦我的标准化列表超过80k,结果就会开始向下移动

维塔利的“脏乱名字”,安杰洛(有一个逗号)

VITALI, ANGELO

标准化名称列表可能包括这些名称(无逗号)

VITALI ANGELO   
SENSABLE TECHNOLOGIES INC

通过上面的匹配,下面的结果显示VITALI,ANGELO与SENSABLE TECNOLOGIES INC.近乎完美的匹配,因为指数已经下移了一个…我认为

 0.00   SENSABLE TECHNOLOGIES INC   VITALI, ANGELO

是否有可能记录的大小或数量超过了矩阵的限制,并以某种方式弄乱了索引


Tags: org名称列表query名字incuniqueprint
1条回答
网友
1楼 · 发布于 2024-09-29 23:31:00

集合一般不保证顺序的保留。因此getNearestN遍历unique_org的顺序可能与list构造函数的顺序不同:

distances, indices = getNearestN(unique_org)  # computed distances with respect to an unordered set

unique_org = list(unique_org) # `unique_org` was potentially shuffled here

相反,用列表试试看是否有效。如果列表速度慢得多,我怀疑罪魁祸首是重复的名称,而不是集合更适合这项工作。您可以在pandas(names['VariationName'].unique())或香草python(list(set(names['VariationName'])))中处理重复项

因此,总而言之,我要确保我没有重复的(可能使用熊猫),然后使用整个列表,看看它是否有效

资料来源:

A set object is an unordered collection of distinct hashable objects.

python docs

相关问题 更多 >

    热门问题