How does clustering (especially String clustering) work?

前端 未结 3 1184
轻奢々
轻奢々 2020-12-07 17:56

I heard about clustering to group similar data. I want to know how it works in the specific case for String.

I have a table with more than different 100,000 words. <

相关标签:
3条回答
  • 2020-12-07 18:29

    You can use an algorithm like the Levenshtein distance for the distance calculation and k-means for clustering.

    the Levenshtein distance is a string metric for measuring the amount of difference between two sequences

    Do some testing and find a similarity threshold per word that will decide your groups.

    0 讨论(0)
  • 2020-12-07 18:33

    There is a package called stringdist that allows for string comparison using several different methods. Copypasting from that page:

    • Hamming distance: Number of positions with same symbol in both strings. Only defined for strings of equal length.
    • Levenshtein distance: Minimal number of insertions, deletions and replacements needed for transforming string a into string b.
    • (Full) Damerau-Levenshtein distance: Like Levenshtein distance, but transposition of adjacent symbols is allowed.
    • Optimal String Alignment / restricted Damerau-Levenshtein distance: Like (full) Damerau-Levenshtein distance but each substring may only be edited once.
    • Longest Common Substring distance: Minimum number of symbols that have to be removed in both strings until resulting substrings are identical.
    • q-gram distance: Sum of absolute differences between N-gram vectors of both strings.
    • Cosine distance: 1 minus the cosine similarity of both N-gram vectors.
    • Jaccard distance: 1 minues the quotient of shared N-grams and all observed N-grams.
    • Jaro distance: The Jaro distance is a formula of 4 values and effectively a special case of the Jaro-Winkler distance with p = 0.
    • Jaro-Winkler distance: This distance is a formula of 5 parameters determined by the two compared strings (A,B,m,t,l) and p chosen from [0, 0.25].

    That will give you the distance. You might not need to perform a cluster analysis, perhaps sorting by the string distance itself is sufficient. I have created a script to provide the basic functionality here... feel free to improve it as needed.

    0 讨论(0)
  • 2020-12-07 18:36

    To understand what clustering is imagine a geographical map. You can see many distinct objects (such as houses). Some of them are close to each other, and others are far. Based on this, you can split all objects into groups (such as cities). Clustering algorithms make exactly this thing - they allow you to split your data into groups without previous specifying groups borders.

    All clustering algorithms are based on the distance (or likelihood) between 2 objects. On geographical map it is normal distance between 2 houses, in multidimensional space it may be Euclidean distance (in fact, distance between 2 houses on the map also is Euclidean distance). For string comparison you have to use something different. 2 good choices here are Hamming and Levenshtein distance. In your particular case Levenshtein distance if more preferable (Hamming distance works only with the strings of same size).

    Now you can use one of existing clustering algorithms. There's plenty of them, but not all can fit your needs. For example, pure k-means, already mentioned here will hardly help you since it requires initial number of groups to find, and with large dictionary of strings it may be 100, 200, 500, 10000 - you just don't know the number. So other algorithms may be more appropriate.

    One of them is expectation maximization algorithm. Its advantage is that it can find number of clusters automatically. However, in practice often it gives less precise results than other algorithms, so it is normal to use k-means on top of EM, that is, first find number of clusters and their centers with EM and then use k-means to adjust the result.

    Another possible branch of algorithms, that may be suitable for your task, is hierarchical clustering. The result of cluster analysis in this case in not a set of independent groups, but rather tree (hierarchy), where several smaller clusters are grouped into one bigger, and all clusters are finally part of one big cluster. In your case it means that all words are similar to each other up to some degree.

    0 讨论(0)
提交回复
热议问题