Nexus Learn

Practical 12A: Fuzzy C-Means I: Soft Groups of Households

Lab Practical CS-511 Artificial Intelligence 8 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. Explain the difference between hard clustering (k-means) and fuzzy clustering (fuzzy c-means).
  2. Compute a fuzzy membership by hand from the distances to the cluster centres.
  3. Group 60 households with skfuzzy.cluster.cmeans and read the centres, memberships and fuzzy partition coefficient.
  4. Find the 'in-between' households and compare the fuzzy groups with k-means.

2. Word Bank

Word

Simple meaning

Real-world example

Hard clustering

Each item belongs to exactly ONE group (yes or no)

k-means in P06a

Fuzzy clustering

Each item belongs to EVERY group with a degree from 0 to 1

House H23: 0.63 low use, 0.29 winter heater

Membership u

The degree to which an item belongs to a cluster; one item's memberships add to 1

0.63 + 0.29 + 0.05 + 0.02 = 1

Fuzzifier m

A number > 1 that controls how soft the groups are (m = 2 is usual)

m = 1.5 sharper, m = 3 softer

FPC

Fuzzy Partition Coefficient: average of the squared memberships, 1/c (very fuzzy) to 1 (hard)

0.878 for c = 4

Hardening (defuzzify)

Giving each item to the cluster where its membership is highest

u.argmax(axis=0)

3. Background

Why fuzzy groups?

In P06a k-means put every house into exactly one of four groups. But some houses sit between groups: a house using 7.6 units a day in summer and 6.9 in winter is a bit 'low use' and a bit 'winter heater'. Forcing it into one group hides that. MEPCO could treat such a customer differently - for example, offer advice on a heater AND on summer savings.

Fuzzy c-means (FCM)

James Bezdek (1981) used the fuzzy sets of Lectures 22-23 for clustering. Each house gets a membership u in every cluster, and one house's memberships add to 1. The algorithm repeats two steps: (1) each centre = the weighted mean of ALL houses, weighted by u to the power m; (2) each membership is updated from the distances: u_i = 1 / Σ_j (d_i / d_j)^(2/(m-1)). A house close to a centre gets a membership near 1 there.

Reading the result

skfuzzy.cluster.cmeans(data, c, m, error, maxiter) returns cntr (centres), u (c x N memberships), u0, d (distances), jm (objective per iteration), p (iterations) and fpc. Careful: data must be FEATURES x SAMPLES (2 x 60), the transpose of the usual pandas table. FPC near 1 means clear groups; near 1/c means everything is mixed.

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)

4. Time Plan (50 minutes)

Minutes

Activity

Part

0-5

Background: hard vs fuzzy groups; the two FCM steps

1-3

5-10

Guided warm-up: one membership by hand; first cmeans run

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. One membership by hand

House H23 uses (7.6, 6.9) units per day. Take two centres: low use (5, 3) and summer AC (22, 5). With m = 2 the membership in cluster i is 1 / Σ_j (d_i / d_j)². Work it out with NumPy and check that the two memberships add to 1.

import numpy as np

house = np.array([7.6, 6.9])

centres = np.array([[5, 3], [22, 5]])

dist = np.linalg.norm(centres - house, axis=1)

print("distances:", np.round(dist, 2))

m = 2

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

print("memberships:", np.round(u, 3), " sum =", round(u.sum(), 3))

Output

distances: [ 4.69 14.52]

memberships: [0.906 0.094] sum = 1.0

A2. First fuzzy c-means run

Run fuzzy c-means with c = 4 groups on all 60 houses and look at what comes back.

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

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

print("data shape:", data.shape, " u shape:", u.shape)

print("centres (summer, winter):"); print(np.round(cntr, 1))

print(f"iterations: {p} FPC: {fpc:.3f}")

print("H01 memberships:", np.round(u[:, 0], 3), " sum =", round(u[:, 0].sum(), 3))

Output

data shape: (2, 60) u shape: (4, 60)

centres (summer, winter):

[[ 5.1 3.3]

[ 7.8 13.3]

[22.7 5.3]

[30. 16.3]]

iterations: 12 FPC: 0.878

H01 memberships: [0.948 0.031 0.015 0.006] 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.

import numpy as np

u = np.array([[0.7, 0.1, 0.5],

[0.3, 0.9, 0.5]])

print(u.sum(axis=0))

print(u.argmax(axis=0))

My prediction

B2.

import numpy as np

u = np.array([[1.0, 0.0, 1.0, 0.0],

[0.0, 1.0, 0.0, 1.0]])

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

My prediction

B3.

import numpy as np

u = np.full((4, 10), 0.25)

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

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 households (as shown earlier) ...

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

print("centres shape:", cntr.shape, " FPC:", round(fpc, 3))

It runs and prints

centres shape: (4, 60) FPC: 1.0

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

C2.

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

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

print(round(fpc, 3))

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

C3.

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

i = 22 # house H23 is row 22

print("H23 memberships:", np.round(u[i], 3))

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

Part D - Graded Tasks (10 marks)

Task

Level

Title

Marks

T1

Easy

Four fuzzy groups of households

3

T2

Medium

Memberships and the in-between houses

3

T3

Challenge

Fuzzy c-means vs k-means

4

Total

10

Task 1 (Easy, 3 marks): Four fuzzy groups of households

Run fuzzy c-means on households.csv with c = 4, m = 2, error = 0.005, maxiter = 1000, seed = 0. Sort the clusters from the lightest to the heaviest users (by summer + winter units of the centre) and print each centre with the names low use, winter heater, summer AC, heavy all year. Also print the number of iterations and the FPC.

Sample run (your output must match):

Output

low use summer 5.1 winter 3.3

winter heater summer 7.8 winter 13.3

summer AC summer 22.7 winter 5.3

heavy all year summer 30.0 winter 16.3

iterations 12, FPC 0.878

Your code (or attach the file)

Task 2 (Medium, 3 marks): Memberships and the in-between houses

Print a membership table (four columns, one per named cluster) for houses H06, H23, H34 and H59. Then find the five houses whose LARGEST membership is smallest - the most 'in-between' houses - and print each one's units and its two strongest clusters.

Sample run (your output must match):

Output

low use winter heater summer AC heavy all year

house

H06 0.82 0.12 0.05 0.02

H23 0.63 0.29 0.05 0.02

H34 0.11 0.13 0.68 0.08

H59 0.03 0.04 0.73 0.20

H23 ( 7.6, 6.9): low use 0.63, winter heater 0.29

H34 (17.6, 6.5): summer AC 0.68, winter heater 0.13

H59 (27.3, 7.2): summer AC 0.73, heavy all year 0.20

H51 ( 3.9, 6.9): low use 0.76, winter heater 0.20

H06 ( 8.5, 4.2): low use 0.82, winter heater 0.12

Your code (or attach the file)

Task 3 (Challenge, 4 marks): Fuzzy c-means vs k-means

Harden the fuzzy result (each house to its strongest cluster) and run KMeans(n_clusters=4, n_init=10, random_state=0). Match each k-means cluster to the nearest fuzzy centre and count how many houses get the same group from both methods. Draw task3.png: houses coloured by fuzzy group, with marker size growing with the largest membership (small dots = unsure houses), and the fuzzy centres as stars. Which houses does a hard method treat unfairly?

Sample run (your output must match):

Output

same group: 60 of 60 houses

houses with largest membership below 0.7: 2

Saved task3.png

Your code (or attach the file)

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

E1. How is fuzzy c-means different from k-means?

Your answer

E2. What does the FPC measure, and what are its smallest and largest values?

Your answer

E3. Why must the data be transposed before calling cmeans?

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 cmeans call (transpose, c, m) and named centres

All correct

One slip

Several wrong

Membership table and in-between houses

Correct and explained

Partly correct

Missing

Comparison with k-means, plot and explanation

Clear and correct

Partly correct

Missing

Submission

  • Make a folder RollNo_P12a with task1.py, task2.py, task3.py (or P12a.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 12a page).
  • Keep your cmeans code - P12b chooses c and m and predicts memberships of new data.

← All CS-511 material