文字列の比較

Pythonで学ぶデータクリーニング

Adel Nehme

VP of AI Curriculum, DataCamp

この章について

 

 

 

 

 

 

第4章 - レコードリンケージ

Pythonで学ぶデータクリーニング

最小編集距離

一方の文字列から他方へ最小の手順で変換

Pythonで学ぶデータクリーニング

最小編集距離

一方の文字列から他方へ最小の手順で変換

Pythonで学ぶデータクリーニング

最小編集距離

Pythonで学ぶデータクリーニング

最小編集距離

ここまでの最小編集距離: 2

Pythonで学ぶデータクリーニング

最小編集距離

最小編集距離: 5

Pythonで学ぶデータクリーニング

最小編集距離

 

Pythonで学ぶデータクリーニング

最小編集距離のアルゴリズム

アルゴリズム 操作
Damerau-Levenshtein 挿入、置換、削除、転置
Levenshtein 挿入、置換、削除
Hamming 置換のみ
Jaro distance 転置のみ
... ...

 

利用可能なパッケージ: nltk, thefuzz, textdistance ..

Pythonで学ぶデータクリーニング

最小編集距離のアルゴリズム

アルゴリズム 操作
Damerau-Levenshtein 挿入、置換、削除、転置
Levenshtein _挿入_, _置換_, _削除_
Hamming 置換のみ
Jaro distance 転置のみ
... ...

 

利用可能なパッケージ: thefuzz

Pythonで学ぶデータクリーニング

簡単な文字列比較

# Lets us compare between two strings
from thefuzz import fuzz

# Compare reeding vs reading fuzz.WRatio('Reeding', 'Reading')
86
Pythonで学ぶデータクリーニング

部分一致と順序の違い

# Partial string comparison
fuzz.WRatio('Houston Rockets', 'Rockets')
90
# Partial string comparison with different order
fuzz.WRatio('Houston Rockets vs Los Angeles Lakers', 'Lakers vs Rockets')
86
Pythonで学ぶデータクリーニング

配列との比較

# Import process
from thefuzz import process

# Define string and array of possible matches
string = "Houston Rockets vs Los Angeles Lakers"
choices = pd.Series(['Rockets vs Lakers', 'Lakers vs Rockets', 
                     'Houson vs Los Angeles', 'Heat vs Bulls'])

process.extract(string, choices, limit = 2)
[('Rockets vs Lakers', 86, 0), ('Lakers vs Rockets', 86, 1)]
Pythonで学ぶデータクリーニング

文字列類似度でカテゴリを統合

第2章

.replace()"eur""Europe" に統合

 

バリエーションが多すぎる場合は?

"EU", "eur", "Europ", "Europa", "Erope", "Evropa"...

 

                                                                                                文字列類似度!

Pythonで学ぶデータクリーニング

文字列マッチングでカテゴリを統合

print(survey['state'].unique())
id          state
0      California
1            Cali
2      Calefornia
3      Calefornie
4      Californie
5       Calfornia
6      Calefernia
7        New York
8   New York City
...
categories
  state
0 California
1 New York
Pythonで学ぶデータクリーニング

州名を一括統合

# For each correct category
for state in categories['state']:

# Find potential matches in states with typoes matches = process.extract(state, survey['state'], limit = survey.shape[0])
# For each potential match match for potential_match in matches: # If high similarity score if potential_match[1] >= 80:
# Replace typo with correct category survey.loc[survey['state'] == potential_match[0], 'state'] = state
Pythonで学ぶデータクリーニング

レコードリンケージ

レコードリンケージ

Pythonで学ぶデータクリーニング

Passons à la pratique !

Pythonで学ぶデータクリーニング

Preparing Video For Download...