classModelKNNRegressor(ModelRegressorMixin,ModelPipeline):'''K-nearest Neighbors model for regression'''_default_name='model_knn_regressor'def__init__(self,knn_params:Union[dict,None]=None,**kwargs)->None:'''Initialization of the class (see ModelPipeline, ModelClass & ModelRegressorMixin for more arguments) Kwargs: knn_params (dict) : Parameters for the K-nearest Neighbors '''# Init.super().__init__(**kwargs)# Get logger (must be done after super init)self.logger=logging.getLogger(__name__)# Manage modelifknn_paramsisNone:knn_params={}self.knn=KNeighborsRegressor(**knn_params)# We define a pipeline in order to be compatible with other modelsself.pipeline=Pipeline([('knn',self.knn)])defreload_from_standalone(self,**kwargs)->None:'''Reloads a model from its configuration and "standalones" files - /!\\ Experimental /!\\ - Kwargs: configuration_path (str): Path to configuration file sklearn_pipeline_path (str): Path to standalone pipeline preprocess_pipeline_path (str): Path to preprocess pipeline Raises: ValueError: If configuration_path is None ValueError: If sklearn_pipeline_path is None ValueError: If preprocess_pipeline_path is None FileNotFoundError: If the object configuration_path is not an existing file FileNotFoundError: If the object sklearn_pipeline_path is not an existing file FileNotFoundError: If the object preprocess_pipeline_path is not an existing file '''# Retrieve argsconfiguration_path=kwargs.get('configuration_path',None)sklearn_pipeline_path=kwargs.get('sklearn_pipeline_path',None)preprocess_pipeline_path=kwargs.get('preprocess_pipeline_path',None)# Checksifconfiguration_pathisNone:raiseValueError("The argument configuration_path can't be None")ifsklearn_pipeline_pathisNone:raiseValueError("The argument sklearn_pipeline_path can't be None")ifpreprocess_pipeline_pathisNone:raiseValueError("The argument preprocess_pipeline_path can't be None")ifnotos.path.exists(configuration_path):raiseFileNotFoundError(f"The file {configuration_path} does not exist")ifnotos.path.exists(sklearn_pipeline_path):raiseFileNotFoundError(f"The file {sklearn_pipeline_path} does not exist")ifnotos.path.exists(preprocess_pipeline_path):raiseFileNotFoundError(f"The file {preprocess_pipeline_path} does not exist")# Load confswithopen(configuration_path,'r',encoding='utf-8')asf:configs=json.load(f)# Set class vars# self.model_name = # Keep the created name# self.model_dir = # Keep the created folderself.nb_fit=configs.get('nb_fit',1)# Consider one unique fit by defaultself.trained=configs.get('trained',True)# Consider trained by default# Try to read the following attributes from configs and, if absent, keep the current oneforattributein['model_type','x_col','y_col','columns_in','mandatory_columns','level_save']:setattr(self,attribute,configs.get(attribute,getattr(self,attribute)))# Reload pipeline modelwithopen(sklearn_pipeline_path,'rb')asf:self.pipeline=pickle.load(f)# Reload pipeline elementsself.knn=self.pipeline['knn']# Reload pipeline preprocessingwithopen(preprocess_pipeline_path,'rb')asf:self.preprocess_pipeline=pickle.load(f)
__init__(knn_params=None,**kwargs)
Initialization of the class (see ModelPipeline, ModelClass & ModelRegressorMixin for more arguments)
Kwargs
knn_params (dict) : Parameters for the K-nearest Neighbors
Source code in template_num/models_training/regressors/models_sklearn/model_knn_regressor.py
def__init__(self,knn_params:Union[dict,None]=None,**kwargs)->None:'''Initialization of the class (see ModelPipeline, ModelClass & ModelRegressorMixin for more arguments) Kwargs: knn_params (dict) : Parameters for the K-nearest Neighbors '''# Init.super().__init__(**kwargs)# Get logger (must be done after super init)self.logger=logging.getLogger(__name__)# Manage modelifknn_paramsisNone:knn_params={}self.knn=KNeighborsRegressor(**knn_params)# We define a pipeline in order to be compatible with other modelsself.pipeline=Pipeline([('knn',self.knn)])
reload_from_standalone(**kwargs)
Reloads a model from its configuration and "standalones" files
- /! Experimental /! -
Kwargs
configuration_path (str): Path to configuration file
sklearn_pipeline_path (str): Path to standalone pipeline
preprocess_pipeline_path (str): Path to preprocess pipeline
Raises:
ValueError: If configuration_path is None
ValueError: If sklearn_pipeline_path is None
ValueError: If preprocess_pipeline_path is None
FileNotFoundError: If the object configuration_path is not an existing file
FileNotFoundError: If the object sklearn_pipeline_path is not an existing file
FileNotFoundError: If the object preprocess_pipeline_path is not an existing file
Source code in template_num/models_training/regressors/models_sklearn/model_knn_regressor.py
defreload_from_standalone(self,**kwargs)->None:'''Reloads a model from its configuration and "standalones" files - /!\\ Experimental /!\\ - Kwargs: configuration_path (str): Path to configuration file sklearn_pipeline_path (str): Path to standalone pipeline preprocess_pipeline_path (str): Path to preprocess pipeline Raises: ValueError: If configuration_path is None ValueError: If sklearn_pipeline_path is None ValueError: If preprocess_pipeline_path is None FileNotFoundError: If the object configuration_path is not an existing file FileNotFoundError: If the object sklearn_pipeline_path is not an existing file FileNotFoundError: If the object preprocess_pipeline_path is not an existing file '''# Retrieve argsconfiguration_path=kwargs.get('configuration_path',None)sklearn_pipeline_path=kwargs.get('sklearn_pipeline_path',None)preprocess_pipeline_path=kwargs.get('preprocess_pipeline_path',None)# Checksifconfiguration_pathisNone:raiseValueError("The argument configuration_path can't be None")ifsklearn_pipeline_pathisNone:raiseValueError("The argument sklearn_pipeline_path can't be None")ifpreprocess_pipeline_pathisNone:raiseValueError("The argument preprocess_pipeline_path can't be None")ifnotos.path.exists(configuration_path):raiseFileNotFoundError(f"The file {configuration_path} does not exist")ifnotos.path.exists(sklearn_pipeline_path):raiseFileNotFoundError(f"The file {sklearn_pipeline_path} does not exist")ifnotos.path.exists(preprocess_pipeline_path):raiseFileNotFoundError(f"The file {preprocess_pipeline_path} does not exist")# Load confswithopen(configuration_path,'r',encoding='utf-8')asf:configs=json.load(f)# Set class vars# self.model_name = # Keep the created name# self.model_dir = # Keep the created folderself.nb_fit=configs.get('nb_fit',1)# Consider one unique fit by defaultself.trained=configs.get('trained',True)# Consider trained by default# Try to read the following attributes from configs and, if absent, keep the current oneforattributein['model_type','x_col','y_col','columns_in','mandatory_columns','level_save']:setattr(self,attribute,configs.get(attribute,getattr(self,attribute)))# Reload pipeline modelwithopen(sklearn_pipeline_path,'rb')asf:self.pipeline=pickle.load(f)# Reload pipeline elementsself.knn=self.pipeline['knn']# Reload pipeline preprocessingwithopen(preprocess_pipeline_path,'rb')asf:self.preprocess_pipeline=pickle.load(f)