I am currently working on an ambulance dataset and one of my tasks is to find when a patient was misdiagnosed by the call dispatcher.

I have two codes; a dispatch code(what the dispatcher believes is wrong with the patient) and a hospital code(what a doctor decides the actual diagnosis is in hospital)

I'm looking for a way of visualizing the relationship between the two codes i.e. given ambulance code x what is the probability of each ambulance code being the outcome.

This can be computed using SQL pretty easily but I'm looking for a way to visualize/cluster it would be great. Any help would be appreciated.

EDIT: Some good feedback in the comments

firstly the dimensionality: Dispatch code can take 1 of 1722 unique values

hospital codes can take 1 of 1058 unique values

The two codes are entirely different an example would be as follows

Dispatcher 17D03:Unconscious 
Hospital R41:Other symptoms and signs involving cognitive functions and awareness

My interest is in visualizing the relationship. So for example given a dispatcher code of unconscious what are the most common hospital codes?

Again this is fairly easy to calculate numerically but a visualization would make it easier to explain to my stake holders.

  • 3
    $\begingroup$ How many unique codes are there? Do hospital and dispatch use the same set of codes? If the answers are "low" and "yes", then a heatmap should be good. $\endgroup$
    – Mephy
    Jul 30, 2018 at 16:20
  • $\begingroup$ How many dimensions does it have? $\endgroup$ Jul 30, 2018 at 18:18
  • $\begingroup$ A matrix as in a spreadsheet (columns=dispatch, rows=hospital) certainly seems appropriate. $\endgroup$ Jul 30, 2018 at 19:46
  • $\begingroup$ Unfortunately both codes have high dimensionality and use a different set of codes. Rather than visualising how frequently a code is classified correctly I want to find the commen relationships for example row in the database is this: $\endgroup$
    – counterpig
    Aug 1, 2018 at 8:28
  • $\begingroup$ Dispatcher 17D03:Unconscious Hospital R41:Other symptoms and signs involving cognitive functions and awareness $\endgroup$
    – counterpig
    Aug 1, 2018 at 8:30

3 Answers 3


You can use a confusion matrix to generate the heat map of your data.

Suppose you have:

labels = ['cardiac arrest', 'choking', 'seizure']
dispatch_code = ['cardiac arrest', 'choking', 'seizure', 'choking', 'seizure', 'seizure', 'cardiac arrest', 'cardiac arrest']
hospital_code = ['cardiac arrest', 'choking', 'cardiac arrest', 'choking', 'seizure', 'seizure', 'seizure', 'cardiac arrest']

Then you can plot with:

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

cm = confusion_matrix(hospital_code, dispatch_code)
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
plt.title('Confusion matrix of the classifier')
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)

enter image description here

This example shows that often there is a correct code coming from the dispatcher (yellow stripe in the middle). Cardiac arrest and seizure are often confused (blue) and chocking is never misclassified (purple).


If you are after counts of multi dimensional variables then Mosaicplot can help. In R there is a function from the graphics package called mosaicplot. It's one of base packages that come with R.

HairEyeColor , , Sex = Male


Hair Brown Blue Hazel Green Black 32 11 10 3 Brown 53 50 25 15 Red 10 10 7 7 Blond 3 30 5 8

, , Sex = Female


Hair Brown Blue Hazel Green Black 36 9 5 2 Brown 66 34 29 14 Red 16 7 7 7 Blond 4 64 5 8

mosiacplot(HairEyeColor) enter image description here


In the end I ended up using an alluvial diagram on rawgraphs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.