我有一个数据集,其中有一列当前被视为具有1000+级别的因子。这些是列的值。我想要清理这些数据。一些值是像"-18 +5= -13“和"5 - 18 = -13”这样的字符串,我希望集群对这些字符串进行不同的分组,而不是说"R3no4“。
这在R中是可能的吗?我查看了自然语言处理任务视图http://cran.r-project.org/web/views/NaturalLanguageProcessing.html,但我需要被推向正确的方向。
数据集来自kdd 2010 cup,我想从这个列创建有意义的新列,以帮助创建预测模型。例如,如果知道字符串是否包含某个操作,或者它是否不包含操作,而是描述问题,那就更好了。
我的数据框如下所示:
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特性,因为它包含最多的唯一因子值。
以及步骤名称的一些示例值:
[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 发布于 2011-12-12 12:12:43
聚类只是根据某种度量对数据数组中的每个实例进行评分,根据计算出的分数对数据数组进行排序,然后将其切片为一定数量的片段,并为每个片段分配一个标签。
换句话说,您可以对任何数据进行聚类,对于这些数据,您可以制定一些有意义的函数来计算每个数据点与其他数据点的相似性;这通常被称为相似性度量标准。
有这样的a lot,但它们中只有一小部分对计算字符串有用。其中,最常用的可能是编辑距离(也称为编辑距离)。
这个度量被表示为一个整数,并且对于将一个单词转换为另一个单词所需的每个“编辑”(插入、删除或更改字母),它都会递增一个单位(+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虽然Levenshtein距离可以用来聚类你的数据,但它是否应该用于你的数据是一个问题,我将留给你一个问题(即,L/D的主要用例显然是纯文本数据)。
(也许在字符串上操作的下一个最常见的相似性度量是汉明距离。Hamming Distance (与Levenshtein不同)要求两个字符串的长度相等,因此它不适用于您的数据。)
发布于 2011-12-12 11:05:58
也许:
> grepl("[[:alpha:]]", c("-18 + 5 = -13", "5 - 18 = -13","R3no4") )
[1] FALSE FALSE TRUEhttps://stackoverflow.com/questions/8468716
复制相似问题