文字列の比較

Rでのデータクリーニング

Maggie Matsui

Content Developer @ DataCamp

値間の距離を測る

0〜15 の数直線。3 と 10 に点。

Rでのデータクリーニング

値間の距離を測る

同じ数直線で、3 と 10 の間の長さを赤い括弧で強調。

Rでのデータクリーニング

値間の距離を測る

距離を示す赤線の下に、10 マイナス 3 は 7 と表示。

                                            typhoon と baboon の距離は?

Rでのデータクリーニング

最小編集距離

一方の文字列からもう一方へ行くのに必要な誤字は何回か?

挿入を示す緑のプラス記号。

Rでのデータクリーニング

最小編集距離

一方の文字列からもう一方へ行くのに必要な誤字は何回か?

削除を示す赤のマイナス記号

Rでのデータクリーニング

最小編集距離

一方の文字列からもう一方へ行くのに必要な誤字は何回か?

置換を示す紫の循環アイコン。

Rでのデータクリーニング

最小編集距離

一方の文字列からもう一方へ行くのに必要な誤字は何回か?

転置を示す青の両矢印。

Rでのデータクリーニング

編集距離 = 1

dog と dogs の違いを緑のプラス記号で示す。

Rでのデータクリーニング

編集距離 = 1

bath と bat の違いを赤のマイナス記号で示す。

Rでのデータクリーニング

編集距離 = 1

cats と rats の違いを紫の循環アイコンで示す。

Rでのデータクリーニング

編集距離 = 1

sign と sign の違いを青の両矢印で示す。

Rでのデータクリーニング

もう少し複雑な例

baboon $\rightarrow$ typhoon

baboon という単語

Rでのデータクリーニング

もう少し複雑な例

baboon $\rightarrow$ typhoon
  • h を挿入

baboon の下に babhoon。h の上に緑のプラス記号。

Rでのデータクリーニング

もう少し複雑な例

baboon $\rightarrow$ typhoon
  • h を挿入
  • bt に置換

その下に tabhoon。t の上に紫の循環アイコン。

Rでのデータクリーニング

もう少し複雑な例

baboon $\rightarrow$ typhoon
  • h を挿入
  • bt に置換
  • ay に置換

その下に tybhoon。y の上に紫の循環アイコン。

Rでのデータクリーニング

もう少し複雑な例

baboon $\rightarrow$ typhoon
  • h を挿入
  • bt に置換
  • ay に置換
  • bp に置換

合計: 4

その下に typhoon。p の上に紫の循環アイコン。

Rでのデータクリーニング

編集距離の種類

  • Damerau-Levenshtein
    • いま学んだ手法
  • Levenshtein
    • 置換・挿入・削除のみを考慮
  • LCS (最長共通部分列)
    • 挿入・削除のみを考慮
  • その他
    • Jaro-Winkler
    • Jaccard

どれが最適か?

Rでのデータクリーニング

R での文字列距離

library(stringdist)
stringdist("baboon", 
           "typhoon",
           method = "dl")
4

前のスライドの baboon から typhoon への変換図。

Rでのデータクリーニング

他の手法

# LCS
stringdist("baboon", "typhoon",
           method = "lcs")
7
# Jaccard
stringdist("baboon", "typhoon",
           method = "jaccard")
0.75
Rでのデータクリーニング

データクレンジングのための文字列比較

  • 第2章では:
    • "EU", "eur", "Europ" $\rightarrow$ "Europe"
  • 変種が多すぎる場合は?
    • "EU", "eur", "Europ", "Europa", "Erope", "Evropa", ... $\rightarrow$ "Europe"?
    • 文字列距離を使う!
Rでのデータクリーニング

データクレンジングのための文字列比較

survey
          city move_score
1       chicgo          4
2   los angles          4
3      chicogo          5
4      new yrk          5
5    new yoork          2
6     seatttle          3
7   losangeles          4
8      seeatle          2
...
cities
         city
1    new york
2     chicago
3 los angeles
4     seattle
Rでのデータクリーニング

文字列距離で再マッピング

library(fuzzyjoin)
stringdist_left_join(survey, cities, by = "city", method = "dl")
        city.x move_score      city.y
1       chicgo          4     chicago
2   los angles          4 los angeles
3      chicogo          5     chicago
4      new yrk          5    new york
5    new yoork          2    new york
6     seatttle          3     seattle
7   losangeles          4 los angeles
8      seeatle          2     seattle
9      siattle          1     seattle
...
Rでのデータクリーニング

文字列距離で再マッピング

stringdist_left_join(survey, cities, by = "city", method = "dl", max_dist = 1)
        city.x move_score      city.y
1       chicgo          4     chicago
2   los angles          4 los angeles
3      chicogo          5     chicago
4      new yrk          5    new york
5    new yoork          2    new york
6     seatttle          3     seattle
7   losangeles          4 los angeles
8      seeatle          2        <NA>
9      siattle          1     seattle
...
Rでのデータクリーニング

Ayo berlatih!

Rでのデータクリーニング

Preparing Video For Download...