2021年3月4日星期四

Can't access kmeans withinss value with Rpy2

I'm trying to update some old Python code from Rpy1 to Rpy2, but there's a couple lines that I can't get to run without error:

result = rpy.r.kmeans(datnorm, cls, kmeans_iterations, kmeans_nstart)  sse = sum(result['withinss'])  

I updated the Rpy dependency to Rpy2 and refactored this code to use the Rpy2 importr to load the R stats module and kmeans function. This is what the code looks like after the change:

result = rstats.kmeans(matnorm, cls, kmeans_iterations, kmeans_nstart)  sse = sum(result['withinss'])  

When this code runs, I get this error:

TypeError: Indices must be integers or slices, not <class 'str'>  

I understand this is saying I can't use a string as an index because kmeans is returning an integer vector. How do I get the withinss value if the result isn't a dict?

https://stackoverflow.com/questions/66484095/cant-access-kmeans-withinss-value-with-rpy2 March 05, 2021 at 06:38AM

没有评论:

发表评论