InfoClusters
Information at sight.

Fuzzy Logic in Machine Learning with Python Code

Reading time: 4 min

Fuzzy Logic: Fuzzy Logic is the logic underlying approximate rather than exact. It is an extension of multivalued logic: Everything, including truth, is a matter of degree. It contains as special cases not only the classical two-value logic and multivalue logic system but also probabilistic logic.

A proportion has a truth value

  • 0 or 1 in the two-value system.
  • element of set T in the multivalue system.
  • The range over the fuzzy subset of T in fuzzy logic

Boolean Logic V/S Fuzzy Logic

Boolean logic is basically the combination of 0 or and Boolean logic also uses sharp distinct whereas Fuzzy logic reflects how people think. Fuzzy logic is a set of mathematical principle for knowledge representation and reasoning based on the degree of membership.

boolean vs fuzzy

As from image above it can be seen that Boolean Logic can only be True or False whereas Fuzzy Logic can be between the values of True or False.

Need for Fuzzy Logic

  • Fuzzy Logic is based on intuition and judgment.
  • No need for a mathematical model.
  • It provides a smooth transition between members and nonmembers.
  • Relatively simple, fast and adaptive.
  • Less sensitive to system fluctuations.
  • Can implement design objectives, difficult to express mathematically, in linguistic or descriptive rules.

Crisp Set

Conventional or crisp sets are Binary. An element either belongs to the set or does not.

{True / False}

{1,0}

 

In Fuzzy ML code we will change the Crisp value to linguistic words by Fuizzification and then output changed into Crisp value for output by Defuzzification.

Example of Fuzzification:

Assume we want to evaluate the health of a person based on his height and weight. The input variables are the crisp values of a person’s height and weight. Fuzzification is the process by which the numbers are changed into linguistic words.

fuzzification of weight

height in linguistic

DEFUZZIFICATION

Defuzzification id a mapping process from a space of fuzzy control actions defined over an output universe of discourse into a space of crisp control actions. It is a process of converting the output fuzzy variable into a unique number.

It has the capability to reduce a fuzzy set into a crisp single-values quantity or into a crisp set; to convert a fuzzy matrix into a crisp matrix, or to convert a fuzzy member into crisp member.

Methods of Defuzzification:

  • Max-membership principle.
  • Centroid method.
  • Weighted average method.
  • Mean-max membership.
  • Center of sums.
  • Center of largest area.
  • First of maxima, last of maxima.

Fuzzy Logic Code to Calculate CGPA based on marks of Continuos Assesment, Mid-Term exams and End-Term exams.

!pip install scikit-fuzzy #install fuzzy in your system

#Antecedent is used for input parameters and Consequent is used for output. Here ca,mte,ete are input and cgpa is output.
ca=ctrl.Antecedent(np.arange(0,26,1),'ca') #ca marks range 0-25
mte=ctrl.Antecedent(np.arange(0,26,1),'mte') #mte marks range 0-25
ete=ctrl.Antecedent(np.arange(0,51,1),'ete') #ete marks range 0-50
cgpa=ctrl.Consequent(np.arange(0,11,1),'cgpa') #cgpa between 0-10

#now we consider bad ca when marks are between 0-10, average when marks between 9-20 and good when marks between 18-25.

ca['bad']=fuzzy.trimf(ca.universe,[0,5,10]) 
ca['avg']=fuzzy.trimf(ca.universe,[9,15,20])
ca['good']=fuzzy.trimf(ca.universe,[18,23,25])

#now we consider bad mte when marks are between 0-10, average when marks between 9-20 and good when marks between 18-25.

mte['bad']=fuzzy.trimf(mte.universe,[0,5,10])
mte['avg']=fuzzy.trimf(mte.universe,[8,15,20])
mte['good']=fuzzy.trimf(mte.universe,[18,23,25])

#now we consider bad ete when marks are between 0-10, average when marks between 18-35 and good when marks between 33-50.

ete['bad']=fuzzy.trimf(ete.universe,[0,10,20])
ete['avg']=fuzzy.trimf(ete.universe,[18,25,35])
ete['good']=fuzzy.trimf(ete.universe,[33,40,50])

#now we consider bad cgpa when marks are between 0-5, average when marks between 4-7.5 and good when marks between 7-10.

cgpa['bad']=fuzzy.trimf(cgpa.universe,[0,3,5])
cgpa['avg']=fuzzy.trimf(cgpa.universe,[4,6,7.5])
cgpa['good']=fuzzy.trimf(cgpa.universe,[7,9,10])
ca.view()
mte.view()
ete.view()
cgpa.view()
#now we will decide rules based on creteria of ca, mte and ete.

rule1=ctrl.Rule(ca['bad'] & mte['bad'] & ete['bad'],cgpa['bad'])
rule2=ctrl.Rule(ca['avg'] & mte['avg'] & ete['avg'],cgpa['avg'])
rule3=ctrl.Rule(ca['good'] & mte['good'] & ete['good'],cgpa['good'])
rule4=ctrl.Rule(ca['bad'] & mte['good'] & ete['good'],cgpa['good'])
rule5=ctrl.Rule(ca['good'] & mte['bad'] & ete['good'],cgpa['avg'])
rule6=ctrl.Rule(ca['good'] & mte['good'] & ete['bad'],cgpa['bad'])
rule7=ctrl.Rule(ca['bad'] & mte['bad'] & ete['good'],cgpa['bad'])
rule8=ctrl.Rule(ca['good'] & mte['avg'] & ete['good'],cgpa['good'])
rule9=ctrl.Rule(ca['avg'] & mte['good'] & ete['good'],cgpa['good'])
rule10=ctrl.Rule(ca['good'] & mte['bad'] & ete['bad'],cgpa['bad'])
#pass the value to ControlSystem and Simulate before calculating actual output.

cg_calc=ctrl.ControlSystem([rule1,rule2,rule3,rule4,rule5,rule6,rule7,rule8,rule9,rule10])
cgpaa=ctrl.ControlSystemSimulation(cg_calc)

#Now pass input as 
cgpaa.input['ca']=18
cgpaa.input['mte']=22
cgpaa.input['ete']=41

cgpaa.compute() #calculate cgpa

print(cgpaa.output['cgpa']) #print calculated cgpa
cgpa.view(sim=cgpaa) #visualize output

 

Get real time updates directly on you device, subscribe now.

%d bloggers like this: