Machine Learning Fundamentals: The Confusion Matrix — Transcript
Full transcript
- 0:01If you feel confused,
- 0:05don't sweat it.
- 0:07StatQuest is here.
- 0:12StatQuest.
- 0:14Hello, I'm Josh Starmer and welcome to
- 0:17StatQuest. Today we're going to cover
- 0:19another machine learning fundamental,
- 0:21the confusion matrix, and it's going to
- 0:23be clearly explained.
- 0:26Imagine that we have this medical data.
- 0:29We've got some clinical measurements
- 0:31like chest pain, good blood circulation,
- 0:33blocked arteries, and weight.
- 0:36And we want to apply a machine learning
- 0:38method to them to predict whether or not
- 0:40someone will develop heart disease.
- 0:43To do this, we could use logistic
- 0:45regression
- 0:47or K nearest neighbors
- 0:49or random forest
- 0:51or some other method. There are tons to
- 0:54choose from.
- 0:55How do we decide which one works best
- 0:58with our data?
- 1:00We start by dividing the data into
- 1:02training and testing sets.
- 1:05Note, this would be an excellent
- 1:08opportunity to use cross-validation. And
- 1:10if you're not familiar with that, well,
- 1:12check out the StatQuest.
- 1:15Then we train all of the methods we're
- 1:17interested in with the training data.
- 1:21And then test each method on the testing
- 1:23set.
- 1:26Now we need to summarize how each method
- 1:28performed on the testing data.
- 1:32One way to do this is by creating a
- 1:34confusion matrix for each method.
- 1:38The rows in a confusion matrix
- 1:40correspond to what the machine learning
- 1:42algorithm predicted.
- 1:44And the columns correspond to the known
- 1:47truth.
- 1:49Since there are only two categories to
- 1:51choose from,
- 1:52heart disease or does not have heart
- 1:56disease,
- 1:58then the top left corner contains true
- 2:01positives.
- 2:03These are patients that had heart
- 2:05disease that were correctly identified
- 2:08by the algorithm.
- 2:10The true negatives are in the bottom
- 2:12right-hand corner.
- 2:14These are patients that did not have
- 2:16heart disease that were correctly
- 2:18identified by the algorithm.
- 2:21The bottom left-hand corner contains
- 2:23false negatives.
- 2:25False negatives are when a patient has
- 2:28heart disease, but the algorithm said
- 2:30they didn't.
- 2:32Lastly, the top right-hand corner
- 2:34contains false positives.
- 2:37False positives are patients that do not
- 2:39have heart disease, but the algorithm
- 2:42says they do.
- 2:44For example, when we applied the random
- 2:46forest to the testing data,
- 2:49there were 142 true positives, patients
- 2:53with heart disease that were correctly
- 2:55classified,
- 2:57and 110 true negatives, patients without
- 3:00heart disease that were correctly
- 3:02classified.
- 3:04However, the algorithm misclassified 29
- 3:08patients that did have heart disease by
- 3:10saying they did not. These are false
- 3:13negatives.
- 3:15And the algorithm misclassified 22
- 3:17patients that did not have heart disease
- 3:20by saying that they did. These are false
- 3:22positives.
- 3:25The numbers along the diagonal, the
- 3:27green boxes, tell us how many times the
- 3:30samples were correctly classified.
- 3:33The numbers not on the diagonal, the red
- 3:36boxes, are samples that the algorithm
- 3:39messed up.
- 3:41Now we can compare the random forest
- 3:43confusion matrix
- 3:46to the confusion matrix we get when we
- 3:48use K nearest neighbors.
- 3:51K nearest neighbors was worse the random
- 3:54forest at predicting patients with the
- 3:56heart disease,
- 3:58107 versus 142,
- 4:02and worse at predicting patients without
- 4:05heart disease, 79 versus 110.
- 4:10So, if we had to choose between using
- 4:11the random forest and K-nearest
- 4:13neighbors, we would choose the random
- 4:16forest.
- 4:18Bam!
- 4:20Lastly, we can apply logistic regression
- 4:22to the testing data set and create a
- 4:24confusion matrix.
- 4:27These two confusion matrices are very
- 4:29similar and make it hard to choose which
- 4:32machine learning method is a better fit
- 4:34for this data.
- 4:36We'll talk about more sophisticated
- 4:38metrics like sensitivity, specificity,
- 4:41ROC, and AOC that can help us make a
- 4:44decision in the next Stack Quests.
- 4:48Now that we have the basic confusion
- 4:50matrix figured out, let's look at a more
- 4:52complicated one.
- 4:55Here's a new data set.
- 4:57Now the question is, based on what
- 5:00people think of these movies, Jurassic
- 5:02Park III, Run for Your Wife, Out Cold,
- 5:06spelled with a K, and Howard the Duck,
- 5:09can we use a machine learning method to
- 5:11predict their favorite movie?
- 5:15If the only options for favorite movie
- 5:17were Troll 2, Gore Police, or Cool as
- 5:21Ice,
- 5:23then the confusion matrix would have
- 5:24three rows and three columns.
- 5:28But just like before, the diagonal, the
- 5:31green boxes, are where the machine
- 5:33learning algorithm did the right thing,
- 5:36and everything else is where the
- 5:38algorithm messed up.
- 5:40In this case, the machine learning
- 5:42algorithm didn't do very well, but can
- 5:45you blame it? These are all terrible
- 5:47movies.
- 5:49Bam!
- 5:51Ultimately, the size of the confusion
- 5:53matrix is determined by the number of
- 5:55things we want to predict.
- 5:58In the first example, we were only
- 6:00trying to predict two things, if someone
- 6:02had heart disease or if they didn't.
- 6:05And that gave us a confusion matrix with
- 6:08two rows and two columns.
- 6:11In the second example, we had three
- 6:13things to choose from.
- 6:15And a confusion matrix with three rows
- 6:18and three columns.
- 6:20If we had four things to choose from, we
- 6:23get a confusion matrix with four rows
- 6:25and four columns.
- 6:28And if we had 40 things to choose from,
- 6:30we get a confusion matrix with 40 rows
- 6:33and 40 columns.
- 6:35Double bam!
- 6:38In summary, a confusion matrix tells you
- 6:41what your machine learning algorithm did
- 6:43right
- 6:44and what it did wrong.
- 6:47Hooray! We've made it to the end of
- 6:49another exciting StatQuest. If you like
- 6:52this StatQuest and want to see more,
- 6:54please subscribe. And if you want to
- 6:55support StatQuest, well, consider buying
- 6:58one or two of my original songs. All
- 7:01right, until next time, quest on.
About this transcript
This page contains the full transcript of Machine Learning Fundamentals: The Confusion Matrix by StatQuest with Josh Starmer, generated from the public captions YouTube serves with the video. The transcript has 899 words across 170 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.