Пример #1
0
        def fit(self, X, y):
            from sklearn.ensemble import RandomForestRegressor
            from OptimizedOffsetRegressor import OptimizedOffsetRegressor

            self.rfr = RandomForestRegressor(
                           max_depth=self.max_depth,
                           n_estimators=self.n_estimators,
                           n_jobs=self.n_jobs,
                           random_state=self.random_state,
                           max_features=self.max_features,
                           min_samples_leaf=self.min_samples_leaf,
                           verbose=self.verbose)
            self.off = OptimizedOffsetRegressor(n_buckets=self.n_buckets,
                           initial_offsets=self.initial_offsets,
                           scoring=self.scoring)

            self.rfr.fit(X, y)
            tr_y_hat = self.clip(self.rfr.predict(X))
            print('Train score is:', -self.scoring(tr_y_hat, y))
            self.off.fit(tr_y_hat, y)
            print("Offsets:", self.off.offsets_)
            return self
Пример #2
0
    class PrudentialRegressor(BaseEstimator, RegressorMixin):
        def __init__(self,
                    max_depth=7,
                    n_estimators=50,
                    n_jobs=-1,
                    random_state=1,
                    max_features=1.0,
                    min_samples_leaf=1.0,
                    verbose=1,
                    n_buckets=8,
                    initial_offsets=[-1.5, -2.6, -3.6, -1.2, -0.8, 0.04, 0.7, 3.6],
                    scoring=NegQWKappaScorer):

            self.max_depth = max_depth
            self.n_estimators = n_estimators
            self.n_jobs = n_jobs
            self.random_state = random_state
            self.max_features = max_features
            self.min_samples_leaf = min_samples_leaf
            self.verbose=verbose

            self.n_buckets = n_buckets
            self.initial_offsets = initial_offsets
            self.scoring = scoring

            return


        def epsilon(self, Tp):
            from numpy import finfo
            return finfo(Tp).eps


        def clip(self, arr):
            from numpy import clip
            return clip(arr, 0., self.n_buckets * (1. - self.epsilon(arr.dtype)))


        def fit(self, X, y):
            from sklearn.ensemble import RandomForestRegressor
            from OptimizedOffsetRegressor import OptimizedOffsetRegressor

            self.rfr = RandomForestRegressor(
                           max_depth=self.max_depth,
                           n_estimators=self.n_estimators,
                           n_jobs=self.n_jobs,
                           random_state=self.random_state,
                           max_features=self.max_features,
                           min_samples_leaf=self.min_samples_leaf,
                           verbose=self.verbose)
            self.off = OptimizedOffsetRegressor(n_buckets=self.n_buckets,
                           initial_offsets=self.initial_offsets,
                           scoring=self.scoring)

            self.rfr.fit(X, y)
            tr_y_hat = self.clip(self.rfr.predict(X))
            print('Train score is:', -self.scoring(tr_y_hat, y))
            self.off.fit(tr_y_hat, y)
            print("Offsets:", self.off.offsets_)
            return self


        def predict(self, X):
            te_y_hat = self.clip(self.rfr.predict(X))
            return self.off.predict(te_y_hat)

        pass