1. Objectives
- Explain the difference between a crisp (ordinary) set and a fuzzy set.
- Build triangular, trapezoidal and Gaussian membership functions with scikit-fuzzy and plot them.
- Find the membership degree of a reading with fuzz.interp_membership and give it a linguistic label.
- Write your own (custom) membership function in NumPy and check it against scikit-fuzzy.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Crisp set | An ordinary set: a value is either in it (1) or not (0) | 'Hot' = 35 °C or more: 34.9 °C is 'not hot' |
Fuzzy set | A set where a value belongs with a degree between 0 and 1 | 34 °C is hot to degree 0.6 |
Membership function (MF) | A curve that gives the degree for every value | Triangle for 'comfortable' |
Universe of discourse | The full range of values we talk about | Temperature 0 to 50 °C |
Linguistic variable / term | A variable described in words, and its word values | Temperature: cold, comfortable, hot |
Support | The values where the degree is above 0 | 'Comfortable' triangle: 15 to 33 °C |
Crossover point | Where two neighbouring sets have equal degree | Cold = comfortable at about 17.4 °C |
3. Background
Why fuzzy?
People say 'it is quite hot today' or 'the bill is a bit high' - they do not use sharp borders. A crisp rule 'hot means 35 °C or more' says 34.9 °C is not hot at all, which is not how we think. Lotfi Zadeh (1965) proposed fuzzy sets: every value belongs to a set with a DEGREE between 0 (not at all) and 1 (fully). Fuzzy logic runs inverter ACs, washing machines, rice cookers and metro trains.
Membership functions
A membership function (MF) gives the degree for every value in the universe. The common shapes are the triangle (trimf: a, b, c), the trapezoid (trapmf: a, b, c, d - flat top from b to c) and the Gaussian bell (gaussmf: mean, sigma). Shoulders at the ends of the universe are trapezoids such as [0, 0, 10, 20].
One value, many sets
A reading usually belongs to two neighbouring sets at the same time: 30 °C may be 0.33 comfortable and 0.2 hot. That overlap is what makes fuzzy controllers smooth (Lecture 22 and Practicals 08-10 build on it).
Data files for this lab
No data file is needed - the programs make their own data.
4. Time Plan (50 minutes)
Minutes | Activity | Part |
|---|---|---|
0-5 | Background: crisp vs fuzzy sets, membership functions | 1-3 |
5-10 | Guided warm-up: crisp vs fuzzy 'hot'; three temperature sets | Part A |
10-13 | Predict the output | Part B |
13-17 | Bug hunt | Part C |
17-47 | Graded tasks 1-3 | Part D |
47-50 | Wrap-up; post-lab questions set as homework | Part E |
Part A - Guided Warm-up (type, run, observe)
A1. Crisp 'hot' vs fuzzy 'hot'
The crisp set jumps from 0 to 1 at 35 °C. The fuzzy set rises smoothly from 30 °C to 40 °C.
import numpy as np import skfuzzy as fuzz x = np.arange(0, 51, 1) hot = fuzz.trapmf(x, [30, 40, 50, 50]) # fuzzy: rises from 30 to 40, then fully hot print("temp crisp hot fuzzy hot") for t in [25, 30, 33, 34.9, 35, 38, 42]: crisp = 1 if t >= 35 else 0 print(f"{t:>4} {crisp:>9} {fuzz.interp_membership(x, hot, t):>9.2f}") |
Output temp crisp hot fuzzy hot 25 0 0.00 30 0 0.00 33 0 0.30 34.9 0 0.49 35 1 0.50 38 1 0.80 42 1 1.00 |
A2. Three fuzzy sets for temperature
Cold is a left shoulder, comfortable a triangle and hot a right shoulder. A reading of 30 °C belongs to two sets.
import numpy as np import skfuzzy as fuzz x = np.arange(0, 51, 1) # universe: 0 to 50 degrees C cold = fuzz.trapmf(x, [0, 0, 10, 20]) comfortable = fuzz.trimf(x, [15, 24, 33]) hot = fuzz.trapmf(x, [28, 38, 50, 50]) import matplotlib.pyplot as plt plt.figure(figsize=(7, 2.6)) for mf, name in [(cold, "cold"), (comfortable, "comfortable"), (hot, "hot")]: plt.plot(x, mf, lw=2, label=name) plt.axvline(30, color="grey", ls=":"); plt.xlabel("Temperature (°C)"); plt.ylabel("Degree") plt.title("Temperature: three fuzzy sets"); plt.legend(); plt.grid(alpha=0.3) plt.tight_layout(); plt.savefig("temperature_mfs.png", dpi=100) t = 30 for mf, name in [(cold, "cold"), (comfortable, "comfortable"), (hot, "hot")]: print(f"{t} C is {name:<11} to degree {fuzz.interp_membership(x, mf, t):.2f}") print("Saved temperature_mfs.png") |
Output 30 C is cold to degree 0.00 30 C is comfortable to degree 0.33 30 C is hot to degree 0.20 Saved temperature_mfs.png |
Part B - Predict the Output
Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.
B1.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 21, 1) mf = fuzz.trimf(x, [0, 10, 20]) print(mf[5], mf[10], mf[15]) |
My prediction |
B2.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 41, 1) mf = fuzz.trapmf(x, [10, 20, 30, 40]) print(mf[25], mf[35]) |
My prediction |
B3.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) mf = fuzz.gaussmf(x, 24, 4) print(round(mf[24], 2), round(mf[28], 2), round(mf[20], 2)) |
My prediction |
Part C - Bug Hunt
Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.
C1.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) warm = fuzz.trimf(x, [30, 20, 40]) print(warm.max()) |
Bug, why it happens, and the fixed line(s) |
C2.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 51, 1) warm = fuzz.trimf(x, 20, 30, 40) print(warm.max()) |
Bug, why it happens, and the fixed line(s) |
C3.
import numpy as np, skfuzzy as fuzz x = np.arange(0, 41, 1) # universe 0-40 C hot = fuzz.trapmf(x, [28, 38, 40, 40]) reading = 45 # a June afternoon in Burewala print("hot to degree", fuzz.interp_membership(x, hot, reading)) |
It runs and prints hot to degree 0.0 |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | Fuzzy electricity use | 3 |
T2 | Medium | Triangle or bell? And where do two sets cross? | 3 |
T3 | Challenge | Write your own membership functions | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): Fuzzy electricity use
Daily electricity use (units per day, universe 0-40, step 0.1) has three fuzzy sets: low = trapmf [0, 0, 5, 12], medium = trimf [8, 15, 22], high = trapmf [18, 28, 40, 40]. For the readings 6, 10, 15, 20 and 30 units print the degree in each set and the label with the highest degree.
Sample run (your output must match):
Output units low medium high label 6 0.86 0.00 0.00 low 10 0.29 0.29 0.00 low 15 0.00 1.00 0.00 medium 20 0.00 0.29 0.20 medium 30 0.00 0.00 1.00 high |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): Triangle or bell? And where do two sets cross?
On the universe 0-50 °C with step 0.01: (a) plot 'comfortable' as trimf [15, 24, 33] and as gaussmf(mean 24, sigma 4) on one graph (save task2.png); (b) print both degrees at 18, 21, 24, 27 and 30 °C; (c) find the crossover point where cold = trapmf [0, 0, 10, 20] and the triangular 'comfortable' have equal degree (search only between 15 and 20 °C).
Sample run (your output must match):
Output 18 C triangle 0.33 Gaussian 0.32 21 C triangle 0.67 Gaussian 0.75 24 C triangle 1.00 Gaussian 1.00 27 C triangle 0.67 Gaussian 0.75 30 C triangle 0.33 Gaussian 0.32 crossover at 17.37 C, degree 0.26 Saved task2.png |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): Write your own membership functions
Write custom functions trap(x, a, b, c, d) and tri(x, a, b, c) using only NumPy (hint: rising edge (x - a) / (b - a), falling edge (d - x) / (d - c), take the minimum and clip to 0-1; treat a == b or c == d as a shoulder). Also write sig(x, c, k) = 1 / (1 + e^(-k(x - c))) for 'very hot'. Check each against fuzz.trapmf, fuzz.trimf and fuzz.sigmf on the universe 0-50 (step 0.5) and print the largest difference. Then print the degree of 37 °C in your sig(x, 35, 0.5).
Sample run (your output must match):
Output trap cold largest difference 0.0e+00 same: True trap hot largest difference 0.0e+00 same: True tri comfortable largest difference 0.0e+00 same: True sig very hot largest difference 0.0e+00 same: True 37 C is 'very hot' to degree 0.731 |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. Differentiate a crisp set and a fuzzy set with the example of 'hot'.
Your answer |
E2. Write the points of a triangular MF for 'comfortable' that peaks at 25 °C and is zero below 18 °C and above 32 °C. What is the degree of 21.5 °C?
Your answer |
E3. Why must the universe of discourse cover every possible reading?
Your answer |
Marking Rubric (applied to Part D)
Criterion | Full | Partial | None |
|---|---|---|---|
Code runs without errors | Runs fully | Small fixable error | Does not run |
Correct membership functions and universes | All correct | One slip | Several wrong |
Degrees, plot and crossover / custom-function checks | All present | Some missing | None |
Explanations (labels, shapes, custom functions) | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P07a with task1.py, task2.py, task3.py (or P07a.ipynb) and task2.png.
- Upload the zipped folder to the LMS / Google Classroom by the end of the day.
- Copy Task 1 and Task 3 code and output into your Lab Book (Practical 07a page).
- Install the library once with: pip install scikit-fuzzy