Skip to content

Commit a720be6

Browse files
boosting_steps_before_interactions_are_allowed
1 parent 08d43b7 commit a720be6

File tree

8 files changed

+108
-65
lines changed

8 files changed

+108
-65
lines changed

API_REFERENCE_FOR_CLASSIFICATION.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# APLRClassifier
22

3-
## class aplr.APLRClassifier(m:int=9000, v:float=0.1, random_state:int=0, n_jobs:int=0, validation_ratio:float=0.2, bins:int=300, verbosity:int=0, max_interaction_level:int=1, max_interactions:int=100000, min_observations_in_split:int=20, ineligible_boosting_steps_added:int=10, max_eligible_terms:int=5, boosting_steps_before_pruning_is_done:int = 0)
3+
## class aplr.APLRClassifier(m:int=9000, v:float=0.1, random_state:int=0, n_jobs:int=0, validation_ratio:float=0.2, bins:int=300, verbosity:int=0, max_interaction_level:int=1, max_interactions:int=100000, min_observations_in_split:int=20, ineligible_boosting_steps_added:int=10, max_eligible_terms:int=5, boosting_steps_before_pruning_is_done:int = 0, boosting_steps_before_interactions_are_allowed: int = 0)
44

55
### Constructor parameters
66

@@ -43,6 +43,9 @@ Limits 1) the number of terms already in the model that can be considered as int
4343
#### boosting_steps_before_pruning_is_done (default = 0)
4444
Specifies how many boosting steps to wait before pruning the model. If 0 (default) then pruning is not done. If for example 500 then the model will be pruned in boosting steps 500, 1000, and so on. When pruning, terms are removed as long as this reduces the training error. This can be a computationally costly operation especially if the model gets many terms. Pruning may improve predictiveness.
4545

46+
#### boosting_steps_before_interactions_are_allowed (default = 0)
47+
Specifies how many boosting steps to wait before searching for interactions. If for example 800, then the algorithm will be forced to only fit main effects in the first 800 boosting steps, after which it is allowed to search for interactions (given that other hyperparameters that control interactions also allow this). The motivation for fitting main effects first may be 1) to get a cleaner looking model that puts more emphasis on main effects and 2) to speed up the algorithm since looking for interactions is computationally more demanding.
48+
4649

4750
## Method: fit(X:npt.ArrayLike, y:List[str], sample_weight:npt.ArrayLike = np.empty(0), X_names:List[str]=[], validation_set_indexes:List[int]=[], prioritized_predictors_indexes:List[int]=[], monotonic_constraints:List[int]=[], interaction_constraints:List[List[int]]=[])
4851

API_REFERENCE_FOR_REGRESSION.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# APLRRegressor
22

3-
## class aplr.APLRRegressor(m:int=1000, v:float=0.1, random_state:int=0, loss_function:str="mse", link_function:str="identity", n_jobs:int=0, validation_ratio:float=0.2, bins:int=300, max_interaction_level:int=1, max_interactions:int=100000, min_observations_in_split:int=20, ineligible_boosting_steps_added:int=10, max_eligible_terms:int=5, verbosity:int=0, dispersion_parameter:float=1.5, validation_tuning_metric:str="default", quantile:float=0.5, calculate_custom_validation_error_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], float]]=None, calculate_custom_loss_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], float]]=None, calculate_custom_negative_gradient_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], npt.ArrayLike]]=None, calculate_custom_transform_linear_predictor_to_predictions_function:Optional[Callable[[npt.ArrayLike], npt.ArrayLike]]=None, calculate_custom_differentiate_predictions_wrt_linear_predictor_function:Optional[Callable[[npt.ArrayLike], npt.ArrayLike]]=None, boosting_steps_before_pruning_is_done: int = 0)
3+
## class aplr.APLRRegressor(m:int=1000, v:float=0.1, random_state:int=0, loss_function:str="mse", link_function:str="identity", n_jobs:int=0, validation_ratio:float=0.2, bins:int=300, max_interaction_level:int=1, max_interactions:int=100000, min_observations_in_split:int=20, ineligible_boosting_steps_added:int=10, max_eligible_terms:int=5, verbosity:int=0, dispersion_parameter:float=1.5, validation_tuning_metric:str="default", quantile:float=0.5, calculate_custom_validation_error_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], float]]=None, calculate_custom_loss_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], float]]=None, calculate_custom_negative_gradient_function:Optional[Callable[[npt.ArrayLike, npt.ArrayLike, npt.ArrayLike, npt.ArrayLike], npt.ArrayLike]]=None, calculate_custom_transform_linear_predictor_to_predictions_function:Optional[Callable[[npt.ArrayLike], npt.ArrayLike]]=None, calculate_custom_differentiate_predictions_wrt_linear_predictor_function:Optional[Callable[[npt.ArrayLike], npt.ArrayLike]]=None, boosting_steps_before_pruning_is_done: int = 0, boosting_steps_before_interactions_are_allowed: int = 0)
44

55
### Constructor parameters
66

@@ -105,6 +105,9 @@ def calculate_custom_differentiate_predictions_wrt_linear_predictor(linear_predi
105105
#### boosting_steps_before_pruning_is_done (default = 0)
106106
Specifies how many boosting steps to wait before pruning the model. If 0 (default) then pruning is not done. If for example 500 then the model will be pruned in boosting steps 500, 1000, and so on. When pruning, terms are removed as long as this reduces the training error. This can be a computationally costly operation especially if the model gets many terms. Pruning may improve predictiveness.
107107

108+
#### boosting_steps_before_interactions_are_allowed (default = 0)
109+
Specifies how many boosting steps to wait before searching for interactions. If for example 800, then the algorithm will be forced to only fit main effects in the first 800 boosting steps, after which it is allowed to search for interactions (given that other hyperparameters that control interactions also allow this). The motivation for fitting main effects first may be 1) to get a cleaner looking model that puts more emphasis on main effects and 2) to speed up the algorithm since looking for interactions is computationally more demanding.
110+
108111
## Method: fit(X:npt.ArrayLike, y:npt.ArrayLike, sample_weight:npt.ArrayLike = np.empty(0), X_names:List[str]=[], validation_set_indexes:List[int]=[], prioritized_predictors_indexes:List[int]=[], monotonic_constraints:List[int]=[], group:npt.ArrayLike = np.empty(0), interaction_constraints:List[List[int]]=[], other_data: npt.ArrayLike = np.empty([0, 0]))
109112

110113
***This method fits the model to data.***

aplr/aplr.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
Callable[[npt.ArrayLike], npt.ArrayLike]
6262
] = None,
6363
boosting_steps_before_pruning_is_done: int = 0,
64+
boosting_steps_before_interactions_are_allowed: int = 0,
6465
):
6566
self.m = m
6667
self.v = v
@@ -95,6 +96,9 @@ def __init__(
9596
self.boosting_steps_before_pruning_is_done = (
9697
boosting_steps_before_pruning_is_done
9798
)
99+
self.boosting_steps_before_interactions_are_allowed = (
100+
boosting_steps_before_interactions_are_allowed
101+
)
98102

99103
# Creating aplr_cpp and setting parameters
100104
self.APLRRegressor = aplr_cpp.APLRRegressor()
@@ -139,6 +143,9 @@ def __set_params_cpp(self):
139143
self.APLRRegressor.boosting_steps_before_pruning_is_done = (
140144
self.boosting_steps_before_pruning_is_done
141145
)
146+
self.APLRRegressor.boosting_steps_before_interactions_are_allowed = (
147+
self.boosting_steps_before_interactions_are_allowed
148+
)
142149

143150
def fit(
144151
self,
@@ -246,6 +253,7 @@ def get_params(self, deep=True):
246253
"calculate_custom_transform_linear_predictor_to_predictions_function": self.calculate_custom_transform_linear_predictor_to_predictions_function,
247254
"calculate_custom_differentiate_predictions_wrt_linear_predictor_function": self.calculate_custom_differentiate_predictions_wrt_linear_predictor_function,
248255
"boosting_steps_before_pruning_is_done": self.boosting_steps_before_pruning_is_done,
256+
"boosting_steps_before_interactions_are_allowed": self.boosting_steps_before_interactions_are_allowed,
249257
}
250258

251259
# For sklearn
@@ -272,6 +280,7 @@ def __init__(
272280
ineligible_boosting_steps_added: int = 10,
273281
max_eligible_terms: int = 5,
274282
boosting_steps_before_pruning_is_done: int = 0,
283+
boosting_steps_before_interactions_are_allowed: int = 0,
275284
):
276285
self.m = m
277286
self.v = v
@@ -288,6 +297,9 @@ def __init__(
288297
self.boosting_steps_before_pruning_is_done = (
289298
boosting_steps_before_pruning_is_done
290299
)
300+
self.boosting_steps_before_interactions_are_allowed = (
301+
boosting_steps_before_interactions_are_allowed
302+
)
291303

292304
# Creating aplr_cpp and setting parameters
293305
self.APLRClassifier = aplr_cpp.APLRClassifier()
@@ -312,6 +324,9 @@ def __set_params_cpp(self):
312324
self.APLRClassifier.boosting_steps_before_pruning_is_done = (
313325
self.boosting_steps_before_pruning_is_done
314326
)
327+
self.APLRClassifier.boosting_steps_before_interactions_are_allowed = (
328+
self.boosting_steps_before_interactions_are_allowed
329+
)
315330

316331
def fit(
317332
self,
@@ -385,6 +400,7 @@ def get_params(self, deep=True):
385400
"ineligible_boosting_steps_added": self.ineligible_boosting_steps_added,
386401
"max_eligible_terms": self.max_eligible_terms,
387402
"boosting_steps_before_pruning_is_done": self.boosting_steps_before_pruning_is_done,
403+
"boosting_steps_before_interactions_are_allowed": self.boosting_steps_before_interactions_are_allowed,
388404
}
389405

390406
# For sklearn

cpp/APLRClassifier.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class APLRClassifier
4343
std::vector<std::string> categories;
4444
std::map<std::string, APLRRegressor> logit_models; // Key is category and value is logit model
4545
size_t boosting_steps_before_pruning_is_done;
46+
size_t boosting_steps_before_interactions_are_allowed;
4647

4748
APLRClassifier(size_t m = 9000, double v = 0.1, uint_fast32_t random_state = std::numeric_limits<uint_fast32_t>::lowest(), size_t n_jobs = 0,
4849
double validation_ratio = 0.2, size_t reserved_terms_times_num_x = 100, size_t bins = 300, size_t verbosity = 0, size_t max_interaction_level = 1,
4950
size_t max_interactions = 100000, size_t min_observations_in_split = 20, size_t ineligible_boosting_steps_added = 10, size_t max_eligible_terms = 5,
50-
size_t boosting_steps_before_pruning_is_done = 0);
51+
size_t boosting_steps_before_pruning_is_done = 0, size_t boosting_steps_before_interactions_are_allowed = 0);
5152
APLRClassifier(const APLRClassifier &other);
5253
~APLRClassifier();
5354
void fit(const MatrixXd &X, const std::vector<std::string> &y, const VectorXd &sample_weight = VectorXd(0),
@@ -68,21 +69,25 @@ class APLRClassifier
6869
APLRClassifier::APLRClassifier(size_t m, double v, uint_fast32_t random_state, size_t n_jobs, double validation_ratio,
6970
size_t reserved_terms_times_num_x, size_t bins, size_t verbosity, size_t max_interaction_level, size_t max_interactions,
7071
size_t min_observations_in_split, size_t ineligible_boosting_steps_added, size_t max_eligible_terms,
71-
size_t boosting_steps_before_pruning_is_done) : m{m}, v{v}, random_state{random_state}, n_jobs{n_jobs}, validation_ratio{validation_ratio},
72-
reserved_terms_times_num_x{reserved_terms_times_num_x}, bins{bins}, verbosity{verbosity}, max_interaction_level{max_interaction_level},
73-
max_interactions{max_interactions}, min_observations_in_split{min_observations_in_split},
74-
ineligible_boosting_steps_added{ineligible_boosting_steps_added}, max_eligible_terms{max_eligible_terms},
75-
boosting_steps_before_pruning_is_done{boosting_steps_before_pruning_is_done}
72+
size_t boosting_steps_before_pruning_is_done, size_t boosting_steps_before_interactions_are_allowed)
73+
: m{m}, v{v}, random_state{random_state}, n_jobs{n_jobs}, validation_ratio{validation_ratio},
74+
reserved_terms_times_num_x{reserved_terms_times_num_x}, bins{bins}, verbosity{verbosity}, max_interaction_level{max_interaction_level},
75+
max_interactions{max_interactions}, min_observations_in_split{min_observations_in_split},
76+
ineligible_boosting_steps_added{ineligible_boosting_steps_added}, max_eligible_terms{max_eligible_terms},
77+
boosting_steps_before_pruning_is_done{boosting_steps_before_pruning_is_done},
78+
boosting_steps_before_interactions_are_allowed{boosting_steps_before_interactions_are_allowed}
7679
{
7780
}
7881

79-
APLRClassifier::APLRClassifier(const APLRClassifier &other) : m{other.m}, v{other.v}, random_state{other.random_state}, n_jobs{other.n_jobs}, validation_ratio{other.validation_ratio},
80-
reserved_terms_times_num_x{other.reserved_terms_times_num_x}, bins{other.bins}, verbosity{other.verbosity},
81-
max_interaction_level{other.max_interaction_level}, max_interactions{other.max_interactions},
82-
min_observations_in_split{other.min_observations_in_split}, ineligible_boosting_steps_added{other.ineligible_boosting_steps_added},
83-
max_eligible_terms{other.max_eligible_terms}, logit_models{other.logit_models}, categories{other.categories},
84-
validation_indexes{other.validation_indexes}, validation_error_steps{other.validation_error_steps}, validation_error{other.validation_error},
85-
feature_importance{other.feature_importance}, boosting_steps_before_pruning_is_done{other.boosting_steps_before_pruning_is_done}
82+
APLRClassifier::APLRClassifier(const APLRClassifier &other)
83+
: m{other.m}, v{other.v}, random_state{other.random_state}, n_jobs{other.n_jobs}, validation_ratio{other.validation_ratio},
84+
reserved_terms_times_num_x{other.reserved_terms_times_num_x}, bins{other.bins}, verbosity{other.verbosity},
85+
max_interaction_level{other.max_interaction_level}, max_interactions{other.max_interactions},
86+
min_observations_in_split{other.min_observations_in_split}, ineligible_boosting_steps_added{other.ineligible_boosting_steps_added},
87+
max_eligible_terms{other.max_eligible_terms}, logit_models{other.logit_models}, categories{other.categories},
88+
validation_indexes{other.validation_indexes}, validation_error_steps{other.validation_error_steps}, validation_error{other.validation_error},
89+
feature_importance{other.feature_importance}, boosting_steps_before_pruning_is_done{other.boosting_steps_before_pruning_is_done},
90+
boosting_steps_before_interactions_are_allowed{other.boosting_steps_before_interactions_are_allowed}
8691
{
8792
}
8893

@@ -106,6 +111,7 @@ void APLRClassifier::fit(const MatrixXd &X, const std::vector<std::string> &y, c
106111
bins, verbosity, max_interaction_level, max_interactions, min_observations_in_split, ineligible_boosting_steps_added,
107112
max_eligible_terms, 1.5, "default", 0.5);
108113
logit_models[categories[0]].boosting_steps_before_pruning_is_done = boosting_steps_before_pruning_is_done;
114+
logit_models[categories[0]].boosting_steps_before_interactions_are_allowed = boosting_steps_before_interactions_are_allowed;
109115
logit_models[categories[0]].fit(X, response_values[categories[0]], sample_weight, X_names, validation_indexes, prioritized_predictors_indexes,
110116
monotonic_constraints, VectorXi(0), interaction_constraints);
111117

@@ -120,6 +126,7 @@ void APLRClassifier::fit(const MatrixXd &X, const std::vector<std::string> &y, c
120126
bins, verbosity, max_interaction_level, max_interactions, min_observations_in_split, ineligible_boosting_steps_added,
121127
max_eligible_terms, 1.5, "default", 0.5);
122128
logit_models[category].boosting_steps_before_pruning_is_done = boosting_steps_before_pruning_is_done;
129+
logit_models[category].boosting_steps_before_interactions_are_allowed = boosting_steps_before_interactions_are_allowed;
123130
logit_models[category].fit(X, response_values[category], sample_weight, X_names, validation_indexes, prioritized_predictors_indexes,
124131
monotonic_constraints, VectorXi(0), interaction_constraints);
125132
}

0 commit comments

Comments
 (0)