Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท Applications

Fuzzy Logic for Medical Decision Support

Build a fuzzy risk estimator for a symptom checker: linguistic variables for fever and cough, a Mamdani rule base with rule traces, and why vague medical terms suit fuzzy sets.

Medicine is full of vague words. A "high" fever, "severe" pain, "mild" cough: none of these has a sharp border. A temperature of 37.9 is not clearly fever, 38.1 is not suddenly an emergency, and two doctors will draw the line in slightly different places. Fuzzy logic was applied to medicine early because it lets a program use the same graded language that clinicians use.

This lesson is a programming exercise. The thresholds and rules are invented to teach the technique and are not medical advice. Real clinical decision support is built from validated guidelines and tested under regulatory rules.

The task

We want a small advisor that estimates a triage risk from 0 to 100 using three inputs:

Input Range Terms
Body temperature 35 to 41 C normal, raised, high
Cough severity 0 to 10 mild, moderate, severe
Days of symptoms 0 to 14 short, long

The output, risk, has terms low, medium and high on a 0 to 100 scale. Rules are the kind a nurse might say:

  1. IF temperature is high AND cough is severe THEN risk is high.
  2. IF temperature is raised AND cough is moderate THEN risk is medium.
  3. IF temperature is normal AND cough is mild THEN risk is low.
  4. IF symptoms are long AND temperature is raised THEN risk is high.
  5. IF cough is severe THEN risk is medium.
flowchart LR
  T["Temperature"] --> F["Fuzzify"]
  C["Cough"] --> F
  D["Days"] --> F
  F --> R["5 Mamdani rules"]
  R --> G["Aggregate and<br/>centroid"]
  G --> O["Risk 0-100"]

Three crisp readings go in, one risk number comes out, and the rules that fired are kept for explanation.

Building a reusable fuzzy engine

Because we need a Mamdani engine more than once, we write a small general one. Rules are (conditions, output_term), where conditions map variable names to term names. AND is min, the output is clipped by the firing strength, sets are aggregated with max and the answer is the centroid.

Python A small Mamdani engine and the triage rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The risk rises with the severity of the symptoms, then levels off near 64 for the worst cases (the centroid of the high set cannot reach 100 because the set has a sloping edge). Cases between two terms (such as 37.9 degrees) get a blend. Because rule 5 gives some weight to a severe cough alone, even a case without fever receives a medium contribution.

Explaining the result

A medical tool must say why it produced a number. We re-run one case and print which rules fired and how strongly: this is the fuzzy version of a "how" explanation.

Python Which rules fired for a worsening case?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the printout as a short report: "the temperature is mostly raised, the cough is fully moderate, and the illness has lasted long, so rules 2 and 4 fired at full strength (1.00)." A clinician can challenge a rule, not a mystery number.

Why fuzzy sets fit here

  • Natural language. Experts and guidelines already use graded terms; fuzzy sets formalize them.
  • Smooth behavior. A change of 0.1 degree changes the output slightly, never with a jump.
  • Transparent. Rules can be reviewed line by line.
  • Small data. Rules can be written from guidelines without thousands of labelled cases.

Limits and safety

  • Rules reflect their authors. Biased or outdated rules produce biased advice; review them.
  • Membership shapes are judgments. Different clinicians choose different shapes, so test how sensitive the output is to them.
  • Not a diagnosis. Decision support presents a recommendation to a human, who stays responsible.
  • Evidence-based validation. Compare the output with expert ratings on real cases before use; fuzzy does not remove the need for validation.

The next lesson applies fuzzy ideas to a different job: grouping data when classes overlap.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are fuzzy sets a good match for medical terms such as 'high fever'?

    Terms like 'high fever' have no exact threshold; membership degrees capture the gradual change.
  2. 2. In our engine, how is the strength of a rule with AND conditions computed?

    AND is min: the rule is only as strong as its weakest condition.
  3. 3. What does printing the fired rules provide?

    Rule traces make the reasoning transparent.
  4. 4. Which statement about this lesson's rules is correct?

    The numbers and rules illustrate the technique only.

Summary

  • Medical language is graded, which suits fuzzy sets and linguistic variables.
  • A small Mamdani engine (min for AND, clip, max aggregate, centroid) turns symptoms into a risk score.
  • Printing memberships and fired rules gives explanations a reviewer can check.
  • Rules and shapes encode judgment: review, test sensitivity and validate against real cases before any real use.

Next: Fuzzy C-Means Clustering