简体   繁体   English

如何使用网状结构从R运行Python gensim函数

[英]How to run a Python gensim functions from R with reticulate

I'd like to use the reticulate package to run gensim from R. I'm not sure I fully understand the syntax of reticulate because I can get this to work with the default function settings but it fails when I try to pass more arguments. 我想使用reticulate包从R运行gensim。我不确定我是否完全理解reticulate的语法,因为我可以使它与默认函数设置一起使用,但是当我尝试传递更多参数时会失败。

library(reticulate)
gensim <- import("gensim")

model<-gensim$models$Word2Vec$load("word2vec_gensim")

matrix(unlist(model$wv$most_similar("queen")),ncol=2,byrow=T)
      [,1]                           [,2]               
 [1,] "princess"                     "0.76466166973114" 
 [2,] "king"                         "0.728749990463257"
 [3,] "prince"                       "0.653270363807678"
 [4,] "lady"                         "0.611525416374207"
 [5,] "consort"                      "0.609499335289001"
 [6,] "duchess"                      "0.608054518699646"
 [7,] "monarch"                      "0.606827557086945"
 [8,] "lady-in-waiting"              "0.605596661567688"
 [9,] "empress"                      "0.602727890014648"
 [10,] "wiki/margrethe_ii_of_denmark" "0.59738427400589"

but... 但...

matrix(unlist(model$wv$most_similar("queen",topn = 25)),ncol=2,byrow=T)

Error in py_call_impl(callable, dots$args, dots$keywords) : 
TypeError: Partition index must be integer

Here "word2vec_gensim" is a pre-trained model, I can't include it because it is a large file but pick your fav pre-trained model. 这里的“ word2vec_gensim”是一个预先训练的模型,我不能包含它,因为它是一个大文件,但是请选择您最喜欢的预先训练的模型。 I think my issue is in how I'm providing additional args to the python function. 我认为我的问题在于我如何为python函数提供附加的args。

EDIT:I figured it out 编辑:我想通了

looks like the R to python communication doesn't handle numbers as expected. 看起来R与python的通讯无法按预期处理数字。

matrix(unlist(model$wv$most_similar("queen",topn = as.integer(25))),ncol=2,byrow=T)

works 作品

You are correct that reticulate doesn't handle the automatic typecasting. 您说对了,网状结构不会处理自动类型转换。 There is no way to programmatically determine this unless knowing specifically whether a particular Python parameter needs to be an integer or a float and then intercede, which is impossible in Python since Python is a typeless language. 除非明确知道某个特定的Python参数是否需要为整数或浮点数然后进行求值,否则无法以编程方式确定这一点,这在Python中是不可能的,因为Python是一种无类型的语言。

You can get around it by explicitly casting your arguments as integers using as.integer(25) or you can use 25L instead of 25 . 您可以通过使用as.integer(25)将参数显式转换为整数来解决它,也可以使用25L而不是25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从R网格调用Python方法 - How to call Python method from R reticulate 子进程从 Python 运行,但在使用 R 和 Reticulate 时不起作用 - Subprocess run works from Python, but not when using R and Reticulate 如何在不使用 R 网状包的情况下从 Anaconda 在 RStudio 中运行 Python 代码(直接使用 Python 解释器,无需任何 R 参与 - How to run Python code in RStudio from Anaconda without using R reticulate package (directly with Python interpreter without any R involvement R reticulate 从 Github 安装 python 库 - R reticulate install python libraries from Github 使用 Reticulate 从 R 导入 Python package(熊猫) - Importing Python package (pandas) from R with Reticulate 使用 reticulate 包从 R 调用 Python - Calling Python from R with reticulate package 如何在 R 中使用 Reticulate 导入 python 包 - How to import python packages using Reticulate in R ImportError:在R中通过网状运行gensim时无法从&#39;urllib3.util.ssl导入名称&#39;ssl&#39; - ImportError: cannot import name 'ssl' from 'urllib3.util.ssl when running gensim in R via reticulate R Reticulate - 以编程方式将定义的变量从 Python 环境移动到 R - R Reticulate - Moving defined variables programmatically from Python environment to R 将数据从R网格化为python并再次返回到R. - Reticulate data from R to python and back to R again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM