【问题标题】:can't import package with rPython无法使用 rPython 导入包
【发布时间】:2018-10-14 03:37:06
【问题描述】:

我在mac OS 上安装了umap-learn,并尝试使用rPythonr markdown 文件中使用它,这里解释的方式如下:

http://blog.schochastics.net/post/using-umap-in-r-with-rpython/#fn1

但是当我运行以下代码时:

```{r}
umap <- function(x,n_neighbors=10,min_dist=0.1,metric="euclidean"){
  x <- as.matrix(x)
  colnames(x) <- NULL
  rPython::python.exec( c( "def umap(data,n,mdist,metric):",
              "\timport umap" ,
              "\timport numpy",
              "\tembedding = umap.UMAP(n_neighbors=n,min_dist=mdist,metric=metric).fit_transform(data)",
              "\tres = embedding.tolist()",
              "\treturn res"))

  res <- rPython::python.call( "umap", x,n_neighbors,min_dist,metric)
  do.call("rbind",res)
}

data(iris)
res <- umap(iris[,1:4])
```

我得到错误:

python.exec(python.command) 中的错误:没有名为 umap 的模块

所以,显然Rstudio 看不到umap。我检查了包是conda list安装的:

umap-learn                0.2.3                    py36_0    conda-forge

我该如何解决这个问题?

更新

python的版本不对,所以我添加了.Rprofile,并让它指向正确的版本,但是错误仍然存​​在。

system("python --version")
Python 3.6.5 :: Anaconda, Inc.

更新

更详细的错误(堆栈跟踪):

 Error in python.exec(python.command) : No module named umap
 4.stop(ret$error.desc)
 3.python.exec(python.command)
 2.rPython::python.call("umap", x, n_neighbors, min_dist, metric)
 1.umap(iris[, 1:4])

【问题讨论】:

  • 不,我该如何验证?错了再切换?

标签: python r rpython


【解决方案1】:

您需要确保 umap 可用于 R 中使用的同一 Python,默认情况下不会是您的 Anaconda 安装。

您可以使用 system("python --version") 检查您的 Python,如果需要,请转到 cmd 行并执行 pip install umappip3 install umap 等(以使用当前可用的 Python;或者您可以 switch Python 路径你的 Anaconda Python)。

【讨论】:

  • 只是补充一下,以下线程帮助我切换到正确的python版本:stackoverflow.com/questions/36705878/…
  • 但是,仍然找不到umap
  • pip3 可能没有安装,因为它正在写入command not found。错误是一样的No module named umap
  • 我跑成功了pip install umap但是没有修复错误。
  • R 版本为3.5.0
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 2020-07-18
  • 1970-01-01
相关资源
最近更新 更多