In [5]:
pip install scikit-fuzzy
In [39]:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# New Antecedent/Consequent objects hold universe variables and membership
# functions
quality = ctrl.Antecedent(np.arange(0, 11, 1), 'quality')
service = ctrl.Antecedent(np.arange(0, 11, 1), 'service')
tip = ctrl.Consequent(np.arange(0, 26, 1), 'tip')
# Auto-membership function population is possible with .automf(3, 5, or 7)
quality.automf(3)
service.automf(3)
# Custom membership functions can be built interactively with a familiar,
# Pythonic API
tip['low'] = fuzz.trimf(tip.universe, [0, 0, 13])
tip['medium'] = fuzz.trimf(tip.universe, [0, 13, 25])
tip['high'] = fuzz.trimf(tip.universe, [13, 25, 25])
In [40]:
# You can see how these look with .view()
quality['average'].view()
In [9]:
service.view()
In [10]:
tip.view()
In [11]:
rule1 = ctrl.Rule(quality['poor'] | service['poor'], tip['low'])
rule2 = ctrl.Rule(service['average'], tip['medium'])
rule3 = ctrl.Rule(service['good'] | quality['good'], tip['high'])
rule1.view()
Out[11]:
In [19]:
from math import log2
def distance_r(t_N, t_M):
return log2(abs(t_N - t_M))
In [20]:
print(distance_r(10,5))
In [106]:
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
# New Antecedent/Consequent objects hold universe variables and membership
# functions
p = ctrl.Antecedent(np.arange(0, 5, 1), 'p')
q = ctrl.Antecedent(np.linspace(0, 1, 6000), 'q')
#tip = ctrl.Consequent(np.arange(0, 26, 1), 'tip')
# Custom membership functions can be built interactively with a familiar,
# Pythonic API
p['P1'] = fuzz.trimf(p.universe, [0, 0, 1])
p['P2'] = fuzz.trimf(p.universe, [0, 1, 2])
p['P3'] = fuzz.trimf(p.universe, [1, 2, 3])
p['P4'] = fuzz.trimf(p.universe, [2, 3, 4])
p['P5'] = fuzz.trimf(p.universe, [3, 4, 4])
q['Q1'] = fuzz.trapmf(q.universe, [0, 0, .1, .2])
q['Q2'] = fuzz.trimf(q.universe, [.1, .2, .3])
q['Q3'] = fuzz.trimf(q.universe, [.2, .4, .6])
q['Q4'] = fuzz.trimf(q.universe, [.3, .6, .9])
q['Q5'] = fuzz.trimf(q.universe, [.7, .8, .9])
q['Q6'] = fuzz.trapmf(q.universe, [.8, .9, 1, 1])
In [107]:
p.view()
q.view()