[英]Python Error - NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'ipywidgets.widgets.widget_string.Text'>'
[英]NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>' only after I run the code twice
如果我运行下面的代码一旦它工作。
import numpy as np
import rpy2.robjects as robjects
x = np.linspace(0, 1, num = 11, endpoint=True)
y = np.array([-1,1,1, -1,1,0, .5,.5,.4, .5, -1])
r_x = robjects.FloatVector(x)
r_y = robjects.FloatVector(y)
r_smooth_spline = robjects.r['smooth.spline'] #extract R function
spline_xy = r_smooth_spline(x=r_x, y=r_y)
print('x =', x)
print('ysplined =',np.array(robjects.r['predict'](spline_xy,robjects.FloatVector(x)).rx2('y')))
如果我在 Jupyter notebook 中运行此单元格两次,我会收到以下错误消息:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-2-5efeb940cd16> in <module>
6 r_x = robjects.FloatVector(x)
7 r_y = robjects.FloatVector(y)
----> 8 r_smooth_spline = robjects.r['smooth.spline'] #extract R function
9 spline_xy = r_smooth_spline(x=r_x, y=r_y)
10 print('x =', x)
2 frames
/usr/local/lib/python3.8/dist-packages/rpy2/robjects/conversion.py in _rpy2py(obj)
250 non-rpy2) objects.
251 """
--> 252 raise NotImplementedError(
253 "Conversion 'rpy2py' not defined for objects of type '%s'" %
254 str(type(obj))
NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>'
这段代码过去总是多次运行而没有问题。 可能是新版本的 python 或 rpy2 是问题所在? 我该如何解决这个问题,以便我能够在一个 Jupyter notebook 中多次运行此代码。
此错误发生在以下版本: ipykernel
版本 5.3.4,
!jupyter --version
Selected Jupyter core packages...
IPython : 7.9.0
ipykernel : 5.3.4
ipywidgets : 7.7.1
jupyter_client : 6.1.12
jupyter_core : 4.11.2
jupyter_server : not installed
jupyterlab : not installed
nbclient : not installed
nbconvert : 5.6.1
nbformat : 5.7.0
notebook : 5.7.16
qtconsole : not installed
traitlets : 5.1.1
结合rpy2
版本 3.5.5
import rpy2
print(rpy2.__version__)
3.5.5
这是由旧版本的ipykernel
中的问题引起的。 我建议升级它而不是降级rpy2
。
最简单的解决方法是运行一次:
!pip install -Iv rpy2==3.4.2
在 Jupyter-notebook 开始时为了回滚到版本3.4.2
,这个问题没有发生(参见Rpy2 Error depends on execution method: NotImplementedError: Conversion "rpy2py" not defined )。 有关如何更改 python 包版本的更多信息,请参阅在 Goolge Colab 中回滚到特定版本的 python 包和使用 pip 安装特定包版本)
了解如何正确使用最新版本的rpy2
仍然很有趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.