From ccfbd20d72a03bd777cbea5c13d55e70ff62a8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=C3=B4nio=20C=C3=A2ndido=20de=20Lima=20e=20Silva?= Date: Thu, 22 Dec 2016 14:15:07 -0200 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20e=20cria=C3=A7=C3=A3o?= =?UTF-8?q?=20da=20package=20common?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.py | 97 --------------------------------------- common/FLR.py | 21 +++++++++ common/FuzzySet.py | 35 ++++++++++++++ common/Membership.py | 40 ++++++++++++++++ common/Transformations.py | 8 ++++ common/__init__.py | 0 6 files changed, 104 insertions(+), 97 deletions(-) delete mode 100644 common.py create mode 100644 common/FLR.py create mode 100644 common/FuzzySet.py create mode 100644 common/Membership.py create mode 100644 common/Transformations.py create mode 100644 common/__init__.py diff --git a/common.py b/common.py deleted file mode 100644 index edb78d3..0000000 --- a/common.py +++ /dev/null @@ -1,97 +0,0 @@ -import numpy as np -from pyFTS import * - -def differential(original): - n = len(original) - diff = [ original[t-1]-original[t] for t in np.arange(1,n) ] - diff.insert(0,0) - return np.array(diff) - -def trimf(x,parameters): - xx = round(x,3) - if(xx < parameters[0]): - return 0 - elif(xx >= parameters[0] and xx < parameters[1]): - return (x-parameters[0])/(parameters[1]-parameters[0]) - elif(xx >= parameters[1] and xx <= parameters[2]): - return (parameters[2]-xx)/(parameters[2]-parameters[1]) - else: - return 0 - -def trapmf(x, parameters): - if(x < parameters[0]): - return 0 - elif(x >= parameters[0] and x < parameters[1]): - return (x-parameters[0])/(parameters[1]-parameters[0]) - elif(x >= parameters[1] and x <= parameters[2]): - return 1 - elif(x >= parameters[2] and x <= parameters[3]): - return (parameters[3]-x)/(parameters[3]-parameters[2]) - else: - return 0 - -def gaussmf(x,parameters): - return math.exp(-0.5*((x-parameters[0]) / parameters[1] )**2) - - -def bellmf(x,parameters): - return 1 / (1 + abs((xx - parameters[2])/parameters[0])**(2*parameters[1])) - - -def sigmf(x,parameters): - return 1 / (1 + math.exp(-parameters[0] * (x - parameters[1]))) - - -class FuzzySet: - def __init__(self,name,mf,parameters,centroid): - self.name = name - self.mf = mf - self.parameters = parameters - self.centroid = centroid - self.lower = min(parameters) - self.upper = max(parameters) - - def membership(self,x): - return self.mf(x,self.parameters) - - def __str__(self): - return self.name + ": " + str(self.mf) + "(" + str(self.parameters) + ")" - -class FLR: - def __init__(self,LHS,RHS): - self.LHS = LHS - self.RHS = RHS - - def __str__(self): - return str(self.LHS) + " -> " + str(self.RHS) - -def fuzzyInstance(inst, fuzzySets): - mv = np.array([ fs.membership(inst) for fs in fuzzySets]) - return mv - - -def getMaxMembershipFuzzySet(inst, fuzzySets): - mv = fuzzyInstance(inst,fuzzySets) - return fuzzySets[ np.argwhere(mv == max(mv) )[0,0] ] - - -def fuzzySeries(data,fuzzySets): - fts = [] - for item in data: - fts.append(getMaxMembershipFuzzySet(item,fuzzySets)) - return fts - - -def generateNonRecurrentFLRs(fuzzyData): - flrs = {} - for i in range(2,len(fuzzyData)): - tmp = FLR(fuzzyData[i-1],fuzzyData[i]) - flrs[str(tmp)] = tmp - ret = [value for key, value in flrs.items()] - return ret - -def generateRecurrentFLRs(fuzzyData): - flrs = [] - for i in np.arange(1,len(fuzzyData)): - flrs.append(FLR(fuzzyData[i-1],fuzzyData[i])) - return flrs diff --git a/common/FLR.py b/common/FLR.py new file mode 100644 index 0000000..caacb86 --- /dev/null +++ b/common/FLR.py @@ -0,0 +1,21 @@ +class FLR: + def __init__(self, LHS, RHS): + self.LHS = LHS + self.RHS = RHS + + def __str__(self): + return str(self.LHS) + " -> " + str(self.RHS) + +def generateNonRecurrentFLRs(fuzzyData): + flrs = {} + for i in range(2,len(fuzzyData)): + tmp = FLR(fuzzyData[i-1],fuzzyData[i]) + flrs[str(tmp)] = tmp + ret = [value for key, value in flrs.items()] + return ret + +def generateRecurrentFLRs(fuzzyData): + flrs = [] + for i in np.arange(1,len(fuzzyData)): + flrs.append(FLR(fuzzyData[i-1],fuzzyData[i])) + return flrs diff --git a/common/FuzzySet.py b/common/FuzzySet.py new file mode 100644 index 0000000..13995c1 --- /dev/null +++ b/common/FuzzySet.py @@ -0,0 +1,35 @@ +import numpy as np +from pyFTS import * + + +class FuzzySet: + def __init__(self, name, mf, parameters, centroid): + self.name = name + self.mf = mf + self.parameters = parameters + self.centroid = centroid + self.lower = min(parameters) + self.upper = max(parameters) + + def membership(self, x): + return self.mf(x, self.parameters) + + def __str__(self): + return self.name + ": " + str(self.mf) + "(" + str(self.parameters) + ")" + + +def fuzzyInstance(inst, fuzzySets): + mv = np.array([fs.membership(inst) for fs in fuzzySets]) + return mv + + +def getMaxMembershipFuzzySet(inst, fuzzySets): + mv = fuzzyInstance(inst, fuzzySets) + return fuzzySets[np.argwhere(mv == max(mv))[0, 0]] + + +def fuzzySeries(data, fuzzySets): + fts = [] + for item in data: + fts.append(getMaxMembershipFuzzySet(item, fuzzySets)) + return fts \ No newline at end of file diff --git a/common/Membership.py b/common/Membership.py new file mode 100644 index 0000000..0a598d0 --- /dev/null +++ b/common/Membership.py @@ -0,0 +1,40 @@ +import numpy as np +import math +from pyFTS import * + + +def trimf(x, parameters): + xx = round(x, 3) + if (xx < parameters[0]): + return 0 + elif (xx >= parameters[0] and xx < parameters[1]): + return (x - parameters[0]) / (parameters[1] - parameters[0]) + elif (xx >= parameters[1] and xx <= parameters[2]): + return (parameters[2] - xx) / (parameters[2] - parameters[1]) + else: + return 0 + + +def trapmf(x, parameters): + if (x < parameters[0]): + return 0 + elif (x >= parameters[0] and x < parameters[1]): + return (x - parameters[0]) / (parameters[1] - parameters[0]) + elif (x >= parameters[1] and x <= parameters[2]): + return 1 + elif (x >= parameters[2] and x <= parameters[3]): + return (parameters[3] - x) / (parameters[3] - parameters[2]) + else: + return 0 + + +def gaussmf(x, parameters): + return math.exp(-0.5 * ((x - parameters[0]) / parameters[1]) ** 2) + + +def bellmf(x, parameters): + return 1 / (1 + abs((x - parameters[2]) / parameters[0]) ** (2 * parameters[1])) + + +def sigmf(x, parameters): + return 1 / (1 + math.exp(-parameters[0] * (x - parameters[1]))) diff --git a/common/Transformations.py b/common/Transformations.py new file mode 100644 index 0000000..26447a4 --- /dev/null +++ b/common/Transformations.py @@ -0,0 +1,8 @@ +import numpy as np +from pyFTS import * + +def differential(original): + n = len(original) + diff = [ original[t-1]-original[t] for t in np.arange(1,n) ] + diff.insert(0,0) + return np.array(diff) \ No newline at end of file diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 0000000..e69de29