πŸ₯ AI-Based Expert System for Medical Diagnosis

A complete AI-powered medical diagnosis system that combines knowledge-based reasoning, probabilistic inference, search algorithms, and machine learning to diagnose diseases from symptoms.

System Architecture

Patient Input (Symptoms)
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LAYER 1: KNOWLEDGE BASE        β”‚
β”‚  41 diseases, 273+ symptoms     β”‚
β”‚  Semantic network (NetworkX)    β”‚
β”‚  Diagnostic rules (forward     β”‚
β”‚  chaining)                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
     β–Ό                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ LAYER 2:     β”‚ β”‚ LAYER 3:       β”‚
β”‚ BAYESIAN     β”‚ β”‚ SEARCH         β”‚
β”‚ REASONING    β”‚ β”‚ ALGORITHMS     β”‚
β”‚ β€’ Naive Bayesβ”‚ β”‚ β€’ A* Search    β”‚
β”‚ β€’ Bayesian   β”‚ β”‚ β€’ Best-First   β”‚
β”‚   Network    β”‚ β”‚   Search      β”‚
β”‚   (pgmpy)   β”‚ β”‚                β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LAYER 4: MACHINE LEARNING      β”‚
β”‚  β€’ Decision Tree (explainable) β”‚
β”‚  β€’ Neural Network (MLP)        β”‚
β”‚  β€’ Random Forest + Ensemble    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LAYER 5: FUSION ENGINE         β”‚
β”‚  Weighted ensemble of all       β”‚
β”‚  methods with configurable       β”‚
β”‚  weights                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LAYER 6: ETHICS                β”‚
β”‚  β€’ Privacy Guard (HIPAA)        β”‚
β”‚  β€’ Explainability Engine        β”‚
β”‚  β€’ Accountability Logger        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Modules

# Module Description Technology
1 Knowledge Base Disease-symptom database, semantic networks, diagnostic rules NetworkX, HuggingFace Datasets
2 Probabilistic Reasoning Bayes' Theorem + Bayesian Networks with exact inference pgmpy, Variable Elimination
3 Search Algorithms A* Search and Best-First Search on bipartite graph heapq, NetworkX
4 Machine Learning Decision Tree, Neural Network (MLP), Random Forest, Ensemble scikit-learn
5 Evaluation Accuracy, Precision, Recall, F1 β€” before vs after ML scikit-learn metrics
6 Ethics Layer Privacy, Explainability, Accountability SHA-256, Audit Logs
7 Fusion Engine Weighted combination of all methods Configurable weights
8 Orchestrator Unified interface tying all modules together Python OOP

Dataset

  • Source: QuyenAnhDE/Diseases_Symptoms (HuggingFace)
  • Scale: 41 diseases (top by symptom count), 273 relevant symptoms
  • Processing: Binary symptom matrix generated from KB with 120 samples/disease
  • Noise Model: Variable noise (10-25%) for true symptoms, 12% false positive rate
  • Split: 80/20 train/test with stratification
  • Total: ~4,920 samples

Evaluation Results

Method Accuracy Precision (wtd) Recall (wtd) F1 Score (wtd)
KB Symptom Matching 0.9654 0.9681 0.9654 0.9652
Naive Bayes 0.9583 0.9602 0.9583 0.9586
Random Forest 0.9329 0.9348 0.9329 0.9327
Neural Network (MLP) 0.9217 0.9263 0.9217 0.9218
Decision Tree 0.4258 0.4376 0.4258 0.4263

Key Findings

  1. KB Symptom Matching achieves the highest accuracy because it leverages the complete knowledge base with exact symptom-disease mappings
  2. Naive Bayes performs nearly as well (F1=0.9586) by learning probabilistic relationships
  3. Neural Network (MLP) and Random Forest achieve >92% F1, demonstrating ML's ability to learn diagnostic patterns
  4. Decision Tree alone underperforms but provides crucial explainability via decision paths and feature importance
  5. The Fusion Engine combines all methods for robust, well-calibrated diagnoses

Before vs After ML Integration

  • Before ML (KB only): F1 = 0.9652
  • After ML (all methods): Multiple methods with F1 > 0.92
  • The true value of ML is in generalization to unseen symptom combinations and explainability rather than pure accuracy on synthetic data
  • On real clinical data (where KB may be incomplete), ML models would outperform pure KB matching

Quick Start (Google Colab)

  1. Open the notebook: ai_medical_diagnosis_system.ipynb
  2. Run all cells β€” the system will:
    • Install dependencies
    • Load the knowledge base from HuggingFace
    • Train all models
    • Evaluate and compare methods
    • Run demo diagnoses with full explanations

Ethics

  • Privacy: HIPAA Safe Harbor de-identification via SHA-256 hashing of PII
  • Explainability: Every diagnosis includes reasoning steps, evidence strength, and feature importance
  • Accountability: Full audit trail with medical disclaimers, human review flags for low-confidence diagnoses

Project Structure

β”œβ”€β”€ ai_medical_diagnosis_system.ipynb   # Complete Colab notebook
β”œβ”€β”€ README.md                            # This file
└── modules/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ knowledge_base.py                # Layer 1: KB + semantic network
    β”œβ”€β”€ bayesian_reasoning.py             # Layer 2: Naive Bayes + BN
    β”œβ”€β”€ search_algorithms.py             # Layer 3: A* + Best-First
    β”œβ”€β”€ ml_models.py                     # Layer 4: DT + NN + RF + Ensemble
    β”œβ”€β”€ evaluation.py                    # Layer 5: Metrics + comparison
    β”œβ”€β”€ ethics_layer.py                  # Layer 6: Privacy + Explainability
    β”œβ”€β”€ fusion_engine.py                 # Layer 7: Weighted fusion
    └── orchestrator.py                 # Layer 8: Main system

Discussion & Improvements

Area Current Improvement
Dataset Synthetic from KB (~5K samples) Use real clinical data (MIMIC-IV, DDXPlus)
Diseases 41 Expand to 500+ with ICD-10 mapping
Symptoms ~273 Add lab results, vital signs, imaging
BN Structure Naive Bayes (Disease→Symptoms) Learn structure with constraint-based methods
ML Models sklearn DT/MLP/RF Try XGBoost, Transformers, domain-specific models
Fusion Weights Fixed Learn optimal weights via validation
Multi-label Single disease Support comorbidity detection

Constraints

  • ⚠️ Not for clinical use β€” this is a research prototype
  • Uses open-source resources only (no paid APIs)
  • Runs within Google Colab free tier
  • Reasonable runtime: ~5-10 minutes for full training

Citation

  • DDXPlus: Fansi Tchango et al., arXiv:2205.09148
  • pgmpy: Ankan & Panda, JMLR 2024
  • QuyenAnhDE/Diseases_Symptoms dataset on HuggingFace
Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Paper for Shadia-Sultana-Anonna/ai-medical-diagnosis-system