Skip to content

Commit a5c6c2c

Browse files
removing intercept_steps
1 parent ae8fbe9 commit a5c6c2c

File tree

6 files changed

+19
-49
lines changed

6 files changed

+19
-49
lines changed

API_REFERENCE.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ The index of the term selected. So ***0*** is the first term, ***1*** is the sec
166166
***Returns the regression coefficient of the intercept term.***
167167

168168

169-
## Method: get_intercept_steps()
170-
171-
***Returns a numpy vector containing the regression coefficients of the intercept term by boosting step.***
172-
173-
174169
## Method: get_m()
175170

176171
***Returns the number of boosting steps in the model (the value that minimized validation error).***

aplr/aplr.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ def get_feature_importance(self)->npt.ArrayLike:
8585
def get_intercept(self)->float:
8686
return self.APLRRegressor.get_intercept()
8787

88-
def get_intercept_steps(self)->npt.ArrayLike:
89-
return self.APLRRegressor.get_intercept_steps()
90-
9188
def get_m(self)->int:
9289
return self.APLRRegressor.get_m()
9390

cpp/APLRRegressor.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ class APLRRegressor
109109
std::vector<std::string> term_names;
110110
VectorXd term_coefficients;
111111
size_t max_interaction_level;
112-
VectorXd intercept_steps;
113112
size_t max_interactions; //max interactions allowed to add (counted in interactions_eligible)
114113
size_t interactions_eligible; //interactions that were eligible when training the model
115114
VectorXd validation_error_steps; //validation error for each boosting step
@@ -146,7 +145,6 @@ class APLRRegressor
146145
VectorXd get_validation_error_steps();
147146
VectorXd get_feature_importance();
148147
double get_intercept();
149-
VectorXd get_intercept_steps();
150148
size_t get_m();
151149
double get_validation_group_mse();
152150
};
@@ -159,7 +157,7 @@ APLRRegressor::APLRRegressor(size_t m,double v,uint_fast32_t random_state,std::s
159157
reserved_terms_times_num_x{reserved_terms_times_num_x},intercept{intercept},m{m},v{v},
160158
family{family},link_function{link_function},validation_ratio{validation_ratio},n_jobs{n_jobs},random_state{random_state},
161159
bins{bins},verbosity{verbosity},max_interaction_level{max_interaction_level},
162-
intercept_steps{VectorXd(0)},max_interactions{max_interactions},interactions_eligible{0},validation_error_steps{VectorXd(0)},
160+
max_interactions{max_interactions},interactions_eligible{0},validation_error_steps{VectorXd(0)},
163161
min_observations_in_split{min_observations_in_split},ineligible_boosting_steps_added{ineligible_boosting_steps_added},
164162
max_eligible_terms{max_eligible_terms},number_of_base_terms{0},tweedie_power{tweedie_power},min_training_prediction_or_response{NAN_DOUBLE},
165163
max_training_prediction_or_response{NAN_DOUBLE},validation_group_mse{NAN_DOUBLE},group_size_for_validation_group_mse{group_size_for_validation_group_mse}
@@ -172,8 +170,8 @@ APLRRegressor::APLRRegressor(const APLRRegressor &other):
172170
family{other.family},link_function{other.link_function},validation_ratio{other.validation_ratio},
173171
n_jobs{other.n_jobs},random_state{other.random_state},bins{other.bins},
174172
verbosity{other.verbosity},term_names{other.term_names},term_coefficients{other.term_coefficients},
175-
max_interaction_level{other.max_interaction_level},intercept_steps{other.intercept_steps},
176-
max_interactions{other.max_interactions},interactions_eligible{other.interactions_eligible},validation_error_steps{other.validation_error_steps},
173+
max_interaction_level{other.max_interaction_level},max_interactions{other.max_interactions},
174+
interactions_eligible{other.interactions_eligible},validation_error_steps{other.validation_error_steps},
177175
min_observations_in_split{other.min_observations_in_split},ineligible_boosting_steps_added{other.ineligible_boosting_steps_added},
178176
max_eligible_terms{other.max_eligible_terms},number_of_base_terms{other.number_of_base_terms},
179177
feature_importance{other.feature_importance},tweedie_power{other.tweedie_power},min_training_prediction_or_response{other.min_training_prediction_or_response},
@@ -420,7 +418,6 @@ void APLRRegressor::initialize()
420418
terms.clear();
421419

422420
intercept=0;
423-
intercept_steps=VectorXd::Constant(m,0);
424421

425422
terms_eligible_current.reserve(X_train.cols()*reserved_terms_times_num_x);
426423
for (size_t i = 0; i < static_cast<size_t>(X_train.cols()); ++i)
@@ -519,7 +516,6 @@ void APLRRegressor::update_intercept()
519516
intercept=neg_gradient_current.mean();
520517
else
521518
intercept=(neg_gradient_current.array()*sample_weight_train.array()).sum()/sample_weight_train.array().sum();
522-
intercept_steps=VectorXd::Constant(m,intercept);
523519
linear_predictor_update=VectorXd::Constant(neg_gradient_current.size(),intercept);
524520
linear_predictor_update_validation=VectorXd::Constant(y_validation.size(),intercept);
525521
update_linear_predictor_and_predictors();
@@ -886,13 +882,6 @@ void APLRRegressor::print_summary_after_boosting_step(size_t boosting_step)
886882

887883
void APLRRegressor::update_coefficients_for_all_steps()
888884
{
889-
//Filling down coefficient_steps for the intercept
890-
for (size_t j = 0; j < m; ++j) //For each boosting step
891-
{
892-
if(j>0 && is_approximately_zero(intercept_steps[j]) && !is_approximately_zero(intercept_steps[j-1]))
893-
intercept_steps[j]=intercept_steps[j-1];
894-
}
895-
//Filling down coefficient_steps for each term in the model
896885
for (size_t i = 0; i < terms.size(); ++i) //For each term
897886
{
898887
for (size_t j = 0; j < m; ++j) //For each boosting step
@@ -916,7 +905,6 @@ void APLRRegressor::find_optimal_m_and_update_model_accordingly()
916905
//Choosing optimal m and updating coefficients
917906
size_t best_boosting_step_index;
918907
validation_error_steps.minCoeff(&best_boosting_step_index); //boosting step with lowest error
919-
intercept=intercept_steps[best_boosting_step_index];
920908
for (size_t i = 0; i < terms.size(); ++i) //for each term set coefficient
921909
{
922910
terms[i].coefficient=terms[i].coefficient_steps[best_boosting_step_index];
@@ -1194,11 +1182,6 @@ double APLRRegressor::get_intercept()
11941182
return intercept;
11951183
}
11961184

1197-
VectorXd APLRRegressor::get_intercept_steps()
1198-
{
1199-
return intercept_steps;
1200-
}
1201-
12021185
size_t APLRRegressor::get_m()
12031186
{
12041187
return m;

cpp/pythonbinding.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ PYBIND11_MODULE(aplr_cpp, m) {
3232
.def("get_validation_error_steps", &APLRRegressor::get_validation_error_steps)
3333
.def("get_feature_importance", &APLRRegressor::get_feature_importance)
3434
.def("get_intercept", &APLRRegressor::get_intercept)
35-
.def("get_intercept_steps", &APLRRegressor::get_intercept_steps)
3635
.def("get_m", &APLRRegressor::get_m)
3736
.def("get_validation_group_mse", &APLRRegressor::get_validation_group_mse)
3837
.def_readwrite("intercept", &APLRRegressor::intercept)
39-
.def_readwrite("intercept_steps", &APLRRegressor::intercept_steps)
4038
.def_readwrite("m", &APLRRegressor::m)
4139
.def_readwrite("v", &APLRRegressor::v)
4240
.def_readwrite("max_interaction_level", &APLRRegressor::max_interaction_level)
@@ -67,35 +65,34 @@ PYBIND11_MODULE(aplr_cpp, m) {
6765
[](const APLRRegressor &a) { // __getstate__
6866
/* Return a tuple that fully encodes the state of the object */
6967
return py::make_tuple(a.m,a.v,a.random_state,a.family,a.n_jobs,a.validation_ratio,a.intercept,a.bins,a.verbosity,
70-
a.max_interaction_level,a.max_interactions,a.validation_error_steps,a.term_names,a.term_coefficients,a.terms,a.intercept_steps,
68+
a.max_interaction_level,a.max_interactions,a.validation_error_steps,a.term_names,a.term_coefficients,a.terms,
7169
a.interactions_eligible,a.min_observations_in_split,a.ineligible_boosting_steps_added,a.max_eligible_terms,
7270
a.number_of_base_terms,a.feature_importance,a.link_function,a.tweedie_power,a.min_training_prediction_or_response,a.max_training_prediction_or_response,
7371
a.validation_group_mse,a.group_size_for_validation_group_mse);
7472
},
7573
[](py::tuple t) { // __setstate__
76-
if (t.size() != 28)
74+
if (t.size() != 27)
7775
throw std::runtime_error("Invalid state!");
7876

7977
/* Create a new C++ instance */
8078
APLRRegressor a(t[0].cast<size_t>(),t[1].cast<double>(),t[2].cast<uint_fast32_t>(),t[3].cast<std::string>(),
81-
t[22].cast<std::string>(),t[4].cast<size_t>(),t[5].cast<double>(),
82-
t[6].cast<double>(),100,t[7].cast<size_t>(),t[8].cast<size_t>(),t[9].cast<size_t>(),t[10].cast<double>(),t[17].cast<size_t>(),
83-
t[23].cast<double>());
79+
t[21].cast<std::string>(),t[4].cast<size_t>(),t[5].cast<double>(),
80+
t[6].cast<double>(),100,t[7].cast<size_t>(),t[8].cast<size_t>(),t[9].cast<size_t>(),t[10].cast<double>(),t[16].cast<size_t>(),
81+
t[22].cast<double>());
8482

8583
a.validation_error_steps=t[11].cast<VectorXd>();
8684
a.term_names=t[12].cast<std::vector<std::string>>();
8785
a.term_coefficients=t[13].cast<VectorXd>();
8886
a.terms=t[14].cast<std::vector<Term>>();
89-
a.intercept_steps=t[15].cast<VectorXd>();
90-
a.interactions_eligible=t[16].cast<size_t>();
91-
a.ineligible_boosting_steps_added=t[18].cast<size_t>();
92-
a.max_eligible_terms=t[19].cast<size_t>();
93-
a.number_of_base_terms=t[20].cast<size_t>();
94-
a.feature_importance=t[21].cast<VectorXd>();
95-
a.min_training_prediction_or_response=t[24].cast<double>();
96-
a.max_training_prediction_or_response=t[25].cast<double>();
97-
a.validation_group_mse=t[26].cast<double>();
98-
a.group_size_for_validation_group_mse=t[27].cast<size_t>();
87+
a.interactions_eligible=t[15].cast<size_t>();
88+
a.ineligible_boosting_steps_added=t[17].cast<size_t>();
89+
a.max_eligible_terms=t[18].cast<size_t>();
90+
a.number_of_base_terms=t[19].cast<size_t>();
91+
a.feature_importance=t[20].cast<VectorXd>();
92+
a.min_training_prediction_or_response=t[23].cast<double>();
93+
a.max_training_prediction_or_response=t[24].cast<double>();
94+
a.validation_group_mse=t[25].cast<double>();
95+
a.group_size_for_validation_group_mse=t[26].cast<size_t>();
9996

10097
return a;
10198
}

examples/train_aplr_cross_validation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
#Terms in the best model
4949
terms=pd.DataFrame({"term":best_model.get_term_names(),"coefficient":best_model.get_term_coefficients()})
5050

51-
#Coefficients for intercept and the first term per boosting step
52-
intercept_coefficient_per_boosting_step = best_model.get_intercept_steps()
51+
#Coefficients for the first term per boosting step
5352
first_term_coefficient_per_boosting_step = best_model.get_term_coefficient_steps(term_index=0)
5453

5554
#Estimated feature importance was estimated on the validation set when the best model was trained

examples/train_aplr_validation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
#Terms in the best model
6060
terms=pd.DataFrame({"term":best_model.get_term_names(),"coefficient":best_model.get_term_coefficients()})
6161

62-
#Coefficients for intercept and the first term per boosting step
63-
intercept_coefficient_per_boosting_step = best_model.get_intercept_steps()
62+
#Coefficients for the first term per boosting step
6463
first_term_coefficient_per_boosting_step = best_model.get_term_coefficient_steps(term_index=0)
6564

6665
#Estimated feature importance was estimated on the validation set when the best model was trained

0 commit comments

Comments
 (0)