def test_quantile_regression(self, boston_X, boston_y): X_train, X_test, y_train, y_test = train_test_split(boston_X, boston_y) forest = RangerForestRegressor(quantiles=False) forest.fit(X_train, y_train) assert not hasattr(forest, "random_node_values_") with pytest.raises(ValueError): forest.predict_quantiles(X_test, quantiles=[0.2, 0.5, 0.8]) forest = RangerForestRegressor(quantiles=True) forest.fit(X_train, y_train) assert hasattr(forest, "random_node_values_") quantiles_lower = forest.predict_quantiles(X_test, quantiles=[0.1]) quantiles_upper = forest.predict_quantiles(X_test, quantiles=[0.9]) assert np.less(quantiles_lower, quantiles_upper).all() assert quantiles_upper.ndim == 1 quantiles = forest.predict_quantiles(X_test, quantiles=[0.1, 0.9]) assert quantiles.shape == (X_test.shape[0], 2) assert np.sum(np.isnan(quantiles_lower)) == 0 assert np.sum(np.isnan(quantiles_upper)) == 0 # test predict method pred = forest.predict(X_test, quantiles=[0.2, 0.5]) assert pred.shape == (X_test.shape[0], 2) assert np.sum(np.isnan(pred)) == 0 pred = forest.predict(X_test, quantiles=[0.2]) assert pred.ndim == 1 assert np.sum(np.isnan(pred)) == 0 # test with single record boston_X_record = boston_X[0:1, :] pred = forest.predict(boston_X_record, quantiles=[0.2, 0.5]) assert pred.shape == (1, 2) assert np.sum(np.isnan(pred)) == 0
def test_predict(self, boston_X, boston_y): forest = RangerForestRegressor() forest.fit(boston_X, boston_y) pred = forest.predict(boston_X) assert len(pred) == boston_X.shape[0] # test with single record boston_X_record = boston_X[0:1, :] pred = forest.predict(boston_X_record) assert len(pred) == 1
def test_sample_fraction(self, boston_X, boston_y): forest = RangerForestRegressor(sample_fraction=0.69) forest.fit(boston_X, boston_y) assert forest.sample_fraction_ == [0.69] # test with single record boston_X_record = boston_X[0:1, :] pred = forest.predict(boston_X_record) assert len(pred) == 1
def test_categorical_features( self, boston_X, boston_y, respect_categorical_features ): # add a categorical feature categorical_col = np.atleast_2d( np.array([random.choice([0, 1]) for _ in range(boston_X.shape[0])]) ) boston_X_c = np.hstack((boston_X, categorical_col.transpose())) categorical_features = [boston_X.shape[1]] forest = RangerForestRegressor( respect_categorical_features=respect_categorical_features, categorical_features=categorical_features, ) if respect_categorical_features not in ["partition", "ignore", "order"]: with pytest.raises(ValueError): forest.fit(boston_X_c, boston_y) return forest.fit(boston_X_c, boston_y) forest.predict(boston_X_c)