2016-12-22 20:15:07 +04:00
|
|
|
import numpy as np
|
|
|
|
from pyFTS import *
|
2016-12-26 21:47:23 +04:00
|
|
|
from pyFTS.common import Membership
|
2016-12-22 20:15:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
class FuzzySet:
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Fuzzy Set
|
|
|
|
"""
|
2016-12-22 20:15:07 +04:00
|
|
|
def __init__(self, name, mf, parameters, centroid):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Create a Fuzzy Set
|
|
|
|
:param name: fuzzy set name
|
|
|
|
:param mf: membership function
|
|
|
|
:param parameters: parameters of the membership function
|
|
|
|
:param centroid: fuzzy set center of mass
|
|
|
|
"""
|
2016-12-22 20:15:07 +04:00
|
|
|
self.name = name
|
|
|
|
self.mf = mf
|
|
|
|
self.parameters = parameters
|
|
|
|
self.centroid = centroid
|
2016-12-26 21:47:23 +04:00
|
|
|
if self.mf == Membership.trimf:
|
|
|
|
self.lower = min(parameters)
|
|
|
|
self.upper = max(parameters)
|
|
|
|
elif self.mf == Membership.gaussmf:
|
|
|
|
self.lower = parameters[0] - parameters[1]*3
|
|
|
|
self.upper = parameters[0] + parameters[1]*3
|
2016-12-22 20:15:07 +04:00
|
|
|
|
|
|
|
def membership(self, x):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Calculate the membership value of a given input
|
|
|
|
:param x: input value
|
|
|
|
:return: membership value of x at this fuzzy set
|
|
|
|
"""
|
2016-12-22 20:15:07 +04:00
|
|
|
return self.mf(x, self.parameters)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-11 00:05:51 +04:00
|
|
|
return self.name + ": " + str(self.mf.__name__) + "(" + str(self.parameters) + ")"
|
2016-12-22 20:15:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
def fuzzyInstance(inst, fuzzySets):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Calculate the membership values for a data point given fuzzy sets
|
|
|
|
:param inst: data point
|
|
|
|
:param fuzzySets: list of fuzzy sets
|
|
|
|
:return: array of membership values
|
|
|
|
"""
|
2016-12-22 20:15:07 +04:00
|
|
|
mv = np.array([fs.membership(inst) for fs in fuzzySets])
|
|
|
|
return mv
|
|
|
|
|
|
|
|
|
2017-02-16 17:54:37 +04:00
|
|
|
def fuzzyInstances(data, fuzzySets):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Calculate the membership values for a data point given fuzzy sets
|
|
|
|
:param inst: data point
|
|
|
|
:param fuzzySets: list of fuzzy sets
|
|
|
|
:return: array of membership values
|
|
|
|
"""
|
2017-02-16 17:54:37 +04:00
|
|
|
ret = []
|
|
|
|
for inst in data:
|
|
|
|
mv = np.array([fs.membership(inst) for fs in fuzzySets])
|
|
|
|
ret.append(mv)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2016-12-22 20:15:07 +04:00
|
|
|
def getMaxMembershipFuzzySet(inst, fuzzySets):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Fuzzify a data point, returning the fuzzy set with maximum membership value
|
|
|
|
:param inst: data point
|
|
|
|
:param fuzzySets: list of fuzzy sets
|
|
|
|
:return: fuzzy set with maximum membership
|
|
|
|
"""
|
2016-12-22 20:15:07 +04:00
|
|
|
mv = fuzzyInstance(inst, fuzzySets)
|
|
|
|
return fuzzySets[np.argwhere(mv == max(mv))[0, 0]]
|
|
|
|
|
2017-02-15 05:01:44 +04:00
|
|
|
def getMaxMembershipFuzzySetIndex(inst, fuzzySets):
|
2017-05-02 18:32:03 +04:00
|
|
|
"""
|
|
|
|
Fuzzify a data point, returning the fuzzy set with maximum membership value
|
|
|
|
:param inst: data point
|
|
|
|
:param fuzzySets: list of fuzzy sets
|
|
|
|
:return: fuzzy set with maximum membership
|
|
|
|
"""
|
2017-02-15 05:01:44 +04:00
|
|
|
mv = fuzzyInstance(inst, fuzzySets)
|
|
|
|
return np.argwhere(mv == max(mv))[0, 0]
|
|
|
|
|
2016-12-22 20:15:07 +04:00
|
|
|
|
|
|
|
def fuzzySeries(data, fuzzySets):
|
|
|
|
fts = []
|
|
|
|
for item in data:
|
|
|
|
fts.append(getMaxMembershipFuzzySet(item, fuzzySets))
|
2016-12-26 17:21:28 +04:00
|
|
|
return fts
|