Nexus Learn

Practical 11A: ANFIS I: Learning the Rule Formulas by Least Squares

Lab Practical CS-511 Artificial Intelligence 9 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A - Guided Warm-up (type, run, observe)
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (10 marks)
  9. Part E - Post-Lab Questions (finish as homework if time runs out)
  10. Marking Rubric (applied to Part D)
  11. Submission

1. Objectives

  1. Describe the five layers of an ANFIS (Adaptive Neuro-Fuzzy Inference System) model.
  2. Compute normalised firing strengths with generalised-bell membership functions and the product AND.
  3. Learn the 12 first-order Sugeno rule formulas from field data with NumPy least squares.
  4. Compare ANFIS with a single linear formula on unseen test data.

2. Word Bank

Word

Simple meaning

Real-world example

ANFIS

Adaptive Neuro-Fuzzy Inference System: a Sugeno fuzzy system whose numbers are learned from data like a neural network

Learns irrigation rules from farm records

Consequent parameters

The numbers p, q, r in a first-order rule output z = p·m + q·t + r

z = 1.2·m - 0.4·t + 30

Premise parameters

The membership function numbers a, b, c (the IF part)

dry = gbell(25, 2, 10)

Normalised strength

A rule's strength divided by the sum of all strengths (row adds to 1)

0.1, 0.3, 0.05, 0.05 -> 0.2, 0.6, 0.1, 0.1

Least squares

Choosing parameters that make the sum of squared errors as small as possible

np.linalg.lstsq

Design matrix

A table with one row per reading and one column per parameter

70 x 12

3. Background

The problem

A Burewala farmer runs a tubewell (irrigation well pump) each evening. How many minutes? Dry soil and a hot day need more water. irrigation.csv holds 100 past readings: soil moisture (%), temperature (°C) and the minutes the farmer actually ran the pump. Instead of asking an expert for rules (P08), we LEARN the rules from these records.

ANFIS in five layers

Jyh-Shing Roger Jang (1993) wrote a first-order Sugeno system as a network: (1) membership degrees with generalised bells (P10b); (2) firing strength w = product of the degrees; (3) normalise: w̄ = w / Σw; (4) each rule's output w̄k·(pk·m + qk·t + rk); (5) sum of the rule outputs. Four rules: IF moisture is dry/wet AND temperature is mild/hot.

Why least squares works

Once the membership functions are fixed, the output is a LINEAR sum of the 12 consequent numbers. So the best 12 numbers come from one least-squares solve - no training loop needed. This is the forward pass of Jang's HYBRID learning; P11b adds the backward pass that tunes the membership functions.

Data files for this lab

irrigation.csv

moisture,temperature,minutes

16.4,19.3,77.1

35.0,28.3,26.2

40.1,38.6,11.5

11.4,41.8,111.6

17.4,37.8,101.9

56.4,16.1,7.2

13.5,25.8,90.7

16.5,19.9,82.9

57.4,45.0,11.0

41.1,19.3,13.2

... (100 data rows in total - copy the full file from the lab folder)

4. Time Plan (50 minutes)

Minutes

Activity

Part

0-5

Background: ANFIS layers and least squares

1-3

5-10

Guided warm-up: the data; layers 1-3 for one reading

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. Look at the data

Load irrigation.csv, split it into 70 learning and 30 test readings, and plot minutes against soil moisture.

import numpy as np

import pandas as pd

df = pd.read_csv("irrigation.csv")

X = df[["moisture", "temperature"]].to_numpy()

y = df["minutes"].to_numpy()

X_tr, y_tr, X_te, y_te = X[:70], y[:70], X[70:], y[70:] # 70 readings to learn, 30 to test

rmse = lambda pred, true: float(np.sqrt(np.mean((pred - true) ** 2)))

import matplotlib.pyplot as plt

print(df.shape)

print(df.head(3).to_string())

print(f"minutes: min {y.min()}, max {y.max()}")

plt.figure(figsize=(7, 2.6))

sc = plt.scatter(df.moisture, df.minutes, c=df.temperature, cmap="autumn_r", s=18)

plt.colorbar(sc, label="Temperature (°C)")

plt.xlabel("Soil moisture (%)"); plt.ylabel("Irrigation (minutes)"); plt.grid(alpha=0.3)

plt.tight_layout(); plt.savefig("data.png", dpi=90)

print("Saved data.png")

Output

(100, 3)

moisture temperature minutes

0 16.4 19.3 77.1

1 35.0 28.3 26.2

2 40.1 38.6 11.5

minutes: min 5.7, max 111.6

Saved data.png

A2. Layers 1-3 for one reading

Moisture 20 %, temperature 40 °C: the degree of each term, the four raw strengths (product) and the normalised strengths.

# ... set-up: load the irrigation data (as shown earlier) ...

def gbell(x, a, b, c):

return 1 / (1 + np.abs((x - c) / a) ** (2 * b))

MF = {"moisture": {"dry": [25, 2, 10], "wet": [25, 2, 60]}, # [a, b, c] for each term

"temperature": {"mild": [15, 2, 15], "hot": [15, 2, 45]}}

RULES = [("dry", "mild"), ("dry", "hot"), ("wet", "mild"), ("wet", "hot")]

def strengths(X, MF):

"""Layers 1-3: degrees, product AND, normalise so each row adds to 1."""

w = np.array([gbell(X[:, 0], *MF["moisture"][m]) * gbell(X[:, 1], *MF["temperature"][t]) for m, t in RULES]).T

return w / w.sum(axis=1, keepdims=True)

def design(X, wn):

"""Layer 4 inputs: for each rule k the columns wn_k*moisture, wn_k*temperature, wn_k."""

Xe = np.c_[X, np.ones(len(X))]

return np.hstack([wn[:, [k]] * Xe for k in range(len(RULES))])

x = np.array([[20, 40]])

for v, col in (("moisture", 0), ("temperature", 1)):

for term, prm in MF[v].items():

print(f"{v} {x[0, col]} is {term:<5} {gbell(x[0, col], *prm):.3f}")

w = np.array([gbell(20, *MF["moisture"][m]) * gbell(40, *MF["temperature"][t]) for m, t in RULES])

print("raw strengths ", np.round(w, 3))

print("normalised strengths", np.round(strengths(x, MF)[0], 3), " sum =", round(strengths(x, MF)[0].sum(), 3))

Output

moisture 20 is dry 0.975

moisture 20 is wet 0.132

temperature 40 is mild 0.115

temperature 40 is hot 0.988

raw strengths [0.112 0.963 0.015 0.131]

normalised strengths [0.092 0.789 0.012 0.107] sum = 1.0

Part B - Predict the Output

Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.

B1.

a, b, c = 25, 2, 10

x = 35

print(1 / (1 + abs((x - c) / a) ** (2 * b)))

My prediction

B2.

import numpy as np

w = np.array([0.1, 0.3, 0.05, 0.05])

print(w / w.sum())

My prediction

B3.

# ... set-up: load the irrigation data (as shown earlier) ...

# ... set-up: the ANFIS model (membership functions, rules, layers) (as shown earlier) ...

A = design(X_tr, strengths(X_tr, MF))

print(A.shape)

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.

# ... set-up: load the irrigation data (as shown earlier) ...

# ... set-up: the ANFIS model (membership functions, rules, layers) (as shown earlier) ...

def fit(X, y, MF):

p, *_ = np.linalg.lstsq(design(X, strengths(X, MF)), y, rcond=None) # least squares

return p

def predict(X, MF, p):

return design(X, strengths(X, MF)) @ p # layer 5: sum

p = fit(X_tr, y_tr, MF)

print(f"test RMSE = {rmse(predict(X_tr, MF, p), y_tr):.2f} minutes")

It runs and prints

test RMSE = 3.61 minutes

Bug, why it happens, and the fixed line(s)

C2.

# ... set-up: load the irrigation data (as shown earlier) ...

# ... set-up: the ANFIS model (membership functions, rules, layers) (as shown earlier) ...

A = design(X_tr, strengths(X_tr, MF))

p, *_ = np.linalg.lstsq(A, y_te, rcond=None)

print(p)

Bug, why it happens, and the fixed line(s)

C3.

def gbell(x, a, b, c):

return 1 / (1 + abs(x - c) / a ** (2 * b))

print(round(gbell(35, 25, 2, 10), 3))

It runs and prints

1.0

Bug, why it happens, and the fixed line(s)

Part D - Graded Tasks (10 marks)

Task

Level

Title

Marks

T1

Easy

Normalised firing strengths

3

T2

Medium

Learn the rule formulas by least squares

3

T3

Challenge

ANFIS or one straight formula?

4

Total

10

Task 1 (Easy, 3 marks): Normalised firing strengths

Write gbell(x, a, b, c) and strengths(X, MF) (layers 1-3) for the four rules dry/mild, dry/hot, wet/mild, wet/hot with MF = dry [25, 2, 10], wet [25, 2, 60], mild [15, 2, 15], hot [15, 2, 45]. For the readings (20 %, 40 °C), (35 %, 30 °C) and (55 %, 20 °C) print the four normalised strengths, check that each row adds to 1, and name the strongest rule.

Sample run (your output must match):

Output

20 %, 40 C: dry/mild 0.092 dry/hot 0.789 wet/mild 0.012 wet/hot 0.107 | sum 1.000 | strongest dry/hot

35 %, 30 C: dry/mild 0.250 dry/hot 0.250 wet/mild 0.250 wet/hot 0.250 | sum 1.000 | strongest all equal

55 %, 20 C: dry/mild 0.072 dry/hot 0.008 wet/mild 0.824 wet/hot 0.096 | sum 1.000 | strongest wet/mild

Your code (or attach the file)

Task 2 (Medium, 3 marks): Learn the rule formulas by least squares

Write fit() and predict() using design() and np.linalg.lstsq. Fit the 70 learning readings, print the four learned rules in the form 'IF moisture is dry AND temperature is mild THEN minutes = p·m + q·t + r', and print the RMSE (root mean square error) on the learning and the test readings.

Sample run (your output must match):

Output

IF moisture is dry AND temperature is mild THEN minutes = -1.07*m +0.25*t +97.93

IF moisture is dry AND temperature is hot THEN minutes = -2.63*m -0.02*t +166.70

IF moisture is wet AND temperature is mild THEN minutes = 1.04*m -0.07*t -50.84

IF moisture is wet AND temperature is hot THEN minutes = 1.40*m -0.57*t -43.46

learning RMSE = 3.61 min, test RMSE = 5.43 min

Your code (or attach the file)

Task 3 (Challenge, 4 marks): ANFIS or one straight formula?

Fit ONE linear formula minutes = p·m + q·t + r to the learning readings (np.linalg.lstsq on [m, t, 1]). Compare its test RMSE with ANFIS, plot predicted against actual minutes for both on the test readings (task3.png), and predict the pump time for moisture 25 %, temperature 38 °C with both. Why does ANFIS win?

Sample run (your output must match):

Output

linear: minutes = -2.24*m +0.34*t +109.89

test RMSE: linear 11.31 min, ANFIS 5.43 min

25 %, 38 C: linear 67.0 min, ANFIS 73.4 min

Saved task3.png

Your code (or attach the file)

Part E - Post-Lab Questions (finish as homework if time runs out)

E1. Name the five layers of ANFIS and what each computes.

Your answer

E2. Why can the consequent parameters be found with one least-squares solve?

Your answer

E3. Why must the test RMSE use unseen readings?

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 gbell, product AND and normalisation

All correct

One slip

Several wrong

Least squares, four rules and RMSE on learning and test data

All present

Some missing

None

Comparison with the linear formula, plot and explanation

Clear and correct

Partly correct

Missing

Submission

  • Make a folder RollNo_P11a with task1.py, task2.py, task3.py (or P11a.ipynb) and task3.png.
  • Upload the zipped folder to the LMS / Google Classroom by the end of the day.
  • Copy Task 2 code and output into your Lab Book (Practical 11a page).
  • Keep gbell(), strengths(), design() and fit() - P11b tunes the membership functions.

← All CS-511 material