Two AIs just matched or beat doctors on diagnosis. The catch: none of the patients were real.
Back to Tutorials
aiTutorialbeginner

Two AIs just matched or beat doctors on diagnosis. The catch: none of the patients were real.

June 19, 202641 views5 min read

Learn how to build a basic AI diagnostic system using Python and machine learning, similar to systems that have matched or beaten doctors in medical diagnosis studies.

Introduction

In a groundbreaking study published in Nature, two AI systems called MIRA and AMIE were able to match or even beat human doctors in diagnosing patients and planning treatments. However, these AI systems were tested on simulated patients, not real people. This tutorial will teach you how to build a simple AI diagnostic system using Python and machine learning, similar to the one described in the study. You'll learn how to create a basic AI that can analyze medical data and make diagnoses.

Prerequisites

  • Basic understanding of Python programming
  • Installed Python 3.7 or higher
  • Installed libraries: scikit-learn, pandas, numpy, matplotlib
  • Basic knowledge of medical data concepts

Step-by-step Instructions

Step 1: Set Up Your Python Environment

Install Required Libraries

Before we begin, we need to install the necessary Python libraries. Open your terminal or command prompt and run the following commands:

pip install scikit-learn pandas numpy matplotlib

This installs the essential libraries for machine learning and data analysis. Scikit-learn is our main machine learning library, while pandas and numpy help us handle data efficiently.

Step 2: Create Sample Medical Data

Generate a Simple Dataset

For this tutorial, we'll create a simple dataset that simulates medical data. This dataset will include symptoms, test results, and diagnoses to train our AI system.

import pandas as pd
import numpy as np

# Create sample medical data
np.random.seed(42)
data = {
    'age': np.random.randint(18, 80, 1000),
    'blood_pressure': np.random.randint(90, 180, 1000),
    'cholesterol': np.random.randint(150, 300, 1000),
    'heart_rate': np.random.randint(60, 120, 1000),
    'diabetes': np.random.choice([0, 1], 1000, p=[0.7, 0.3]),
    'diagnosis': np.random.choice(['Healthy', 'Hypertension', 'Diabetes', 'Heart Disease'], 1000, p=[0.4, 0.25, 0.2, 0.15])
}

# Create DataFrame
df = pd.DataFrame(data)
df.to_csv('medical_data.csv', index=False)
print(df.head())

This code generates 1,000 rows of synthetic medical data with various health indicators and diagnoses. The dataset includes age, blood pressure, cholesterol levels, heart rate, and diabetes status, along with a diagnosis for each patient.

Step 3: Load and Explore the Data

Prepare the Dataset

Next, we'll load our dataset and examine its structure to understand what we're working with:

import pandas as pd

df = pd.read_csv('medical_data.csv')
print("Dataset Shape:", df.shape)
print("\nDataset Info:")
df.info()
print("\nFirst 5 Rows:")
print(df.head())
print("\nDiagnosis Distribution:")
print(df['diagnosis'].value_counts())

Understanding our data is crucial. This step shows us how many records we have, what variables we're working with, and how the diagnoses are distributed across our sample.

Step 4: Preprocess the Data

Prepare Data for Machine Learning

Before training our AI, we need to prepare the data properly:

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Define features and target
X = df[['age', 'blood_pressure', 'cholesterol', 'heart_rate', 'diabetes']]
y = df['diagnosis']

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Scale the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

print("Training set shape:", X_train.shape)
print("Testing set shape:", X_test.shape)

We split our data into training and testing sets so we can evaluate how well our AI performs. Scaling ensures all features are on the same scale, which is important for many machine learning algorithms.

Step 5: Train a Machine Learning Model

Build the Diagnostic AI

Now we'll create and train a machine learning model to make diagnoses:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# Create and train the model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)

# Make predictions
y_pred = model.predict(X_test_scaled)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print("Model Accuracy:", accuracy)
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

We're using a Random Forest classifier, which is excellent for medical diagnosis tasks because it can handle complex relationships between symptoms and diseases. The accuracy score tells us how often our AI correctly diagnosed patients.

Step 6: Test the AI with New Patients

Make Predictions on New Data

Let's test our trained AI with some new patient data:

# Create new patient data
new_patients = [[35, 120, 200, 75, 0],  # Young, normal blood pressure, no diabetes
                [65, 160, 280, 85, 1],  # Elderly, high blood pressure, has diabetes
                [45, 110, 180, 70, 0]]  # Middle-aged, normal blood pressure, no diabetes

# Scale the new data using the same scaler
new_patients_scaled = scaler.transform(new_patients)

# Make predictions
predictions = model.predict(new_patients_scaled)
probabilities = model.predict_proba(new_patients_scaled)

# Display results
for i, (patient, pred, prob) in enumerate(zip(new_patients, predictions, probabilities)):
    print(f"Patient {i+1} (Age: {patient[0]}, BP: {patient[1]}, Cholesterol: {patient[2]}, HR: {patient[3]}, Diabetes: {patient[4]}):")
    print(f"  Diagnosis: {pred}")
    print(f"  Confidence: {max(prob):.2f}")
    print()

This step shows how our AI would work in practice, diagnosing new patients based on their symptoms and test results.

Step 7: Visualize Results

Understand Model Performance

Let's visualize how our AI performed:

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import seaborn as sns

# Create confusion matrix
cm = confusion_matrix(y_test, y_pred)

# Plot confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted Diagnosis')
plt.ylabel('Actual Diagnosis')
plt.show()

The confusion matrix helps us see where our AI is most accurate and where it might be making mistakes.

Summary

In this tutorial, you've learned how to build a basic AI diagnostic system similar to the ones described in the Nature study. You've created synthetic medical data, trained a machine learning model, and tested it with new patient data. While this is a simplified version of the real AI systems, it demonstrates the fundamental concepts behind how AI can assist in medical diagnosis. Remember that real medical AI systems require much more complex data, extensive testing, and careful validation before they can be used in clinical settings.

Source: TNW Neural

Related Articles