def _set_model(self, model): '''Extract supported methods from the model. Each model needs to implement a class method called _get_queryable_methods() that tells this Predictive Object whether or not it expects a SFrame, SArray or other type as input, the 'query' method of this class will automatically convert to appropriate SFrame or SArray that is needed. The model method can also expect either an SArray or an SFrame, for example, recommender.recommend() method could expect the first parameter 'user' to be either a list of users or an SFrame with more information regarding the users. For example, recommender model would return the following information: {'predict': { 'dataset': 'sframe', 'new_observation_data': 'sframe', 'new_user_data': 'sframe', 'new_item_data': 'sframe' }, 'recommend': { 'users': ['sframe', 'sarray'] 'items': ['sframe', 'sarray'], 'new_observation_data': 'sframe', 'new_user_data': 'sframe', 'new_item_data': 'sframe', 'exclude': 'sframe'} } ''' if is_path(model): # This is a path, download the file and load it model = graphlab.load_model(model) self.model = model self._model_methods = model._get_queryable_methods() if type(self._model_methods) != dict: raise RuntimeError("_get_queryable_methods for model %s should return a \ dictionary" % model.__class__) for (method, description) in self._model_methods.iteritems(): if type(description) != dict: raise RuntimeError("model %s _get_queryable_methods should use dict as method\ description."% model.__class__) for (param_name, param_types) in description.iteritems(): # support either "sframe", "sarray" or ["sframe", "sarray"] if not isinstance(param_types, list): param_types = [param_types] for param_type in param_types: if (param_type not in ['sframe', 'sarray']): raise RuntimeError("model %s _get_queryable_methods should only use \ 'sframe' or 'sarray' type. %s is not supported" % (model.__class__, param_type)) description.update({param_name: param_types}) self._model_methods.update({method: description})
def __init__(self, model, description = ''): super(ModelPredictiveObject, self).__init__(description) if not (isinstance(model, graphlab.Model) or \ isinstance(model, graphlab.CustomModel)) and not is_path(model): raise TypeError('Model must be a GraphLab Create model or a path to a model.') self._set_model(model)