Questions on using RandomSearchCV with custom estimator using sklearn?

Viewed 65

I've read through some stackoverflow discussions and articles but I can't seem to understand how the parameters get passed around from fit to predict to score when RandomSearch is used. This is my code so far;

class own_estimator():
    def __init__(self,param1,param2,param3):
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3

    def fit(self,X):
        if (len(X)>0 and self.param1 <0 and self.param2 >0):
            self.param3 = 233
        else:
            self.param3 = 566
        return self

    def set_params(self,param1,param2):
        self.param1 = param1
        self.param2 = param2
        return self

    def get_params(self,deep=True):
        return {'param1':self.param1,'param2':self.param2,'param3':self.param3}

    def predict(self,X):
        precictions = [x*self.param3 for x in X]
        return precictions

    def score(self,X):
        x_pred = self.predict(X)

        if mean(x_pred)>mean(X):
            return x_pred
        else:
            return X

And my RandomSearchingCV Code

my_model = own_estimator(param1=1)

new_params = {
    'param2':[0,1,2,3,4],
    'param3':[9,9,1,2],
}

search = RandomizedSearchCV(my_model,new_params,n_iter=10,n_jobs=-1)
search.fit(dataset)

The dataset I'm working on is in the format of (simply put, I have a data item which but I am working on the individual labels for that item which would be multi-categorical, I also need the ID of the person who provided the label, hence a worker with the ID 123 provided a label as 1 and then worker with ID 234 provided 2).;

dataset[0] = {'item_id':123,'labels':{'123':1,'234':2}}

My questions (the actual implementation works on it's own, but just not with RandomSearching);

  1. Based on my code, am I doing anything that is wrong?
  2. When I pass in the dataset to fit function, does the RandomSearchingCV split this into train,dev, and test?
  3. Given my dataset and that I need the individual labels (for fit and predict), any recommendations on how I could do it but also make it work with the RandomSearchingCV?

Thank you

1 Answers

The problem I had was the set_params and get_params both need to match in terms of the parameters that are shared. Once that is resolved, got through the problems.

Related