Fuzzy Logic in Machine Learning with Python Code

fuzzy logic in machine learning with code example
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}



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 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.


#now we consider bad mte when marks are between 0-10, average when marks between 9-20 and good when marks between 18-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.


#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.

#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.


#Now pass input as 

cgpaa.compute() #calculate cgpa

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



Leave a Reply

Your email address will not be published. Required fields are marked *


world suicide prevention day,

World Suicide Prevention Day

international day of democracy,

15th Sep – International Day of Democracy