Navigation

Quick search

Enter search terms or a module, class or function name.

Source code for mlproblems.ranking

# Copyright 2011 Hugo Larochelle. All rights reserved.# # Redistribution and use in source and binary forms, with or without modification, are# permitted provided that the following conditions are met:# # 1. Redistributions of source code must retain the above copyright notice, this list of# conditions and the following disclaimer.# # 2. Redistributions in binary form must reproduce the above copyright notice, this list# of conditions and the following disclaimer in the documentation and/or other materials# provided with the distribution.# # THIS SOFTWARE IS PROVIDED BY Hugo Larochelle ``AS IS'' AND ANY EXPRESS OR IMPLIED# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Hugo Larochelle OR# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.# # The views and conclusions contained in the software and documentation are those of the# authors and should not be interpreted as representing official policies, either expressed# or implied, of Hugo Larochelle."""The ``mlproblems.ranking`` module contains MLProblems specificallyfor ranking problems.This module contains the following classes:* RankingProblem: . Generates a ranking problem.* RankingToClassificationProblem: Generates a classification problem from a ranking problem.* RankingToRegressionProblem: Generates a regression problem from a ranking problem."""importgenericasmlpbimportnumpyasnp

[docs]classRankingProblem(mlpb.MLProblem):""" Generates a ranking problem. The data should be an iterator input/target/query pairs, where the target is a relevance score. When grouping query data together, is it assumed that examples from the same query are next to each other in the data (e.g. there aren't examples from the same query at the beginning and end of the data). The ranking examples become triplets (inputs_for_query,targets_for_query,query) where inputs_for_query and target_for_query are the lists of inputs and targets for a given query. **Required metadata:** * ``'n_queries'``: Number of queries (optional, will set the output of ``__len__(self)``). """def__init__(self,data=None,metadata={},call_setup=True):mlpb.MLProblem.__init__(self,data,metadata)self.__length__=Noneif'n_queries'inself.metadata:# Gives a chance to set length through metadataself.__length__=self.metadata['n_queries']delself.metadata['n_queries']# So that it isn't passed to subsequent mlproblemsifcall_setup:RankingProblem.setup(self)def__iter__(self):tot_input=[]tot_target=[]last_query=Noneforinput,target,queryinself.data:iflast_query!=None:# Is not first exampleifnotnp.all(last_query==query):# Yield ranking example if query changedyield(tot_input,tot_target,last_query)tot_input=[]tot_target=[]tot_input+=[input]tot_target+=[target]last_query=queryiftot_input:# Output last ranking exampleyield(tot_input,tot_target,last_query)

[docs]classRankingToClassificationProblem(mlpb.MLProblem):""" Generates a classification problem from a ranking problem. Option ``'merge_document_and_query'`` (a function with 2 arguments) is used to generate inputs for the classification problem. The list of possible scores must be provided as a metadata. **IMPORTANT:** the scores should be ordered from less to more relevant in the list. This list will be used to generate the set of targets and 'class_to_id' mapping. **Required_metadata:** * ``'scores'``: List of possible scores, ordered from less relevant to more relevant. * ``'n_pairs'``: Number of document/query pairs (optional, will set the output of ``__len__(self)``). **Defined metadata:** * ``'targets'`` * ``'class_to_id'`` """def__init__(self,data=None,metadata={},call_setup=True,merge_document_and_query=None):mlpb.MLProblem.__init__(self,data,metadata)self.merge_document_and_query=merge_document_and_queryself.__length__=Noneif'n_pairs'inself.metadata:# Gives a chance to set length through metadataself.__length__=self.metadata['n_pairs']delself.metadata['n_pairs']# So that it isn't passed to subsequent mlproblemsifcall_setup:RankingToClassificationProblem.setup(self)def__iter__(self):forinputs,targets,queryinself.data:forinput,targetinzip(inputs,targets):iftargetinself.class_to_id:yieldself.merge_document_and_query(input,query),self.class_to_id[target]else:yieldself.merge_document_and_query(input,query),None# For unlabeled datadefsetup(self):# Creating class (string) to id (integer) mappingself.class_to_id={}current_id=0forscoreinself.metadata['scores']:self.class_to_id[score]=current_idcurrent_id+=1self.metadata['class_to_id']=self.class_to_idself.metadata['targets']=set(self.metadata['scores'])defapply_on(self,new_data,new_metadata={}):ifself.__source_mlproblem__isnotNone:new_data=self.__source_mlproblem__.apply_on(new_data,new_metadata)new_metadata={}# new_data should already contain the new_metadata, since it is an mlproblemnew_problem=RankingToClassificationProblem(new_data,new_metadata,call_setup=False,merge_document_and_query=self.merge_document_and_query)new_problem.metadata['class_to_id']=self.metadata['class_to_id']new_problem.metadata['targets']=self.metadata['targets']new_problem.class_to_id=self.class_to_idreturnnew_problem

[docs]classRankingToRegressionProblem(mlpb.MLProblem):""" Generates a regression problem from a ranking problem. Option ``'merge_document_and_query'`` (a function with 2 arguments) is used to generate inputs for the classification problem. **Required_metadata:** * ``'n_pairs'``: Number of document/query pairs (optional, will set the output of ``__len__(self)``). """def__init__(self,data=None,metadata={},call_setup=True,merge_document_and_query=None):mlpb.MLProblem.__init__(self,data,metadata)self.merge_document_and_query=merge_document_and_queryself.__length__=Noneif'n_pairs'inself.metadata:# Gives a chance to set length through metadataself.__length__=self.metadata['n_pairs']delself.metadata['n_pairs']# So that it isn't passed to subsequent mlproblemsifcall_setup:RankingToRegressionProblem.setup(self)def__iter__(self):forinputs,targets,queryinself.data:forinput,targetinzip(inputs,targets):yieldself.merge_document_and_query(input,query),targetdefapply_on(self,new_data,new_metadata={}):ifself.__source_mlproblem__isnotNone:new_data=self.__source_mlproblem__.apply_on(new_data,new_metadata)new_metadata={}# new_data should already contain the new_metadata, since it is an mlproblemnew_problem=RankingToRegressionProblem(new_data,new_metadata,call_setup=False,merge_document_and_query=self.merge_document_and_query)returnnew_problem