首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Ops.simple_triplet_matrix(m,1)中使用R-错误中的lsa包:不兼容尺寸

在Ops.simple_triplet_matrix(m,1)中使用R-错误中的lsa包:不兼容尺寸
EN

Stack Overflow用户
提问于 2015-06-09 15:41:24
回答 1查看 1.6K关注 0票数 2

我正在努力学习使用R中的lsa包,我正在使用比下面示例大得多的数据集,但这是为了可再现性(支持这个人在他的站点上发布这段代码,这是一个很好的资源)。

我收到一条奇怪的错误信息,似乎无法解决:

代码语言:javascript
复制
Error in Ops.simple_triplet_matrix(m, 1) : Incompatible dimensions. 

下面是我正在修改的一些代码:

代码语言:javascript
复制
# load required libraries
library(tm)
library(ggplot2)
library(lsa)
library(SnowballC)
lsa <- function () {
# 1. Prepare mock data
text <- c("transporting food by cars will cause global warming. so we should go     local.",
          "we should try to convince our parents to stop using cars because it     will cause global warming.",
          "some food, such as mongo, requires a warm weather to grow. so they     have to be transported to canada.",
          "a typical Electronic Circuit can be built with a battery, a bulb, and     a switch.",
          "electricity flows from batteries to the bulb, just like water flows     through a tube.",
          "batteries have chemical energe in it. then electrons flow through a     bulb to light it up.",
          "birds can fly because they have feather and they are light.", "why     some birds like pigeon can fly while some others like chicken cannot?",
          "feather is important for birds' fly. if feather on a bird's wings is     removed, this bird cannot fly.")
view <- factor(rep(c("view 1", "view 2", "view 3"), each = 3))
df <- data.frame(text, view, stringsAsFactors = FALSE)

# prepare corpus
corpus <- Corpus(VectorSource(df$text))
# corpus <- tm_map(corpus, tolower)
# corpus <- tm_map(corpus, removePunctuation)
# corpus <- tm_map(corpus, function(x) removeWords(x, stopwords("english")))
# corpus <- tm_map(corpus, stemDocument, language = "english")
corpus <- tm_map(corpus, PlainTextDocument)

# 2. MDS with raw term-document matrix compute distance matrix
td.mat <- TermDocumentMatrix(corpus)

td.mat.lsa <- lw_logtf(td.mat) * gw_idf(td.mat)  # weighting
lsaSpace <- lsa(td.mat.lsa)  # create LSA space
dist.mat.lsa <- dist(t(as.textmatrix(lsaSpace)))  # compute distance matrix
return(dist.mat.lsa)  # check distance matrix

}

我可以生成没有问题的语料库,也可以把它转换成一个术语文档矩阵。当我定义dt.mat.lsa时会触发错误。

追溯如下:

代码语言:javascript
复制
4 stop("Incompatible dimensions.") 
3 Ops.simple_triplet_matrix(m, 1) 
2 lw_logtf(td.mat) at lsa.R#31
1 lsa() 

因此,我的主要问题是:

  1. 为什么我会有这个错误?
  2. 如何修正我的代码以避免这样的错误?

谢谢你能在这里提供的任何帮助,这是我的第一篇帖子,所以我的问题质量反馈也是欢迎的!

EN

回答 1

Stack Overflow用户

发布于 2015-06-18 08:26:31

已经算出来了!

我将代码包装在'lsa‘函数调用中,并在函数正文中使用'lsa’作为变量名。因此,它具有不兼容的维度,因为lsa是在此环境中定义不同的函数。

哎呀!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30736756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档