R,python,reticulate和limmbo:将数组“float64”强制转换为“int64”错误(R, python, reticulate, and limmbo: cast array

编程入门 行业动态 更新时间:2024-10-23 12:28:49
R,python,reticulate和limmbo:将数组“float64”强制转换为“int64”错误(R, python, reticulate, and limmbo: cast array 'float64' to 'int64' error)

我试图通过reticulate R包使用Python模块limmbo ( https://github.com/HannahVMeyer/limmbo )与R。 我已经用Anaconda2成功安装了limmbo 。 我现在正在尝试使用函数limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation ,如下面的代码所示。 当我运行下面的代码时,出现了有关将float64转换为整数64的错误。

```{r} library(reticulate) import("limmbo") -> limmbo ```

然后我运行python代码:

```{python} import numpy from numpy.random import RandomState from numpy.linalg import cholesky as chol from limmbo.core.vdsimple import vd_reml from limmbo.io.input import InputData random = RandomState(15) N = 100 S = 1000 P = 3 snps = (random.rand(N, S) < 0.2).astype(float) kinship = numpy.dot(snps, snps.T) / float(10) y = random.randn(N, P) pheno = numpy.dot(chol(kinship), y) pheno_ID = [ 'PID{}'.format(x+1) for x in range(P)] samples = [ 'SID{}'.format(x+1) for x in range(N)] datainput = InputData() datainput.addPhenotypes(phenotypes = pheno, phenotype_ID = pheno_ID, pheno_samples = samples) datainput.addRelatedness(relatedness = kinship, relatedness_samples = samples) ```

当我尝试运行R函数时出现问题limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation :

```{r} (limmbo$core$vdbootstrap$LiMMBo(py$datainput, timing = TRUE, iterations = 100, S = 2) -> foo) limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation(foo, cpus = 1, seed = 12345) ``` Error in py_call_impl(callable, dots$args, dots$keywords) : TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'safe' Detailed traceback: File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 96, in runBootstrapCovarianceEstimation minCooccurrence=minCooccurrence) File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 353, in __generateBootstrapMatrix rand_state = np.random.RandomState(seed) File "mtrand.pyx", line 644, in mtrand.RandomState.__init__ File "mtrand.pyx", line 687, in mtrand.RandomState.seed Calls: <Anonymous> ... eval -> eval -> <Anonymous> -> py_call_impl -> .Call Execution halted

I am trying to use the python module limmbo (https://github.com/HannahVMeyer/limmbo) with R via the reticulate R package. I have successfully installed limmbo with Anaconda2. I'm now trying to use the function limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation, as in my code below. When I run the code below, I get an error about converting a float64 to an integer64.

```{r} library(reticulate) import("limmbo") -> limmbo ```

I then run the python code:

```{python} import numpy from numpy.random import RandomState from numpy.linalg import cholesky as chol from limmbo.core.vdsimple import vd_reml from limmbo.io.input import InputData random = RandomState(15) N = 100 S = 1000 P = 3 snps = (random.rand(N, S) < 0.2).astype(float) kinship = numpy.dot(snps, snps.T) / float(10) y = random.randn(N, P) pheno = numpy.dot(chol(kinship), y) pheno_ID = [ 'PID{}'.format(x+1) for x in range(P)] samples = [ 'SID{}'.format(x+1) for x in range(N)] datainput = InputData() datainput.addPhenotypes(phenotypes = pheno, phenotype_ID = pheno_ID, pheno_samples = samples) datainput.addRelatedness(relatedness = kinship, relatedness_samples = samples) ```

The problem arises when I try to run the R function limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation:

```{r} (limmbo$core$vdbootstrap$LiMMBo(py$datainput, timing = TRUE, iterations = 100, S = 2) -> foo) limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation(foo, cpus = 1, seed = 12345) ``` Error in py_call_impl(callable, dots$args, dots$keywords) : TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'safe' Detailed traceback: File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 96, in runBootstrapCovarianceEstimation minCooccurrence=minCooccurrence) File "/Users/frederickboehm/anaconda2/lib/python2.7/site-packages/limmbo/core/vdbootstrap.py", line 353, in __generateBootstrapMatrix rand_state = np.random.RandomState(seed) File "mtrand.pyx", line 644, in mtrand.RandomState.__init__ File "mtrand.pyx", line 687, in mtrand.RandomState.seed Calls: <Anonymous> ... eval -> eval -> <Anonymous> -> py_call_impl -> .Call Execution halted

最满意答案

首先,通过np <- import("numpy", convert = FALSE)导入你的numpy模块。

然后你可以使用reticulate::np_array(datainput, dtype = np$int64)重新创建你的numpy数组。

您可以在本教程中学习更多关于如何操作和创建数组的知识。

希望这可以帮助。

First of all, import you numpy module via np <- import("numpy", convert = FALSE).

And then you can re-create your numpy array with explicit type int64 by using reticulate::np_array(datainput, dtype = np$int64).

You can learn more about how to manipulate and create your arrays in this tutorial.

Hope this helps.

更多推荐

本文发布于:2023-08-02 19:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   错误   limmbo   reticulate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!