Nexus Learn

Practical 12B: Fuzzy C-Means II: Choosing c and m, and Grouping Cotton Fields

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. Choose the number of clusters c with the fuzzy partition coefficient (FPC).
  2. Explain how the fuzzifier m makes the groups sharper or softer.
  3. Group the soil tests of 45 cotton fields and find the least certain fields.
  4. Predict the memberships of new fields with cmeans_predict and turn them into advice.

2. Word Bank

Word

Simple meaning

Real-world example

FPC

Fuzzy Partition Coefficient: Σu²/N, from 1/c (all mixed) to 1 (hard groups)

Highest FPC -> a good c

Fuzzifier m

Controls softness: near 1 = almost hard, larger = softer

m = 1.5, 2, 3

cmeans_predict

Gives memberships of NEW data using centres already found (centres are not moved)

A new soil test from the lab

Soil pH

How acidic (< 7) or alkaline (> 7) the soil is; above about 8.5 is a problem for cotton

pH 8.6: add gypsum

Organic matter

Rotted plant and animal material in the soil (%); more is better for water and nutrients

0.45 % poor, 1.35 % rich

Gypsum

Calcium sulphate; farmers add it to lower the alkalinity of soil

Punjab agriculture department advice

3. Background

How many groups?

In P12a we were told c = 4. Usually nobody tells us. The fuzzy partition coefficient FPC = Σu²/N helps: run cmeans for several c and pick the c with the HIGHEST FPC (the crispest groups). Like the elbow and silhouette of P06a it is a guide, not a law - always check that the groups make sense to a person who knows the field.

The fuzzifier m

m decides how quickly membership falls with distance: u_i = 1 / Σ_j (d_i / d_j)^(2/(m-1)). With m close to 1 the nearest centre takes almost everything (like k-means); with m = 3 memberships are spread out. m = 2 is the usual choice.

The farm problem

The soil-testing lab at Vehari sent soil tests of 45 cotton fields near Burewala: pH and organic matter %. Fields of similar soil need similar advice - gypsum for alkaline soil, compost or green manure for poor soil. soil.csv has the 45 tests. After grouping, NEW tests can be placed in the groups with cmeans_predict, without running the clustering again.

Data files for this lab

households.csv

house,summer_units,winter_units

H01,6.6,1.8

H02,5.9,3.5

H03,19.4,3.7

H04,20.7,5.9

H05,24.1,5.4

H06,8.5,4.2

H07,7.9,11.7

H08,19.6,6.1

H09,22.1,2.4

H10,4.8,2.6

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

soil.csv

field,ph,organic_matter

F01,8.65,0.41

F02,7.73,0.75

F03,7.8,0.84

F04,7.37,1.42

F05,7.36,1.24

F06,8.7,0.41

F07,7.79,0.85

F08,7.23,1.4

F09,7.91,0.88

F10,8.67,0.49

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

4. Time Plan (50 minutes)

Minutes

Activity

Part

0-5

Background: choosing c with FPC; the fuzzifier m; the soil problem

1-3

5-10

Guided warm-up: FPC for c = 2 to 7; m for one house

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. Which c for the households?

Run cmeans on households.csv for c = 2 to 7 and print the FPC of each. Does the best c agree with P06a (k = 4)?

import numpy as np

import pandas as pd

import skfuzzy as fuzz

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

X = df[["summer_units", "winter_units"]].to_numpy() # 60 houses x 2 features

data = X.T # skfuzzy wants features x samples: 2 x 60

for c in range(2, 8):

*_, fpc = fuzz.cluster.cmeans(data, c=c, m=2, error=0.005, maxiter=1000, seed=0)

print(f"c = {c}: FPC = {fpc:.3f}")

Output

c = 2: FPC = 0.856

c = 3: FPC = 0.830

c = 4: FPC = 0.878

c = 5: FPC = 0.796

c = 6: FPC = 0.733

c = 7: FPC = 0.675

A2. The effect of m on one house

House H23 (7.6, 6.9) was the most in-between house in P12a. Print its four memberships for m = 1.5, 2 and 3.

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

cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(data, c=4, m=2, error=0.005, maxiter=1000, seed=0)

order = np.argsort(cntr[:, 0] + cntr[:, 1]) # sort clusters from lightest to heaviest users

cntr, u = cntr[order], u[order]

NAMES = ["low use", "winter heater", "summer AC", "heavy all year"]

for m in (1.5, 2, 3):

um, *_ = fuzz.cluster.cmeans_predict(data[:, [22]], cntr, m, error=0.005, maxiter=1000, seed=0)

print(f"m = {m}: " + " ".join(f"{n} {v:.2f}" for n, v in zip(NAMES, um[:, 0])))

Output

m = 1.5: low use 0.82 winter heater 0.18 summer AC 0.01 heavy all year 0.00

m = 2: low use 0.63 winter heater 0.29 summer AC 0.05 heavy all year 0.02

m = 3: low use 0.47 winter heater 0.32 summer AC 0.13 heavy all year 0.08

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

d = np.array([1.0, 2.0]) # distances of one item to two centres

for m in (2, 3):

u = [1 / np.sum((di / d) ** (2 / (m - 1))) for di in d]

print(m, np.round(u, 3))

My prediction

B2.

import numpy as np

u = np.full((2, 6), 0.5)

print(np.sum(u ** 2) / u.shape[1])

My prediction

B3.

import numpy as np

import pandas as pd

import skfuzzy as fuzz

soil = pd.read_csv("soil.csv")

S = soil[["ph", "organic_matter"]].to_numpy() # 45 fields x 2 features

cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(S.T, c=3, m=2, error=0.005, maxiter=1000, seed=0)

alk = int(np.argmax(cntr[:, 0])) # highest pH

rich = int(np.argmax(cntr[:, 1])) # highest organic matter

NAMES = {alk: "alkaline", rich: "compost-rich"}

NAMES[3 - alk - rich] = "normal"

new = np.array([[8.4, 0.50], [7.5, 1.10]])

u_new, *_ = fuzz.cluster.cmeans_predict(new.T, cntr, 2, error=0.005, maxiter=1000, seed=0)

print(cntr.shape, u_new.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 soil tests (as shown earlier) ...

fpcs = {}

for c in range(2, 7):

*_, fpcs[c] = fuzz.cluster.cmeans(S.T, c=c, m=2, error=0.005, maxiter=1000, seed=0)

best = min(fpcs, key=fpcs.get)

print("best c =", best)

It runs and prints

best c = 6

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

C2.

# ... set-up: load the soil tests (as shown earlier) ...

# ... set-up: fuzzy c-means on the soil tests (c = 3) and name the groups (as shown earlier) ...

new = np.array([[8.4, 0.50], [7.5, 1.10], [8.2, 0.65]])

u_new, *_ = fuzz.cluster.cmeans_predict(new, cntr, 2, error=0.005, maxiter=1000, seed=0)

print(np.round(u_new, 2))

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

C3.

# ... set-up: load the soil tests (as shown earlier) ...

# ... set-up: fuzzy c-means on the soil tests (c = 3) and name the groups (as shown earlier) ...

new = np.array([[8.2, 0.65]])

u_new, *_ = fuzz.cluster.cmeans_predict(new.T, cntr, 3, error=0.005, maxiter=1000, seed=0)

print({NAMES[k]: round(float(u_new[k, 0]), 2) for k in range(3)})

It runs and prints

{'compost-rich': 0.17, 'alkaline': 0.39, 'normal': 0.44}

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

Part D - Graded Tasks (10 marks)

Task

Level

Title

Marks

T1

Easy

How many kinds of field?

3

T2

Medium

Name the groups and find the least certain fields

3

T3

Challenge

Advice for three new fields

4

Total

10

Task 1 (Easy, 3 marks): How many kinds of field?

Load soil.csv. Run fuzzy c-means (m = 2, error = 0.005, maxiter = 1000, seed = 0) for c = 2 to 6, print the FPC of each, choose the best c and plot FPC against c (task1.png).

Sample run (your output must match):

Output

(45, 3)

field ph organic_matter

F01 8.65 0.41

F02 7.73 0.75

F03 7.80 0.84

c = 2: FPC = 0.864

c = 3: FPC = 0.934

c = 4: FPC = 0.828

c = 5: FPC = 0.814

c = 6: FPC = 0.790

best c = 3

Saved task1.png

Your code (or attach the file)

Task 2 (Medium, 3 marks): Name the groups and find the least certain fields

Cluster the fields with c = 3, m = 2. Name the clusters from their centres (highest pH = alkaline, highest organic matter = compost-rich, the other = normal) and print each centre. Harden the memberships and print how many fields are in each group. Then list the three LEAST certain fields (smallest largest membership) with their two strongest groups.

Sample run (your output must match):

Output

compost-rich centre: pH 7.30, organic matter 1.32 %

alkaline centre: pH 8.64, organic matter 0.46 %

normal centre: pH 7.80, organic matter 0.80 %

fields per group: {'compost-rich': 15, 'alkaline': 15, 'normal': 15}

F32 pH 7.57, OM 0.81: normal 0.83, compost-rich 0.13

F22 pH 7.99, OM 0.70: normal 0.87, alkaline 0.09

F28 pH 7.69, OM 0.94: normal 0.89, compost-rich 0.09

Your code (or attach the file)

Task 3 (Challenge, 4 marks): Advice for three new fields

The lab sends three new tests: A (pH 8.4, OM 0.50), B (7.5, 1.10), C (8.2, 0.65). Use cmeans_predict with the centres from Task 2 and m = 2 to get their memberships. Print advice for each: 'add gypsum' if alkaline membership > 0.3, 'add compost / green manure' if normal + alkaline membership > 0.6 (poor organic matter), otherwise 'keep current practice'. Plot the 45 fields coloured by group and the new fields as black triangles labelled A, B, C (task3.png).

Sample run (your output must match):

Output

A pH 8.4, OM 0.5: compost-rich 0.03, alkaline 0.86, normal 0.11 -> add gypsum + add compost / green manure

B pH 7.5, OM 1.1: compost-rich 0.65, alkaline 0.03, normal 0.32 -> keep current practice

C pH 8.2, OM 0.65: compost-rich 0.08, alkaline 0.41, normal 0.51 -> add gypsum + add compost / green manure

Saved task3.png

Your code (or attach the file)

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

E1. How do you choose c with the FPC, and why is it only a guide?

Your answer

E2. What happens to the memberships when m is increased from 1.5 to 3?

Your answer

E3. What is the difference between cmeans and cmeans_predict?

Your answer

Marking Rubric (applied to Part D)

Criterion

Full

Partial

None

Code runs without errors

Runs fully

Small fixable error

Does not run

FPC for several c, best c and plot

All correct

One slip

Several wrong

Groups named from centres, sizes and mixed fields

Correct and explained

Partly correct

Missing

Predictions for new fields, advice and plot

Clear and correct

Partly correct

Missing

Submission

  • Make a folder RollNo_P12b with task1.py, task2.py, task3.py (or P12b.ipynb), task1.png and task3.png.
  • Upload the zipped folder to the LMS / Google Classroom by the end of the day.
  • Copy Task 3 code and output into your Lab Book (Practical 12b page).
  • P13 (next week) saves a fuzzy system to a file and runs it as a program - keep your fuzzy code tidy.

← All CS-511 material