I'm getting the error below from some Python code. Can anyone help me figure out what is going on? I tried to embed the full code, but I kept getting an error saying my code was not properly formatted. I'll try to post a reply with the full code!
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'ipywidgets.widgets.widget_string.Text'>'
**Code:**
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
Input In [122], in <cell line: 1>()
----> 1 get_ipython().run_cell_magic('R', '-i cutoff -i mtry -i ntree -i data -o max_f1 -o max_cut -o max_mtry -o max_ntree -o max_precision -o max_recall', 'library(randomForest)\n\nfrom types import NoneType\nNoneType = type(None)\n\n#from IPython.core.interactiveshell import InteractiveShell\n#InteractiveShell.ast_node_interactivity = "all"\n\n#9-24-2022 SL\n#from rpy2.robjects.conversion import localconverter as lc\n\n#with lc(ro.default_converter + pr.converter):\n# fileName_c = ro.conversion.py2rpy(fileName)\n# url_c = ro.conversion.py2rpy(url)\n#ro.globalenv[\'fileName\'] = fileName_c\n#ro.globalenv[\'url\'] = url_c\n\n#from rpy2.robjects import pandas2ri\n\n\n#pandas2ri.activate()\n#end 9-24-2022 SL\n\ndata$target = factor(data$target)\ncutoff_list = unlist(strsplit("0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9",",")) #cutoff 9-24-2022 SL\nmtry_list = unlist(strsplit("2,3,4,5",",")) #mtry 9-24-2022 SL\nntree_list = unlist(strsplit("100,500,1000",",")) #ntree 9-24-2022 SL\nset.seed(830)\nmax_f1 = 0\nmax_cut = 0\nmax_mtry = 0\nmax_ntree = 0\nmax_precision = 0\nmax_recall = 0\n# a nested for loop for ntree, mtry, cutoff?\nfor (cut in cutoff_list )\n{\n for (Mtry in mtry_list)\n {\n for (Ntree in ntree_list)\n {\n cut = as.numeric(cut) #9-24-2022 SL cut - should be "cutoff"?\n Mtry = as.numeric(mtry) #9-24-2022 SL mtry\n Ntree = as.numeric(NTree) #9-24-2022 SL Ntree\n fit_data <- randomForest(data$target ~ ., data=data, importance=TRUE, proximity=TRUE, cutoff=c(cut, 1-cut), ntree=Ntree, mtry=Mtry)\n true_positives = fit_data$confusion[\'1\',\'1\']\n true_negatives = fit_data$confusion[\'0\',\'0\']\n false_positives = fit_data$confusion[\'0\',\'1\']\n false_negatives = fit_data$confusion[\'1\',\'0\']\n accuracy = (true_positives + true_negatives)/nrow(data)\n precision = true_positives/(true_positives+false_positives)\n recall = true_positives/(true_positives + false_negatives)\n f1_score = 2*((recall*precision)/(precision+recall))\n if (! is.nan(f1_score) && f1_score > max_f1)\n {\n max_f1 = f1_score\n max_cut = cut\n max_mtry = Mtry\n max_ntree = Ntree\n max_precision = precision\n max_recall = recall\n }\n }\n }\n}\n\n')
File ~\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2347, in InteractiveShell.run_cell_magic(self, magic_name, line, cell)
2345 with self.builtin_trap:
2346 args = (magic_arg_s, cell)
-> 2347 result = fn(*args, **kwargs)
2348 return result
File ~\anaconda3\lib\site-packages\rpy2\ipython\rmagic.py:755, in RMagics.R(self, line, cell, local_ns)
753 raise NameError("name '%s' is not defined" % input)
754 with localconverter(converter) as cv:
--> 755 ro.r.assign(input, val)
757 if args.display:
758 try:
File ~\anaconda3\lib\site-packages\rpy2\robjects\functions.py:203, in SignatureTranslatedFunction.__call__(self, *args, **kwargs)
201 v = kwargs.pop(k)
202 kwargs[r_k] = v
--> 203 return (super(SignatureTranslatedFunction, self)
204 .__call__(*args, **kwargs))
File ~\anaconda3\lib\site-packages\rpy2\robjects\functions.py:118, in Function.__call__(self, *args, **kwargs)
116 def __call__(self, *args, **kwargs):
117 cv = conversion.get_conversion()
--> 118 new_args = [cv.py2rpy(a) for a in args]
119 new_kwargs = {}
120 for k, v in kwargs.items():
121 # TODO: shouldn't this be handled by the conversion itself ?
File ~\anaconda3\lib\site-packages\rpy2\robjects\functions.py:118, in <listcomp>(.0)
116 def __call__(self, *args, **kwargs):
117 cv = conversion.get_conversion()
--> 118 new_args = [cv.py2rpy(a) for a in args]
119 new_kwargs = {}
120 for k, v in kwargs.items():
121 # TODO: shouldn't this be handled by the conversion itself ?
File ~\anaconda3\lib\functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
884 if not args:
885 raise TypeError(f'{funcname} requires at least '
886 '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)
File ~\anaconda3\lib\site-packages\rpy2\robjects\numpy2ri.py:134, in nonnumpy2rpy(obj)
129 return ro.default_converter.py2rpy(obj)
130 elif original_converter is None:
131 # This means that the conversion module was not "activated".
132 # For now, go with the default_converter.
133 # TODO: the conversion system needs an overhaul badly.
--> 134 return ro.default_converter.py2rpy(obj)
135 else:
136 # The conversion module was "activated"
137 return original_converter.py2rpy(obj)
File ~\anaconda3\lib\functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
884 if not args:
885 raise TypeError(f'{funcname} requires at least '
886 '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)
File ~\anaconda3\lib\site-packages\rpy2\robjects\conversion.py:240, in _py2rpy(obj)
238 if isinstance(obj, _rinterface_capi.SupportsSEXP):
239 return obj
--> 240 raise NotImplementedError(
241 "Conversion 'py2rpy' not defined for objects of type '%s'" %
242 str(type(obj))
243 )
NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'ipywidgets.widgets.widget_string.Text'>'
Turns out I there were some input parameters that I needed to hit for prior steps to store them properly. Once I did that, the above error got resolved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.