Source code for baybe.recommenders.pure.nonpredictive.sampling
"""Recommenders based on sampling."""fromtypingimportClassVarimportnumpyasnpimportpandasaspdfromsklearn.preprocessingimportStandardScalerfrombaybe.recommenders.pure.nonpredictive.baseimportNonPredictiveRecommenderfrombaybe.searchspaceimportSearchSpace,SearchSpaceType,SubspaceDiscretefrombaybe.utils.plottingimportto_stringfrombaybe.utils.sampling_algorithmsimportfarthest_point_sampling
[docs]classRandomRecommender(NonPredictiveRecommender):"""Recommends experiments randomly."""# Class variablescompatibility:ClassVar[SearchSpaceType]=SearchSpaceType.HYBRID# See base class.def_recommend_hybrid(self,searchspace:SearchSpace,candidates_exp:pd.DataFrame,batch_size:int,)->pd.DataFrame:# See base class.ifsearchspace.type==SearchSpaceType.DISCRETE:returncandidates_exp.sample(batch_size)cont_random=searchspace.continuous.sample_uniform(batch_size=batch_size)ifsearchspace.type==SearchSpaceType.CONTINUOUS:returncont_randomdisc_candidates,_=searchspace.discrete.get_candidates(True,True)# TODO decide mechanism if number of possible discrete candidates is smaller# than batch sizedisc_random=disc_candidates.sample(n=batch_size,replace=len(disc_candidates)<batch_size,)cont_random.index=disc_random.indexreturnpd.concat([disc_random,cont_random],axis=1)def__str__(self)->str:fields=[to_string("Compatibility",self.compatibility,single_line=True)]returnto_string(self.__class__.__name__,*fields)
[docs]classFPSRecommender(NonPredictiveRecommender):"""An initial recommender that selects candidates via Farthest Point Sampling."""# Class variablescompatibility:ClassVar[SearchSpaceType]=SearchSpaceType.DISCRETE# See base class.def_recommend_discrete(self,subspace_discrete:SubspaceDiscrete,candidates_exp:pd.DataFrame,batch_size:int,)->pd.Index:# See base class.# Fit scaler on entire search space# TODO [Scaling]: scaling should be handled by search space objectscaler=StandardScaler()scaler.fit(subspace_discrete.comp_rep)# Scale and samplecandidates_comp=subspace_discrete.transform(candidates_exp)candidates_scaled=np.ascontiguousarray(scaler.transform(candidates_comp))ilocs=farthest_point_sampling(candidates_scaled,batch_size)returncandidates_comp.index[ilocs]def__str__(self)->str:fields=[to_string("Compatibility",self.compatibility,single_line=True)]returnto_string(self.__class__.__name__,*fields)