2010-12-14 3 views
0

Ich versuche, rpart über RPY2 mit Python 2.6.5 und R 10.0 auszuführen.Problem mit rpy2, Rpart übergibt Daten korrekt von Python zu r

ich einen Datenrahmen in Python erstellen und entlang passieren, aber ich erhalte eine Fehlermeldung besagt:

Error in function (x) : binary operation on non-conformable arrays 
Traceback (most recent call last): 
    File "partitioningSANDBOX.py", line 86, in <module> 
    model=r.rpart(**rpart_params) 
    File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 83, in __call__ 
    File "build/bdist.macosx-10.3-fat/egg/rpy2/robjects/functions.py", line 35, in __call__ 
rpy2.rinterface.RRuntimeError: Error in function (x) : binary operation on non-conformable arrays 

Kann mir jemand helfen, festzustellen, was ich falsch mache, diesen Fehler zu werfen?

der relevante Teil meines Codes ist dies:

import numpy as np 
import rpy2 
import rpy2.robjects as rob 
import rpy2.robjects.numpy2ri 


#Fire up the interface to R 
r = rob.r 
r.library("rpart") 

datadict = dict(zip(['responsev','predictorv'],[cLogEC,csplitData])) 
Rdata = r['data.frame'](**datadict) 
Rformula = r['as.formula']('responsev ~.') 
#Generate an RPART model in R. 
Rpcontrol = r['rpart.control'](minsplit=10, xval=10) 
rpart_params = {'formula' : Rformula, \ 
     'data' : Rdata, 
     'control' : Rpcontrol} 
model=r.rpart(**rpart_params) 

Die beiden Variablen cLogEC und csplitData sind numpy Arrays von Typ float.

Auch sieht meine Datenrahmen wie folgt aus:

In [2]: print Rdata 
------> print(Rdata) 
    responsev predictorv 
1 0.6020600  312 
2 0.3010300  300 
3 0.4771213  303 
4 0.4771213  249 
5 0.9242793  239 
6 1.1986571  297 
7 0.7075702  287 
8 1.8115750  270 
9 0.6020600  296 
10 1.3856063  248 
11 0.6127839  295 
12 0.3010300  283 
13 1.1931246  345 
14 0.3010300  270 
15 0.3010300  251 
16 0.3010300  246 
17 0.3010300  273 
18 0.7075702  252 
19 0.4771213  252 
20 0.9294189  223 
21 0.6127839  252 
22 0.7075702  267 
23 0.9294189  252 
24 0.3010300  378 
25 0.3010300  282 

und die Formel sieht wie folgt aus:

In [3]: print Rformula 
------> print(Rformula) 
responsev ~ . 
+0

Datenrahmen in R sind Listen. Vielleicht sollten Sie Arrays an Arrays oder Matrizen übergeben? –

+0

Ich habe versucht, Matrizen zu übergeben, aber das warf auch einen Fehler. Interessanterweise, wenn ich r.plsr für r.rpart ersetze, funktioniert es gut und sowohl rpart als auch plsr sagen, sie wollen Daten als data.frame .... – mishaF

Antwort

5

Das Problem R idiosynkratische Code in rpart verwandt ist (um genau zu sein, die folgender Block, insbesondere die letzte Zeile:

m <- match.call(expand.dots = FALSE) 
m$model <- m$method <- m$control <- NULL 
m$x <- m$y <- m$parms <- m$... <- NULL 
m$cost <- NULL 
m$na.action <- na.action 
m[[1L]] <- as.name("model.frame") 
m <- eval(m, parent.frame()) 

).

Eine Möglichkeit, das zu umgehen, besteht darin, das Eingeben dieses Codeblocks zu vermeiden (siehe unten) oder eine verschachtelte Auswertung von Python zu konstruieren (so dass parent.frame() sich verhält). Das ist nicht so einfach, wie man hoffen würde, aber vielleicht werde ich Zeit finden, um es in Zukunft einfacher zu machen.

from rpy2.robjects import DataFrame, Formula 
import rpy2.robjects.numpy2ri as npr 
import numpy as np 
from rpy2.robjects.packages import importr 
rpart = importr('rpart') 
stats = importr('stats') 

cLogEC = np.random.uniform(size=10) 
csplitData = np.array(range(10), 'i') 

dataf = DataFrame({'responsev': cLogEC, 
        'predictorv': csplitData}) 
formula = Formula('responsev ~.') 
rpart.rpart(formula=formula, data=dataf, 
      control=rpart.rpart_control(minsplit = 10, xval = 10), 
      model = stats.model_frame(formula, data=dataf)) 
+0

Ihre Antwort war perfekt und die Lösung hat perfekt funktioniert. Vielen, vielen Dank! Ich zog mir die Haare aus dem Gesicht. – mishaF

Verwandte Themen