def test_categorical(k: int, learner, breast_cancer_data: Tuple4Array): X_train, X_test, y_train, _ = breast_cancer_data dist = k_categorical(k) y_train = np.random.randint(0, k, (len(y_train))) # test early stopping features ngb = NGBClassifier(Dist=dist, Score=LogScore, Base=learner, verbose=False) ngb.fit(X_train, y_train) y_pred = ngb.predict(X_test) y_prob = ngb.predict_proba(X_test) y_dist = ngb.pred_dist(X_test)
def test_categorical(self, learners, cls_data): X_cls_train, X_cls_test, Y_cls_train, Y_cls_test = cls_data for K in [2,4,7]: Dist = k_categorical(K) Y_cls_train = np.random.randint(0,K,(len(Y_cls_train))) for Learner in learners: # test early stopping features ngb = NGBClassifier(Dist=Dist, Score=LogScore, Base=Learner, verbose=False) ngb.fit(X_cls_train, Y_cls_train) y_pred = ngb.predict(X_cls_test) y_prob = ngb.predict_proba(X_cls_test) y_dist = ngb.pred_dist(X_cls_test)
def __setstate__(self, state_dict): if "K" in state_dict.keys(): state_dict["Dist"] = k_categorical(state_dict["K"]) state_dict["Manifold"] = manifold(state_dict["Score"], state_dict["Dist"]) self.__dict__ = state_dict
from ngboost.distns import k_categorical from ngboost import NGBClassifier from sklearn.datasets import load_breast_cancer from sklearn.model_selection import GridSearchCV, train_test_split from sklearn.tree import DecisionTreeRegressor from sklearn.linear_model import Ridge if __name__ == "__main__": # An example where the base learner is also searched over (this is how you would vary tree depth): X, Y = load_breast_cancer(True) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2) b1 = DecisionTreeRegressor(criterion='friedman_mse', max_depth=2) b2 = DecisionTreeRegressor(criterion='friedman_mse', max_depth=4) b3 = Ridge(alpha=0.0) param_grid = { 'n_estimators': [20, 50], 'minibatch_frac': [1.0, 0.5], 'Base': [b1, b2] } ngb = NGBClassifier(natural_gradient=True, verbose=False, Dist=k_categorical(2)) grid_search = GridSearchCV(ngb, param_grid=param_grid, cv=5) grid_search.fit(X_train, Y_train) print(grid_search.best_params_)
from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from ngboost import NGBClassifier from ngboost.distns import k_categorical if __name__ == "__main__": X, y = load_breast_cancer(True) y[0: 15] = 2 # artificially make this a 3-class problem instead of a 2-class problem X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.2) ngb = NGBClassifier(Dist=k_categorical( 3)) # tell ngboost that there are 3 possible outcomes ngb.fit(X_train, Y_train) # Y should have only 3 values: {0,1,2} # predicted probabilities of class 0, 1, and 2 (columns) for each observation (row) preds = ngb.predict_proba(X_test)
def ng_model(self): ngb_cat = NGBClassifier(Dist=k_categorical(2), verbose=True) ng_clf = ngb_cat.fit(self.X_t, self.y_t) print(ng_clf.feature_importances_) return ng_clf
from sklearn.datasets import load_breast_cancer from sklearn.linear_model import Ridge from sklearn.model_selection import GridSearchCV, train_test_split from sklearn.tree import DecisionTreeRegressor from ngboost import NGBClassifier from ngboost.distns import k_categorical if __name__ == "__main__": # An example where the base learner is also searched over (this is how you would vary tree depth): X, Y = load_breast_cancer(True) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2) b1 = DecisionTreeRegressor(criterion="friedman_mse", max_depth=2) b2 = DecisionTreeRegressor(criterion="friedman_mse", max_depth=4) b3 = Ridge(alpha=0.0) param_grid = { "n_estimators": [20, 50], "minibatch_frac": [1.0, 0.5], "Base": [b1, b2], } ngb = NGBClassifier(natural_gradient=True, verbose=False, Dist=k_categorical(2)) grid_search = GridSearchCV(ngb, param_grid=param_grid, cv=5) grid_search.fit(X_train, Y_train) print(grid_search.best_params_)
from ngboost import NGBClassifier from ngboost.distns import k_categorical from ngboost.learners import default_tree_learner from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split if __name__ == "__main__": X, y = load_breast_cancer(True) y[0:15] = 2 # artificially make this a 3-class problem instead of a 2-class problem X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.2) ngb = NGBClassifier( Dist=k_categorical(3) ) # tell ngboost that there are 3 possible outcomes ngb.fit(X_train, Y_train) # Y should have only 3 values: {0,1,2} # predicted probabilities of class 0, 1, and 2 (columns) for each observation (row) preds = ngb.predict_proba(X_test)