def __init__(self, **kwargs): """ Even Length Grid Partitioner :param seasonality: Time granularity, from pyFTS.models.seasonal.common.DateTime :param data: Training data of which the universe of discourse will be extracted. The universe of discourse is the open interval between the minimum and maximum values of the training data. :param npart: The number of universe of discourse partitions, i.e., the number of fuzzy sets that will be created :param func: Fuzzy membership function (pyFTS.common.Membership) """ super(TimeGridPartitioner, self).__init__(name="TimeGrid", preprocess=False, **kwargs) self.season = kwargs.get('seasonality', DateTime.day_of_year) '''Seasonality, a pyFTS.models.seasonal.common.DateTime object''' self.mask = kwargs.get('mask', '%Y-%m-%d %H:%M:%S') '''A string with datetime formating mask''' data = kwargs.get('data', None) if self.season == DateTime.year: ndata = [strip_datepart(k, self.season) for k in data] self.min = min(ndata) self.max = max(ndata) else: tmp = (self.season.value / self.partitions) / 2 self.min = tmp self.max = self.season.value + tmp self.type = kwargs.get('type', 'seasonal') self.sets = self.build(None) if self.ordered_sets is None and self.setnames is not None: self.ordered_sets = self.setnames else: self.ordered_sets = FS.set_ordered(self.sets) if self.type == 'seasonal': self.extractor = lambda x: strip_datepart(x, self.season, self.mask )
def forecast(self, ndata, **kwargs): explain = kwargs.get('explain', False) l = len(ndata) if not explain else 1 ret = [] for k in np.arange(0, l): actual = FuzzySet.get_maximum_membership_fuzzyset( ndata[k], self.sets) if explain: print("Fuzzyfication:\n\n {} -> {} \n".format( ndata[k], actual.name)) if actual.name not in self.flrgs: ret.append(actual.centroid) if explain: print("Rules:\n\n {} -> {} (Naïve)\t Midpoint: {} \n\n". format(actual.name, actual.name, actual.centroid)) else: _flrg = self.flrgs[actual.name] mp = _flrg.get_midpoint(self.sets) ret.append(mp) if explain: print("Rules:\n\n {} \t Midpoint: {} \n".format( str(_flrg), mp)) print("Deffuzyfied value: {} \n".format(mp)) return ret
def __init__(self, **kwargs): """ Fixed UoD with Even Length Grid Partitioner """ kwargs['preprocess'] = False super(GridPartitioner, self).__init__(name="Grid", **kwargs) self.max = kwargs.get('max', None) self.min = kwargs.get('min', None) if self.max is None or self.min is None: raise Exception( "It is mandatory to inform the max and min parameters!") self.sets = self.build(None) self.partitions = len(self.sets) if self.ordered_sets is None and self.setnames is not None: self.ordered_sets = self.setnames[:len(self.sets)] else: self.ordered_sets = FuzzySet.set_ordered(self.sets)
def __init__(self, **kwargs): """ Universe of Discourse partitioner scheme. Split data on several fuzzy sets """ self.name = kwargs.get('name', "") """partitioner name""" self.partitions = kwargs.get('npart', 10) """The number of universe of discourse partitions, i.e., the number of fuzzy sets that will be created""" self.sets = {} self.membership_function = kwargs.get('func', Membership.trimf) """Fuzzy membership function (pyFTS.common.Membership)""" self.setnames = kwargs.get('names', None) """list of partitions names. If None is given the partitions will be auto named with prefix""" self.prefix = kwargs.get('prefix', 'A') """prefix of auto generated partition names""" self.transformation = kwargs.get('transformation', None) """data transformation to be applied on data""" self.indexer = kwargs.get('indexer', None) self.variable = kwargs.get('variable', None) """In a multivariate context, the variable that contains this partitioner""" self.type = kwargs.get('type', 'common') """The type of fuzzy sets that are generated by this partitioner""" self.ordered_sets = None """A ordered list of the fuzzy sets names, sorted by their middle point""" self.kdtree = None """A spatial index to help in fuzzyfication""" self.margin = kwargs.get("margin", 0.1) """The upper and lower exceeding margins for the known UoD. The default value is .1""" self.lower_margin = kwargs.get("lower_margin", self.margin) """Specific lower exceeding margins for the known UoD. The default value is the self.margin parameter""" self.upper_margin = kwargs.get("lower_margin", self.margin) """Specific upper exceeding margins for the known UoD. The default value is the self.margin parameter""" if kwargs.get('preprocess', True): data = kwargs.get('data', [None]) if self.indexer is not None: ndata = self.indexer.get_data(data) else: ndata = data if self.transformation is not None: ndata = self.transformation.apply(ndata) else: ndata = data if self.indexer is not None: ndata = self.indexer.get_data(ndata) _min = np.nanmin(ndata) if _min == -np.inf: ndata[ndata == -np.inf] = 0 _min = np.nanmin(ndata) self.min = float(_min * (1 + self.lower_margin) if _min < 0 else _min * (1 - self.lower_margin)) _max = np.nanmax(ndata) self.max = float(_max * (1 + self.upper_margin) if _max > 0 else _max * (1 - self.upper_margin)) self.sets = self.build(ndata) self.partitions = len(self.sets) if self.ordered_sets is None and self.setnames is not None: self.ordered_sets = self.setnames[:len(self.sets)] else: self.ordered_sets = FuzzySet.set_ordered(self.sets) del (ndata)
def train(self, data, **kwargs): if kwargs.get('sets', None) is not None: self.sets = kwargs.get('sets', None) tmpdata = FuzzySet.fuzzyfy_series_old(data, self.sets) flrs = FLR.generate_non_recurrent_flrs(tmpdata) self.generate_flrg(flrs)
def __init__(self, **kwargs): """ Universe of Discourse partitioner scheme. Split data on several fuzzy sets """ self.name = kwargs.get('name',"") """partitioner name""" self.partitions = kwargs.get('npart', 10) """The number of universe of discourse partitions, i.e., the number of fuzzy sets that will be created""" self.sets = {} self.membership_function = kwargs.get('func', Membership.trimf) """Fuzzy membership function (pyFTS.common.Membership)""" self.setnames = kwargs.get('names', None) """list of partitions names. If None is given the partitions will be auto named with prefix""" self.prefix = kwargs.get('prefix', 'A') """prefix of auto generated partition names""" self.transformation = kwargs.get('transformation', None) """data transformation to be applied on data""" self.indexer = kwargs.get('indexer', None) self.variable = kwargs.get('variable', None) """In a multivariate context, the variable that contains this partitioner""" self.type = kwargs.get('type', 'common') """The type of fuzzy sets that are generated by this partitioner""" self.extractor = kwargs.get('extractor', lambda x: x) """Anonymous function used to extract a single primitive type from an object instance""" self.ordered_sets = None """A ordered list of the fuzzy sets names, sorted by their middle point""" self.kdtree = None """A spatial index to help in fuzzyfication""" if kwargs.get('preprocess',True): data = kwargs.get('data',[None]) if self.indexer is not None: ndata = self.indexer.get_data(data) else: ndata = data if self.transformation is not None: ndata = self.transformation.apply(ndata) else: ndata = data if self.indexer is not None: ndata = self.indexer.get_data(ndata) _min = np.nanmin(ndata) if _min == -np.inf: ndata[ndata == -np.inf] = 0 _min = np.nanmin(ndata) self.min = float(_min * 1.1 if _min < 0 else _min * 0.9) _max = np.nanmax(ndata) self.max = float(_max * 1.1 if _max > 0 else _max * 0.9) self.sets = self.build(ndata) if self.ordered_sets is None and self.setnames is not None: self.ordered_sets = self.setnames[:len(self.sets)] else: self.ordered_sets = FuzzySet.set_ordered(self.sets) del(ndata)
def train(self, ndata, **kwargs): tmpdata = FuzzySet.fuzzyfy_series(ndata, self.sets, method='maximum') flrs = FLR.generate_recurrent_flrs(tmpdata) self.generate_flrg(flrs)
def append(self, name, mf, parameters, **kwargs): """ Append a new partition (fuzzy set) to the partitioner :param name: Fuzzy set name :param mf: One of the pyFTS.common.Membership functions :param parameters: A list with the parameters for the membership function :param kwargs: Optional arguments for the fuzzy set """ if name is None or len(name) == 0: raise ValueError("The name of the fuzzy set cannot be empty") if name in self.sets: raise ValueError("This name has already been used") if mf is None or mf not in (Membership.trimf, Membership.gaussmf, Membership.trapmf, Membership.singleton, Membership.sigmf): raise ValueError( "The mf parameter should be one of pyFTS.common.Membership functions, not {}" .format(mf)) if mf == Membership.trimf: if len(parameters) != 3: raise ValueError( "Incorrect number of parameters for the Membership.trimf") centroid = parameters[1] elif mf == Membership.gaussmf: if len(parameters) != 2: raise ValueError( "Incorrect number of parameters for the Membership.gaussmf" ) centroid = parameters[0] elif mf == Membership.trapmf: if len(parameters) != 4: raise ValueError( "Incorrect number of parameters for the Membership.trapmf") centroid = (parameters[1] + parameters[2]) / 2 elif mf == Membership.singleton: if len(parameters) != 1: raise ValueError( "Incorrect number of parameters for the Membership.singleton" ) centroid = parameters[0] elif mf == Membership.sigmf: if len(parameters) != 2: raise ValueError( "Incorrect number of parameters for the Membership.sigmf") centroid = parameters[1] + (parameters[1] / (2 * parameters[0])) self.sets[name] = FuzzySet.FuzzySet(name, mf, parameters, centroid, **kwargs) self.partitions += 1 self.ordered_sets = [ key for key in sorted(self.sets.keys(), key=lambda k: self.sets[k].centroid) ] self.min = self.sets[self.ordered_sets[0]].lower self.max = self.sets[self.ordered_sets[-1]].upper
def generate_lhs_flrg(self, sample, explain=False): nsample = [FuzzySet.fuzzyfy(k, partitioner=self.partitioner, mode="sets", alpha_cut=self.alpha_cut) for k in sample] return self.generate_lhs_flrg_fuzzyfied(nsample, explain)
def train(self, data, **kwargs): tmpdata = FuzzySet.fuzzyfy_series(data, self.sets, method='maximum') flrs = FLR.generate_non_recurrent_flrs(tmpdata) self.operation_matrix(flrs)
def fuzzyfy_instance(data_point, var): return FuzzySet.fuzzyfy(data_point, var.partitioner, mode='sets', method='fuzzy', alpha_cut=var.alpha_cut)