Custom python model with library editor

Tags
Registered Posts: 1 ✭✭✭
edited July 2024 in Using Dataiku

Hey,

I’m trying to define a custom Python model that should be fitted adjusting for the variable "exposure". The code runs without an error in the notebook, but this error message occurs when the model is fitted in the visual analyses.

"Failed to train : <class 'IndexError'> : only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices. "

The code in the library editor looks like this:

class Poisson_gbrt(BaseEstimator): 
    
    def __init__(self):
        self.model = Pipeline([
        ("regressor",
          HistGradientBoostingRegressor(loss="poisson",max_leaf_nodes=12,random_state=42),),])
    
    def fit(self,X,y,regressor__sample_weight=None):
        self.model.fit(X,y,regressor__sample_weight=X["Exposure"]) 
        return None 
    
    
    def predict(self, X):
        return self.model.predict(X)

          

Thanks,

Odin

Answers

  • Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,270 Dataiker
    edited July 2024

    Hi @Odin
    ,

    The error suggests you are passing X["Exposure"] which is not what HistGradientBoostingRegressor you could try to convert this to numpy array instead :

    exposure_weights = X["Exposure"].to_numpy()

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.