简体   繁体   English

R,Python,网状和Limmbo:将数组'float64'转换为'int64'错误

[英]R, python, reticulate, and limmbo: cast array 'float64' to 'int64' error

I am trying to use the python module limmbo ( https://github.com/HannahVMeyer/limmbo ) with R via the reticulate R package. 我正在尝试通过reticulate R包将python模块limmbohttps://github.com/HannahVMeyer/limmbo )与R一起使用。 I have successfully installed limmbo with Anaconda2. 我已经用Anaconda2成功安装了limmbo I'm now trying to use the function limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation , as in my code below. 我现在正在尝试使用函数limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation ,如下面的代码所示。 When I run the code below, I get an error about converting a float64 to an integer64. 当我运行下面的代码时,出现有关将float64转换为integer64的错误。

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

I then run the python code: 然后,我运行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)
```

The problem arises when I try to run the R function limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation : 当我尝试运行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

First of all, import you numpy module via np <- import("numpy", convert = FALSE) . 首先,通过np <- import("numpy", convert = FALSE)导入numpy模块。

And then you can re-create your numpy array with explicit type int64 by using reticulate::np_array(datainput, dtype = np$int64) . 然后,您可以使用reticulate::np_array(datainput, dtype = np$int64)重新创建具有显式类型int64的numpy数组。

You can learn more about how to manipulate and create your arrays in this tutorial . 您可以在本教程中了解有关如何操作和创建数组的更多信息。

Hope this helps. 希望这可以帮助。

Yuan's tutorial (link in the answer above) contains suggestions that allowed me to answer the question. Yuan的教程(上面答案中的链接)包含一些建议,这些建议使我可以回答问题。 Here is my revised R code that, as of now, works: 这是我目前可以修改的R代码:

np <- import("numpy", convert = FALSE)
(limmbo$core$vdbootstrap$LiMMBo(datainput, timing = TRUE, iterations = np_array(10, dtype = "int64"), S = np_array(2, dtype = "int64")) -> foo)
limmbo$core$vdbootstrap$LiMMBo$runBootstrapCovarianceEstimation(foo, cpus = np$int(1), seed = np_array(1232, dtype = "int64"))

暂无
暂无

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

相关问题 在熊猫中将int64非自愿转换为float64 - Involuntary conversion of int64 to float64 in pandas 随机int64和float64数字 - Random int64 and float64 numbers 计算不等于(!=)的float64或int64的频率 - count the frequency of float64 or int64 with not equal(!=) 使用“安全”将float64转换为int64 - Convert float64 to int64 using “safe” 根据规则“安全”,无法将数组数据从dtype(&#39;float64&#39;)转换为dtype(&#39;int64&#39;)! int和float之间的astype函数 - Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe' ! astype function between int and float 在 Pandas 中将 float64 列转换为 int64 - Convert float64 column to int64 in Pandas 2D numpy数组不会从int64隐式转换为float64 - 2D numpy array doesn't implicitly convert from int64 to float64 Python:如何将大数存储在 Pandas dataframe 作为 int64 或 float64? - Python: How to store large numbers in a Pandas dataframe as int64 or float64? 在 np.array 中查找最频繁的值会返回“TypeError:无法根据安全规则将数组数据从 dtype('float64) 转换为 dtype('int64')” - Find most frequent value in an np.array gives back "TypeError: cannot cast array data from dtype('float64) to dtype('int64') according to safe rule" 无法使用转换规则“same_kind”将 ufunc 'add' output 从 dtype('float64') 转换为 dtype('int64') - Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM