Skip to content

Commit

Permalink
make binning inplace optional
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixWick committed Dec 9, 2023
1 parent 81c8dcf commit c969ff7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 69 deletions.
11 changes: 9 additions & 2 deletions cyclic_boosting/binning/bin_number_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ def __init__(
weight_column=None,
epsilon=1e-9,
tolerance=0.1,
inplace=False,
):
self.n_bins = n_bins
self.feature_properties = feature_properties
self.weight_column = weight_column
self.epsilon = epsilon
self.tolerance = tolerance
self.nan_representation = MISSING_VALUE_AS_BINNO
self.inplace = inplace
ECdfTransformer.__init__(
self,
n_bins=self.n_bins,
Expand Down Expand Up @@ -172,8 +174,13 @@ def is_finite(x):
xt = MISSING_VALUE_AS_BINNO
return xt

def transform(self, X, y=None):
self._check_input_for_transform(X)
def transform(self, X_orig, y=None):
self._check_input_for_transform(X_orig)

if not self.inplace:
X = X_orig.copy()
else:
X = X_orig

if check_frame_empty(X):
if isinstance(X, pd.DataFrame):
Expand Down
3 changes: 2 additions & 1 deletion cyclic_boosting/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def pipeline_CB(
regalpha=0.0,
quantile=None,
costs=None,
inplace=False,
):
if estimator in [CBPoissonRegressor, CBLocPoissonRegressor, CBLocationRegressor, CBClassifier]:
estimatorCB = estimator(
Expand Down Expand Up @@ -171,7 +172,7 @@ def pipeline_CB(
)
else:
raise Exception("No valid CB estimator.")
binner = binning.BinNumberTransformer(n_bins=number_of_bins, feature_properties=feature_properties)
binner = binning.BinNumberTransformer(n_bins=number_of_bins, feature_properties=feature_properties, inplace=inplace)

return Pipeline([("binning", binner), ("CB", estimatorCB)])

Expand Down
12 changes: 6 additions & 6 deletions docs/source/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ from cyclic_boosting.pipelines import pipeline_CBMultiplicativeQuantileRegressor
from cyclic_boosting.quantile_matching import J_QPD_S

CB_est_qlow = pipeline_CBMultiplicativeQuantileRegressor(quantile=0.2)
CB_est_qlow.fit(X_train.copy(), y)
yhat_qlow = CB_est_qlow.predict(X_test.copy())
CB_est_qlow.fit(X_train, y)
yhat_qlow = CB_est_qlow.predict(X_test)

CB_est_qmedian = pipeline_CBMultiplicativeQuantileRegressor(quantile=0.5)
CB_est_qmedian.fit(X_train.copy(), y)
yhat_qmedian = CB_est_qmedian.predict(X_test.copy())
CB_est_qmedian.fit(X_train, y)
yhat_qmedian = CB_est_qmedian.predict(X_test)

CB_est_qhigh = pipeline_CBMultiplicativeQuantileRegressor(quantile=0.8)
CB_est_qhigh.fit(X_train.copy(), y)
yhat_qhigh = CB_est_qhigh.predict(X_test.copy())
CB_est_qhigh.fit(X_train, y)
yhat_qhigh = CB_est_qhigh.predict(X_test)

j_qpd_s_42 = J_QPD_S(0.2, yhat_qlow[42], yhat_qmedian[42], yhat_qhigh[42])
yhat_42_percentile95 = j_qpd_s_42.ppf(0.95)
Expand Down
Loading

0 comments on commit c969ff7

Please sign in to comment.