[英]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.