【问题标题】:Text Mining: Cluster Analysis phrases. ERROR: cannot take a sample larger than the population文本挖掘:聚类分析短语。错误:无法抽取大于总体的样本
【发布时间】:2021-12-30 09:15:24
【问题描述】:

我正在处理包含数千个句子的数据集。数据集由一列和 k 行构成。 我必须找到它们之间的一些相似之处,例如我正在进行聚类分析。我创建了一个语料库并将其转换为数字(TF-IDF),然后我开始进行聚类分析。当我做 kmeans 时,它给了我错误:“无法获取大于总体的样本”,我无法继续分析。 我认为问题出在语料库中。也许它应该由 totK 文件而不是带有 tot 行的文件组成。 我在互联网上找到了一些类似的问题,但我没有找到解决方案。 下面是代码,谢谢

####Stupid example of my dataset
column <- c("hi everyon, i'm Gio"," I'm Luisa, nice to meet you","How are you?", "Good morning. i'm Josh","Hello, Is Luca ok?")
df <- data.frame(column)
    corpus = tm::Corpus(tm::VectorSource(ticket_data1), readerControl = list(readerControl=readPlain))

corpus.cleaned <- tm::tm_map(corpus, tm::removeWords, tm::stopwords('english'))    #### stop-words

corpus.cleaned <- tm::tm_map(corpus, tm::stemDocument, language = "english")  

corpus.cleaned <- tm::tm_map(corpus.cleaned, tm::stripWhitespace) 

tdm <- tm::DocumentTermMatrix(corpus) 
tdm.tfidf <- tm::weightTfIdf(tdm)


tdm.tfidf <- tm::removeSparseTerms(tdm.tfidf, 0.999) 
tfidf.matrix <- as.matrix(tdm.tfidf) 

dist.matrix = proxy::dist(tfidf.matrix, method = "cosine")
k<- kmeans(tfidf.matrix, centers = 2, nstart = 25)

我强调我的数据集大约有 10k 行。这是一个简化的例子。 我希望我很清楚,你可以帮助我。 谢谢

【问题讨论】:

    标签: r cluster-analysis tm corpus


    【解决方案1】:

    在将tm::VectorSource(ticket_data1) 替换为tm::VectorSource(column) 后,您的示例对我有用。不过,我仍然收到一些警告。

    也许是一般建议:使用quantedatidytext 而不是tm。这两个软件包都更快,采用现代方法并且更加一致。这相当于你在quanteda 中所做的事情:

    column <- c("hi everyon, i'm Gio"," I'm Luisa, nice to meet you","How are you?", "Good morning. i'm Josh","Hello, Is Luca ok?")
    
    library(quanteda)
    dist.matrix <- tokens(column) %>% 
      tokens_remove(stopwords(language = "en")) %>% 
      dfm() %>% 
      dfm_trim(sparsity = 0.999) %>% 
      dfm_tfidf() %>% 
      quanteda.textstats::textstat_simil(method = "cosine")
    

    kmeans 在这种情况下完美运行:

    set.seed(1)
    kmeans(dist.matrix, centers = 2, nstart = 25)
    #> K-means clustering with 2 clusters of sizes 2, 3
    #> 
    #> Cluster means:
    #>        text1      text2    text3     text4      text5
    #> 1 0.01545654 0.01545654 0.653818 0.0000000 0.65381800
    #> 2 0.34416288 0.34416288 0.000000 0.3333333 0.02060872
    #> 
    #> Clustering vector:
    #> text1 text2 text3 text4 text5 
    #>     2     2     1     2     1 
    #> 
    #> Within cluster sum of squares by cluster:
    #> [1] 0.4803235 1.9587262
    #>  (between_SS / total_SS =  36.2 %)
    #> 
    #> Available components:
    #> 
    #> [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
    #> [6] "betweenss"    "size"         "iter"         "ifault"
    

    reprex package (v2.0.1) 于 2021 年 11 月 19 日创建

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 2019-06-04
      • 2019-05-05
      • 2017-04-10
      • 1970-01-01
      • 2014-11-02
      • 2011-02-07
      • 2018-04-04
      • 2012-01-25
      相关资源
      最近更新 更多