@@ -46,7 +46,8 @@ class APLRRegressor
4646    bool  abort_boosting;
4747
4848    // Methods
49-     void  validate_input_to_fit (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight,const  std::vector<std::string> &X_names);
49+     void  validate_input_to_fit (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight,const  std::vector<std::string> &X_names, const  std::vector<size_t > &validation_set_indexes);
50+     void  throw_error_if_validation_set_indexes_has_invalid_indexes (const  VectorXd &y, const  std::vector<size_t > &validation_set_indexes);
5051    void  define_training_and_validation_sets (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight, const  std::vector<size_t > &validation_set_indexes);
5152    void  initialize (const  MatrixXd &X);
5253    void  add_term_to_terms_eligible_current (Term &term);
@@ -161,7 +162,7 @@ APLRRegressor::~APLRRegressor()
161162// invalidating validation_ratio. The rest of indices are used to train. 
162163void  APLRRegressor::fit (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight,const  std::vector<std::string> &X_names,const  std::vector<size_t > &validation_set_indexes)
163164{
164-     validate_input_to_fit (X,y,sample_weight,X_names);
165+     validate_input_to_fit (X,y,sample_weight,X_names,validation_set_indexes );
165166    define_training_and_validation_sets (X,y,sample_weight,validation_set_indexes);
166167    initialize (X);
167168    execute_boosting_steps ();
@@ -173,12 +174,28 @@ void APLRRegressor::fit(const MatrixXd &X,const VectorXd &y,const VectorXd &samp
173174    cleanup_after_fit ();
174175}
175176
176- void  APLRRegressor::validate_input_to_fit (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight,const  std::vector<std::string> &X_names)
177+ void  APLRRegressor::validate_input_to_fit (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight,const  std::vector<std::string> &X_names,  const  std::vector< size_t > &validation_set_indexes )
177178{
178179    if (X.rows ()!=y.size ()) throw  std::runtime_error (" X and y must have the same number of rows."  );
179180    if (X.rows ()==0 ) throw  std::runtime_error (" X and y cannot have zero rows."  );
180181    if (sample_weight.size ()>0  && sample_weight.size ()!=y.size ()) throw  std::runtime_error (" sample_weight must have 0 or as many rows as X and y."  );
181182    if (X_names.size ()>0  && X_names.size ()!=static_cast <size_t >(X.cols ())) throw  std::runtime_error (" X_names must have as many columns as X."  );
183+     throw_error_if_matrix_has_nan_or_infinite_elements (X, " X"  );
184+     throw_error_if_matrix_has_nan_or_infinite_elements (y, " y"  );
185+     throw_error_if_matrix_has_nan_or_infinite_elements (sample_weight, " sample_weight"  );
186+     throw_error_if_validation_set_indexes_has_invalid_indexes (y, validation_set_indexes);
187+ }
188+ 
189+ void  APLRRegressor::throw_error_if_validation_set_indexes_has_invalid_indexes (const  VectorXd &y, const  std::vector<size_t > &validation_set_indexes)
190+ {
191+     bool  validation_set_indexes_is_provided{validation_set_indexes.size ()>0 };
192+     if (validation_set_indexes_is_provided)
193+     {
194+         size_t  max_index{*std::max_element (validation_set_indexes.begin (), validation_set_indexes.end ())};
195+         bool  validation_set_indexes_has_elements_out_of_bounds{max_index > static_cast <size_t >(y.size ()-1 )};
196+         if (validation_set_indexes_has_elements_out_of_bounds)
197+             throw  std::runtime_error (" validation_set_indexes has elements that are out of bounds."  );
198+     }
182199}
183200
184201void  APLRRegressor::define_training_and_validation_sets (const  MatrixXd &X,const  VectorXd &y,const  VectorXd &sample_weight, const  std::vector<size_t > &validation_set_indexes)
@@ -815,6 +832,7 @@ void APLRRegressor::validate_that_model_can_be_used(const MatrixXd &X)
815832    if (X.rows ()==0 ) throw  std::runtime_error (" X cannot have zero rows."  );
816833    size_t  cols_provided{static_cast <size_t >(X.cols ())};
817834    if (cols_provided!=number_of_base_terms) throw  std::runtime_error (" X must have "  +std::to_string (number_of_base_terms) +"  columns but "  +std::to_string (cols_provided)+"  were provided."  );
835+     throw_error_if_matrix_has_nan_or_infinite_elements (X, " X"  );
818836}
819837
820838void  APLRRegressor::cleanup_after_fit ()
0 commit comments