60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from pyFTS import *
|
|
|
|
class ImprovedWeightedFLRG:
|
|
def __init__(self,premiss):
|
|
self.premiss = premiss
|
|
self.consequent = {}
|
|
self.count = 0.0
|
|
|
|
def append(self,c):
|
|
if c not in self.consequent:
|
|
self.consequent[c] = 1.0
|
|
else:
|
|
self.consequent[c] = self.consequent[c] + 1.0
|
|
self.count = self.count + 1.0
|
|
|
|
def weights(self):
|
|
return np.array([ self.consequent[c]/self.count for c in self.consequent.keys() ])
|
|
|
|
def __str__(self):
|
|
tmp = self.premiss + " -> "
|
|
tmp2 = ""
|
|
for c in self.consequent.keys():
|
|
if len(tmp2) > 0:
|
|
tmp2 = tmp2 + ","
|
|
tmp2 = tmp2 + c + "(" + str(round(self.consequent[c]/self.count,3)) + ")"
|
|
return tmp + tmp2
|
|
|
|
|
|
class ImprovedWeightedFTS(fts.FTS):
|
|
def __init__(self,name):
|
|
super(ImprovedWeightedFTS, self).__init__(1,name)
|
|
|
|
def defuzzy(self,data):
|
|
actual = self.fuzzy(data)
|
|
if actual["fuzzyset"] not in self.flrgs:
|
|
return self.sets[actual["fuzzyset"]].centroid
|
|
flrg = self.flrgs[actual["fuzzyset"]]
|
|
mi = np.array([self.sets[s].centroid for s in flrg.consequent.keys()])
|
|
return mi.dot( flrg.weights() )
|
|
|
|
def learn(self, data, sets):
|
|
last = {"fuzzyset":"", "membership":0.0}
|
|
actual = {"fuzzyset":"", "membership":0.0}
|
|
|
|
for s in sets:
|
|
self.sets[s.name] = s
|
|
|
|
self.flrgs = {}
|
|
count = 1
|
|
for inst in data:
|
|
actual = self.fuzzy(inst)
|
|
|
|
if count > self.order:
|
|
if last["fuzzyset"] not in self.flrgs:
|
|
self.flrgs[last["fuzzyset"]] = ImprovedWeightedFLRG(last["fuzzyset"])
|
|
|
|
self.flrgs[last["fuzzyset"]].append(actual["fuzzyset"])
|
|
count = count + 1
|
|
last = actual
|