python数据模糊匹配_如何在python中的数组的列中模糊匹配项?

I have an array of team names from NCAA, along with statistics associated with them. The school names are often shortened or left out entirely, but there is usually a common element in all variations of the name (like Alabama Crimson Tide vs Crimson Tide). These names are all contained in an array in no particular order. I would like to be able to take all variations of a team name by fuzzy matching them and rename all variants to one name. I'm working in python 2.7 and I have a numpy array with all of the data. Any help would be appreciated, as I have never used fuzzy matching before.

I have considered fuzzy matching through a for-loop, which would (despite being unbelievably slow) compare each element in the column of the array to every other element, but I'm not really sure how to build it.

Currently, my array looks like this:

{Names , info1, info2, info 3}

The array is a few thousand rows long, so I'm trying to make the program as efficient as possible.

解决方案

The Levenshtein edit distance is the most common way to perform fuzzy matching of strings. It is available in the python-Levenshtein package. Another popular distance is Jaro Winkler's distance, also available in the same package.

Assuming a simple array numpy array:

import numpy as np

import Levenshtein as lv

ar = np.array([

'string'

, 'stum'

, 'Such'

, 'Say'

, 'nay'

, 'powder'

, 'hiden'

, 'parrot'

, 'ming'

])

We define helpers to give us indexes of Levenshtein and Jaro distances, between a string we have and all strings in the array.

def levenshtein(dist, string):

return map(lambda x: x

def jaro(dist, string):

return map(lambda x: x

Now, note that Levenshtein distance is an integer value counted in number of characters, whilst Jaro's distance is a floating point value that normally varies between 0 and 1. Let's test this using np.where:

print ar[np.where(levenshtein(3, 'str'))]

print ar[np.where(levenshtein(5, 'str'))]

print ar[np.where(jaro(0.00000001, 'str'))]

print ar[np.where(jaro(0.9, 'str'))]

And we get:

['stum']

['string' 'stum' 'Such' 'Say' 'nay' 'ming']

['Such' 'Say' 'nay' 'powder' 'hiden' 'ming']

['string' 'stum' 'Such' 'Say' 'nay' 'powder' 'hiden' 'parrot' 'ming']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值