我有一个数据集,其中有一列当前被视为具有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 ...
我最感兴趣的是Step.Name特征,因为它包含了最多的唯一因子值。
以下是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
回答:
聚类只是根据某种度量对数据数组中的每个实例进行评分,根据这个计算出的分数对数据数组进行排序,然后将其切割成若干段,并为每一段分配一个标签。
换句话说,你可以对任何你能为其制定出有意义的函数来计算每个数据点与其他数据点相似度的数据进行聚类;这通常被称为相似度度量。
这些度量有很多,但只有其中的一小部分对评估字符串有用。其中,可能最常用的是Levenshtein距离(也称为编辑距离)。
这个度量以整数形式表达,每次“编辑”——插入、删除或更改一个字母——需要将一个词转换成另一个词时,该度量增加一个单位(+1)。将这些个别编辑(每个字母一个)相加,就得到了Levenshtein距离。
R包vwr包含了一个实现:
> library(vwr)> levenshtein.distance('cat', 'hat') hat 1 > levenshtein.distance('cat', 'catwalk') catwalk 4 > levenshtein.distance('catwalk', 'sidewalk') sidewalk 4> # 使用vmr库提供的数据集 > EW = english.words> ew1 = sample(EW, 20) # 从EW中随机选择20个词> # 第二个参数是一个词向量,返回一个距离向量> dx = levenshtein.distance('cat', ew1)> dxfurriers 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
虽然Levenshtein距离可以用于聚类你的数据,但它是否应该用于你的数据,这是一个我留给你决定的问题(即,L/D的主要用例显然是纯文本数据)。
(可能在字符串上操作的下一个最常见的相似度度量是Hamming距离。与Levenshtein不同,Hamming距离要求两个字符串长度相等,因此它不适用于你的数据。)