在R中聚类字符串(可能吗?)

Har*_*eno 4 csv r cluster-analysis machine-learning

我有一个列的数据集,目前被视为1000+级别的因素.这些是列的值.我想清理这些数据.有些值是字符串,如"-18 + 5 = -13"和"5 - 18 = -13",我希望聚类以不同于"R3no4"的方式对这些进行分组.

这可能在R?我查看了自然语言处理任务视图http://cran.r-project.org/web/views/NaturalLanguageProcessing.html但我需要向正确的方向推进.

数据集来自kdd 2010杯 我想从这个专栏创建有意义的新专栏,以帮助创建预测模型.例如,知道字符串是否包含某个操作,或者它是否包含没有操作而是描述问题会很好.

我的数据框看起来像这样:

str(data1)
'data.frame':   809694 obs. of  19 variables:
 $ Row                        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Anon.Student.Id            : Factor w/ 574 levels "02i5jCrfQK","02ZjVTxC34",..: 7 7     7 7 7 7 7 7 7 7 ...
 $ Problem.Hierarchy          : Factor w/ 138 levels "Unit CTA1_01, Section CTA1_01-1",..: 80 80 80 80 80 80 80 80 80 80 ...
 $ Problem.Name               : Factor w/ 1084 levels "1PTB02","1PTB03",..: 377 377 378 378 378 378 378 378 378 378 ...
 $ Problem.View               : int  1 1 1 1 2 2 3 3 4 4 ...
 $ Step.Name                  : Factor w/ 187539 levels "-(-0.24444444-y) = -0.93333333",..: 116742 177541 104443 64186 58776 58892 153246 153078 45114 163923 ...
Run Code Online (Sandbox Code Playgroud)

我对Step.Name功能最感兴趣,因为它包含最多的唯一因子值.

以及步骤名称的一些示例值:

[97170] (1+7)/4 = x                                                               
[97171] (1-sqrt(1^2-4*2*-6))/4 = x                                                
[97172] (1-sqrt(1^2-(-48)))/4 = x                                                 
[97173] (1-sqrt(1-(-48)))/4 = x                                                   
[97174] (1-sqrt(49))/4 = x                                                        
[97175] (1-7)/4 = x                                                               
[97176] x^2+15x+44 = 0                                                            
[97177] a-factor-node                                                             
[97178] b-factor-node                                                             
[97179] c-factor-node                                                             
[97180] num1-factor-node                                                          
[97181] num2-factor-node                                                          
[97182] den1-factor-node                                                          
[97183] (-15?sqrt((-15)^2-4*1*44))/2 = x                                          
[97184] (-15+sqrt((-15)^2-4*1*44))/2 = x                                          
[97185] (-15+sqrt((-15)^2-176))/2 = x                                             
[97186] (-15+sqrt(225-176))/2 = x                                                 
[97187] (-15+sqrt(49))/2 = x                                                      
[97188] (-15+7)/2 = x                                                             
[97189] (-15-sqrt((-15)^2-4*1*44))/2 = x                                          
[97190] (-15-sqrt((-15)^2-176))/2 = x                                             
[97191] (-15-sqrt(225-176))/2 = x                                                 
[97192] (-15-sqrt(49))/2 = x                                                      
[97193] (-15-7)/2 = x                                                             
[97194] 2x^2+x = 0                                                                
[97195] a-factor-node                                                             
[97196] b-factor-node                                                             
[97197] c-factor-node                                                             
[97198] num1-factor-node                                                          
[97199] num2-factor-node                                                          
[97200] den1-factor-node                                                          
[97201] (-1?sqrt((-1)^2-4*2*0))/4 = x                                             
[97202] (-1+sqrt((-1)^2-4*2*0))/4 = x                                             
[97203] (-1+sqrt((-1)^2-0))/4 = x                                                 
[97204] (-1+sqrt((-1)^2))/4 = x                                                   
[97205] (-1+1)/4 = x                                                              
[97206] (-1-sqrt((-1)^2-4*2*0))/4 = x                                             
[97207] (-1-sqrt((-1)^2-0))/4 = x                                                 
[97208] (-1-sqrt((-1)^2))/4 = x                                                   
[97209] (-1-1)/4 = x                                                              
[97210] x^2-6x = 0                                                                
[97211] a-factor-node                                                             
[97212] b-factor-node                                                                
Run Code Online (Sandbox Code Playgroud)

dou*_*oug 9

聚类只是根据某个度量对数据数组中的每个实例进行评分,根据此计算得分对数据数组进行排序,然后切片到一些段,每个段分配一个标签.

换句话说,您可以对任何可以形成一些有意义函数的数据进行聚类,以计算每个数据点的相似度w/r/t; 这通常被称为相似性度量.

这些中有很多,但只有一小部分可用于评估字符串.其中,最常用的可能是Levenshtein Distance(又名编辑距离).

此度量标准以整数表示,并为每个"编辑"增加一个单位(+1) - 插入,删除或更改字母 - 需要将一个单词转换为另一个单词.将这些单独的编辑(每个字母一个)相加可以得到Levenshtein距离.

R Package vwr包含一个实现:

> library(vwr)
> levenshtein.distance('cat', 'hat')
    hat 
    1 
> levenshtein.distance('cat', 'catwalk')
    catwalk 
    4 
> levenshtein.distance('catwalk', 'sidewalk')
    sidewalk 
    4

> # using a data set supplied with the vmr library 
> EW = english.words
> ew1 = sample(EW, 20)     # random select 20 words from EW
> # the second argument is a vector of words, returns a vector of distances
> dx = levenshtein.distance('cat', ew1)
> dx
furriers       graves      crooned    cursively       gabled   caparisons   drainpipes 
    8            5            6            8            5            8            9 
patricians     medially     beholder   chirpiness    fluttered     bobolink   lamentably 
    8            7            8            9            8            8            8 
depredations      alights    unearthed     thimbles    supersede   dissembler 
    10            6            7            8            9           10
Run Code Online (Sandbox Code Playgroud)

虽然Levenshtein Distance 用于聚类您的数据,但是它是否应该用于您的数据是一个我将留给您的问题(即,L/D的主要用例显然是纯文本数据).

(也许对字符串进行操作的下一个最常见的相似性度量是汉明距离.汉明距离(与Levenshtein不同)要求两个字符串长度相等,因此它不适用于您的数据.)