无法导入rPython包

2024-09-27 09:22:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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])
```

我得到了一个错误:

Error in python.exec(python.command) : No module named umap

所以,显然Rstudio没有看到{}。我检查了包是否由conda list安装:

^{pr2}$

我怎么能修好呢?在

Update

python的版本是错误的,所以我添加了.Rprofile并使其指向正确的版本,但是错误仍然存在。在

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

Update

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

 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])

Tags: inirisdatadist错误neighborsrescall
1条回答
网友
1楼 · 发布于 2024-09-27 09:22:06

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

您可以使用system("python version")来检查您的Python,如果需要,可以在cmd行执行pip install umap或{}等操作(使用当前可用的Python;或者,switch您的PythonPython的Python路径)。在

相关问题 更多 >

    热门问题