Seasonal models refactoring; Composite fuzzy sets; Seasonal fuzzy set

This commit is contained in:
Petrônio Cândido 2017-09-28 10:16:28 -03:00
parent a0016a7371
commit 71cbb84574
9 changed files with 134 additions and 40 deletions

39
pyFTS/common/Composite.py Normal file
View File

@ -0,0 +1,39 @@
"""
Composite Fuzzy Sets
"""
import numpy as np
from pyFTS import *
from pyFTS.common import Membership, FuzzySet
class FuzzySet(FuzzySet.FuzzySet):
"""
Composite Fuzzy Set
"""
def __init__(self, name):
"""
Create an empty composite fuzzy set
:param name: fuzzy set name
"""
super(FuzzySet, self).__init__(self, name=name, mf=None, parameters=None, centroid=None)
self.mf = []
self.parameters = []
def membership(self, x):
"""
Calculate the membership value of a given input
:param x: input value
:return: membership value of x at this fuzzy set
"""
return min([self.mf[ct](x, self.parameters[ct]) for ct in np.arange(0, len(self.mf))])
def append(self, mf, parameters):
"""
Adds a new function to composition
:param mf:
:param parameters:
:return:
"""
self.mf.append(mf)
self.parameters.append(parameters)

View File

@ -1,10 +1,33 @@
"""
Non Stationary Fuzzy Sets
GARIBALDI, Jonathan M.; JAROSZEWSKI, Marcin; MUSIKASUWAN, Salang. Nonstationary fuzzy sets.
IEEE Transactions on Fuzzy Systems, v. 16, n. 4, p. 1072-1086, 2008.
"""
import numpy as np import numpy as np
from pyFTS import * from pyFTS import *
from pyFTS.common import FuzzySet, Membership from pyFTS.common import FuzzySet, Membership
class NonStationaryMembershipFunction(object): class MembershipFunction(object):
"""
Non Stationary Membership Function
"""
def __init__(self, name, mf, parameters, **kwargs): def __init__(self, name, mf, parameters, **kwargs):
"""
Non Stationary Membership Function
:param name:
:param mf:
:param parameters:
:param kwargs:
- location: Pertubation function that affects the location of the membership function
- location_params: Parameters for location pertubation function
- width: Pertubation function that affects the width of the membership function
- width_params: Parameters for width pertubation function
- noise: Pertubation function that adds noise on the membership function
- noise_params: Parameters for noise pertubation function
"""
self.mf = mf self.mf = mf
self.parameters = parameters self.parameters = parameters
self.location = kwargs.get("location", None) self.location = kwargs.get("location", None)
@ -73,12 +96,9 @@ class NonStationaryMembershipFunction(object):
return tmp return tmp
class NonStationaryFuzzySet(FuzzySet.FuzzySet): class FuzzySet(FuzzySet.FuzzySet):
""" """
Non Stationary Fuzzy Sets Non Stationary Fuzzy Sets
GARIBALDI, Jonathan M.; JAROSZEWSKI, Marcin; MUSIKASUWAN, Salang. Nonstationary fuzzy sets.
IEEE Transactions on Fuzzy Systems, v. 16, n. 4, p. 1072-1086, 2008.
""" """
def __init__(self, name, mf, **kwargs): def __init__(self, name, mf, **kwargs):
@ -89,4 +109,7 @@ class NonStationaryFuzzySet(FuzzySet.FuzzySet):
""" """
super(FuzzySet, self).__init__(name=name, mf=mf, parameters=None, centroid=None) super(FuzzySet, self).__init__(name=name, mf=mf, parameters=None, centroid=None)
def membership(self, x, t):
return self.mf.membership(x,t)

View File

@ -0,0 +1,12 @@
import numpy as np
from pyFTS.common import FuzzySet, FLR
from pyFTS import fts, sfts
class NonStationaryFTS(sfts.SeasonalFTS):
"""NonStationaryFTS Fuzzy Time Series"""
def __init__(self, name, **kwargs):
super(NonStationaryFTS, self).__init__(1, "NSFTS " + name, **kwargs)
self.name = "Non Stationary FTS"
self.detail = ""
self.flrgs = {}

View File

@ -1,6 +1,7 @@
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from enum import Enum from enum import Enum
from pyFTS.seasonal import common
class SeasonalIndexer(object): class SeasonalIndexer(object):
@ -124,17 +125,6 @@ class DataFrameSeasonalIndexer(SeasonalIndexer):
return data return data
class DateTime(Enum):
year = 1
month = 2
day_of_month = 3
day_of_year = 4
day_of_week = 5
hour = 6
minute = 7
second = 8
class DateTimeSeasonalIndexer(SeasonalIndexer): class DateTimeSeasonalIndexer(SeasonalIndexer):
def __init__(self,date_field, index_fields, index_seasons, data_fields,**kwargs): def __init__(self,date_field, index_fields, index_seasons, data_fields,**kwargs):
super(DateTimeSeasonalIndexer, self).__init__(len(index_seasons), **kwargs) super(DateTimeSeasonalIndexer, self).__init__(len(index_seasons), **kwargs)
@ -143,29 +133,6 @@ class DateTimeSeasonalIndexer(SeasonalIndexer):
self.data_fields = data_fields self.data_fields = data_fields
self.date_field = date_field self.date_field = date_field
def strip_datepart(self, date, date_part, resolution):
if date_part == DateTime.year:
tmp = date.year
elif date_part == DateTime.month:
tmp = date.month
elif date_part == DateTime.day_of_year:
tmp = date.timetuple().tm_yday
elif date_part == DateTime.day_of_month:
tmp = date.day
elif date_part == DateTime.day_of_week:
tmp = date.weekday()
elif date_part == DateTime.hour:
tmp = date.hour
elif date_part == DateTime.minute:
tmp = date.minute
elif date_part == DateTime.second:
tmp = date.second
if resolution is None:
return tmp
else:
return tmp // resolution
def get_season_of_data(self, data): def get_season_of_data(self, data):
ret = [] ret = []
@ -175,7 +142,10 @@ class DateTimeSeasonalIndexer(SeasonalIndexer):
date = data[self.date_field][ix] date = data[self.date_field][ix]
season = [] season = []
for c, f in enumerate(self.fields, start=0): for c, f in enumerate(self.fields, start=0):
season.append(self.strip_datepart(date, f, self.seasons[c])) tmp = common.strip_datepart(date, f)
if self.seasons[c] is not None:
tmp = tmp // self.seasons[c]
season.append(tmp)
ret.append(season) ret.append(season)
elif isinstance(data, pd.Series): elif isinstance(data, pd.Series):

50
pyFTS/seasonal/common.py Normal file
View File

@ -0,0 +1,50 @@
import numpy as np
import pandas as pd
from enum import Enum
from pyFTS.common import FuzzySet
class DateTime(Enum):
year = 1
month = 2
day_of_month = 3
day_of_year = 4
day_of_week = 5
hour = 6
minute = 7
second = 8
def strip_datepart(self, date, date_part):
if date_part == DateTime.year:
tmp = date.year
elif date_part == DateTime.month:
tmp = date.month
elif date_part == DateTime.day_of_year:
tmp = date.timetuple().tm_yday
elif date_part == DateTime.day_of_month:
tmp = date.day
elif date_part == DateTime.day_of_week:
tmp = date.weekday()
elif date_part == DateTime.hour:
tmp = date.hour
elif date_part == DateTime.minute:
tmp = date.minute
elif date_part == DateTime.second:
tmp = date.second
return tmp
class FuzzySet(FuzzySet.FuzzySet):
"""
Temporal/Seasonal Fuzzy Set
"""
def __init__(self, datepart, name, mf, parameters, centroid):
super(FuzzySet, self).__init__(name, mf, parameters, centroid)
self.datepart = datepart
def membership(self, x):
dp = strip_datepart(x, self.datepart)
return self.mf.membership(dp)