Latest / CyberSecurity Summary / Easily: Practical Machine Learning Algorithms with Python
Transcript
- 0:00Speaker 1: Okay, So have you ever felt like you're just drowning
- 0:02Speaker 1: in information, you know, for a project, or maybe getting
- 0:06Speaker 1: ready for a meeting, or even just trying to learn
- 0:08Speaker 1: something new, and you just wish someone could kind of
- 0:10Speaker 1: boil it all down.
- 0:11Speaker 2: Yeah, just give you the essentials, right, what really.
- 0:13Speaker 1: Matters exactly, and maybe, you know, throw in a few
- 0:16Speaker 1: surprising bits to keep it interesting. Well, if that sounds
- 0:20Speaker 1: like you, you are definitely in the right place, because
- 0:23Speaker 1: that's what we do here on the Deep Dive. We're
- 0:24Speaker 1: sort of your shortcut to getting properly informed, and today
- 0:28Speaker 1: we're taking a deep dive into your source material. These
- 0:32Speaker 1: are excerpts from Easily Practical Machine Learning Algorithms with Python
- 0:36Speaker 1: by doctor Darren Thomas.
- 0:38Speaker 2: Yeah, and this isn't like your standard dance textbook. The author,
- 0:43Speaker 2: doctor Thomas, he's got a PhD, loads the teaching experience.
- 0:46Speaker 1: And get this a background and saxophone performance, right, but.
- 0:50Speaker 2: His passion for machine learning led him to use these
- 0:52Speaker 2: algorithms in education. He's a lecturer now at Asia Pacific
- 0:56Speaker 2: International University and.
- 0:58Speaker 1: The book's aim, which is really really key for us today,
- 1:01Speaker 1: is to be simple, easy to follow, a kind of
- 1:05Speaker 1: condensed guide for actually using these algorithms with Python exactly.
- 1:09Speaker 2: He even says his goal was always to show what
- 1:12Speaker 2: to do, rather than talk a lot about how to
- 1:14Speaker 2: do it. So less heavy theory, more hands on application.
- 1:17Speaker 1: Okay, so important. Note, then, the book and this deep
- 1:22Speaker 1: dive too, sort of assumes you've already got some background
- 1:25Speaker 1: in Python. Maybe data science stats.
- 1:27Speaker 2: Yeah, it's more for folks looking to build on existing skills,
- 1:30Speaker 2: maybe not for absolute beginners to data science itself.
- 1:33Speaker 1: Right, So our mission today we want to unpack some
- 1:36Speaker 1: of the most common machine learning algorithms. We'll look at
- 1:39Speaker 1: classification that's predicting.
- 1:41Speaker 2: Categories like spam or not spam.
- 1:43Speaker 1: Exactly, and numeric prediction predicting continuous values like say, house prices.
- 1:49Speaker 1: Will get into how they're used, their surprising upsides, their challenges,
- 1:54Speaker 1: and crucially, how you actually figure out if the models
- 1:57Speaker 1: any good and how to make it better.
- 1:58Speaker 2: Yeah, judging and improving them key.
- 2:00Speaker 1: Totally, So ready to jump in. Let's unpack this.
- 2:02Speaker 2: Let's do it, Okay.
- 2:04Speaker 1: First up, decision trees. You can think of these as
- 2:08Speaker 1: like the foundation for a lot of predictive stuff.
- 2:11Speaker 2: Right At its heart, a decision tree is just a
- 2:13Speaker 2: way to classify things or predict numbers by splitting your
- 2:17Speaker 2: data up. Splitting it how It basically keeps dividing the
- 2:20Speaker 2: sample into smaller and smaller groups, trying to make each
- 2:23Speaker 2: little group as similar as possible inside.
- 2:26Speaker 1: So it looks like a tree visually like a flow.
- 2:28Speaker 2: Shart exactly like a float chart. You start at the
- 2:30Speaker 2: top the root node. That's your first big decision point.
- 2:33Speaker 2: Then you follow branches down through more decision nodes, making more.
- 2:36Speaker 1: Splits until you hit the end the leaf nodes.
- 2:39Speaker 2: Yep, the leaf nodes. That's where you get your final prediction.
- 2:42Speaker 1: And how does it decide where to split?
- 2:44Speaker 2: Well? For classification, it often uses something called entropy. High
- 2:48Speaker 2: entropy means things are really mixed up. The tree tries
- 2:51Speaker 2: to make splits that reduce that entropy, creating purer groups.
- 2:55Speaker 1: Lower entropy, more homogeneous, got it. And for predicting numbers.
- 3:00Speaker 2: Prediction that uses metrics like means squared error msee or
- 3:04Speaker 2: maybe R square to guide the splits.
- 3:07Speaker 1: Okay, so what's great about them? Why start here?
- 3:10Speaker 2: Well? A big plus is flexibility. They handle missing data
- 3:13Speaker 2: pretty well. They don't really care if your data isn't,
- 3:16Speaker 2: you know, perfectly normal. A nice bell curve right, and
- 3:19Speaker 2: you don't even have to use all your variables. Plus,
- 3:22Speaker 2: and this is a big one, they're relatively easy to interpret.
- 3:25Speaker 1: Ah, so you don't need a math PhD to figure
- 3:28Speaker 1: out what it's doing.
- 3:29Speaker 2: Pretty much. You can literally trace a path down the
- 3:32Speaker 2: tree and see the reasoning that.
- 3:34Speaker 1: Transparency sounds really useful, especially if you need to explain
- 3:38Speaker 1: why a prediction was made.
- 3:40Speaker 2: Absolutely imagine telling someone why their loan was denied. Showing
- 3:44Speaker 2: them a simple tree is way easier than explaining you know,
- 3:48Speaker 2: complex equations from some other models. Builds trust.
- 3:52Speaker 1: Okay, but there's always a catch, right, what's the downside?
- 3:55Speaker 2: The main one is that they can get really complex
- 3:57Speaker 2: if you let them grow too much, really deep trees,
- 4:00Speaker 2: and that leads to that often leads to overfitting.
- 4:03Speaker 1: Overfitting like it learns the training data too well.
- 4:06Speaker 2: Exactly, it fits the specific sample data perfectly, maybe even
- 4:09Speaker 2: the noise. But then it can generalize well to new
- 4:12Speaker 2: data it hasn't seen before.
- 4:14Speaker 1: Like memorizing answers instead of understanding the concept perfect analogy.
- 4:18Speaker 2: And of course, a super complex tree, even though it's visual,
- 4:22Speaker 2: can still be hard to explain easily.
- 4:24Speaker 1: Okay, let's make it concrete. The source had an example, right,
- 4:26Speaker 1: a cancer data.
- 4:27Speaker 2: Set, Yeah, predicting health status alive or dead. The model
- 4:31Speaker 2: mainly used variables like time and age for its splits.
- 4:35Speaker 1: Then how did it perform?
- 4:36Speaker 2: It got seventy eight percent accuracy on the data it
- 4:38Speaker 2: trained on, okay, but then on the unseen test data
- 4:42Speaker 2: it drops slightly to seventy three percent.
- 4:44Speaker 1: That drop five percent is that bad or expected?
- 4:47Speaker 2: That's actually pretty common and often expected. It shows it's generalizing. Okay,
- 4:51Speaker 2: still performing decently on new stuff. It learned patterns and
- 4:54Speaker 2: applied them.
- 4:54Speaker 1: Right now, What about using it for numeric prediction, same
- 4:58Speaker 1: data set, but predicting aid yep.
- 5:01Speaker 2: So here, instead of looking at purity like with dagony,
- 5:03Speaker 2: the tree uses mse means squared error in the nodes,
- 5:07Speaker 2: and the leaves predict the average age for that group.
- 5:10Speaker 1: And which variables were important there.
- 5:12Speaker 2: The source mentioned pH dot Carno Andmeal dot.
- 5:14Speaker 1: Cow and the results.
- 5:16Speaker 2: Well, the correlation between the actual age and predicted age
- 5:20Speaker 2: was okay on the training data about point five to
- 5:22Speaker 2: four moderate moderate yeah, But on the test data it
- 5:26Speaker 2: dropped way down to point one eight.
- 5:28Speaker 1: Ouch, big drop? What about the error the.
- 5:31Speaker 2: MSc MC was sixty one point eight on the training set,
- 5:34Speaker 2: but jumped up to eighty five point twenty four on
- 5:36Speaker 2: the test set.
- 5:37Speaker 1: So again, that drop tells us.
- 5:39Speaker 2: It tells us while it learned something, it really struggled
- 5:42Speaker 2: to generalize the age prediction to new people. Highlights that
- 5:45Speaker 2: overfitting risk with single trees, which.
- 5:48Speaker 1: Leads us perfectly into the next one. Random forest. This
- 5:50Speaker 1: sounds cool.
- 5:51Speaker 2: Yeah, this is where it gets really interesting. Random Forest
- 5:53Speaker 2: tackles that overfitting problem head on. How instead of building
- 5:56Speaker 2: just one decision tree, it builds hundreds, maybe even thousands
- 6:00Speaker 2: of them.
- 6:00Speaker 1: Wow? Okay, where does the random part come?
- 6:02Speaker 2: In? Two places? First, each tree is built using a
- 6:05Speaker 2: different random sample of your data drawn with replacement called
- 6:09Speaker 2: boots trapping. Second, at each split point in a tree,
- 6:12Speaker 2: it only considers a random subset of your available features.
- 6:15Speaker 1: So not every tree sees all the data, and not
- 6:18Speaker 1: every split considers all the factors exactly.
- 6:20Speaker 2: And the idea is you get lots of slightly different trees,
- 6:24Speaker 2: none of them perfect, but hopefully their errors are kind
- 6:26Speaker 2: of random and cancel each other out.
- 6:28Speaker 1: So how does it make a final prediction with all
- 6:30Speaker 1: those trees?
- 6:31Speaker 2: It's pretty democratic. Actually, for classification, it's just a majority
- 6:35Speaker 2: vote whichever prediction most trees make.
- 6:38Speaker 1: Wins simple enough, and for predicting numbers.
- 6:41Speaker 2: It just averages the predictions from all the individual trees.
- 6:44Speaker 1: And this whole wisdom of the crowd thing really helps
- 6:47Speaker 1: with overfitting massively.
- 6:49Speaker 2: That aggregation step makes the model much more robust and
- 6:53Speaker 2: way less prone to overfitting compared to a single complex
- 6:58Speaker 2: decision tree, so.
- 6:59Speaker 1: The benefits seem clear less. Overfitting works well even if
- 7:03Speaker 1: you don't have tons of data handles missing values.
- 7:06Speaker 2: Sounds great, It often is. It's a very popular, reliable
- 7:09Speaker 2: algorithm for those reasons.
- 7:10Speaker 1: Okay, but the drawback you mentioned transparency with decision trees,
- 7:14Speaker 1: what about here?
- 7:15Speaker 2: Oh yeah, that's the main trade off. With potentially thousands
- 7:18Speaker 2: of trees. You can't just visualize it like a single
- 7:21Speaker 2: flow chart.
- 7:22Speaker 1: Anymore, so it becomes a black box pretty much.
- 7:25Speaker 2: You know what goes in, you know what comes out,
- 7:27Speaker 2: but explaining exactly how it arrived at that specific prediction
- 7:31Speaker 2: is really hard.
- 7:32Speaker 1: That's a problem when.
- 7:34Speaker 2: When you need to explain the why, like we said,
- 7:36Speaker 2: loan applications, medical diagnoses, if you can't explain the reasoning,
- 7:41Speaker 2: it can cause issues with trust or even regulations that
- 7:44Speaker 2: demand transparency.
- 7:46Speaker 1: Right, so that lack of interpretability is a real consideration.
- 7:50Speaker 1: You might get great predictions but lose the explanation.
- 7:52Speaker 2: It's a definite trade off you have to weigh.
- 7:54Speaker 1: Let's look at the example the doctor Aus data set
- 7:57Speaker 1: predicting gender.
- 7:58Speaker 2: Right, the source noted income and age came out as
- 8:01Speaker 2: strong predictors, pointing out the known differences in salaries and
- 8:05Speaker 2: life expectancy. And the performance impressive on the training data
- 8:08Speaker 2: ninety three percent accuracy. Wow, but then quite a big
- 8:12Speaker 2: drop on the test data, down to sixty six percent.
- 8:15Speaker 1: Oo. That's a nearly thirty percent draw. What does that
- 8:18Speaker 1: tell us?
- 8:18Speaker 2: It tells us that while the model learned the training
- 8:21Speaker 2: data extremely well, almost perfectly, it really struggled to generalize
- 8:26Speaker 2: that learning.
- 8:27Speaker 1: So even random forest isn't immune to some overfitting. Or
- 8:31Speaker 1: maybe the training data just wasn't fully representative.
- 8:33Speaker 2: Could be either or both. It's a stark reminder that
- 8:37Speaker 2: high training accuracy is nice, but test accuracy is what
- 8:40Speaker 2: really counts for real world use.
- 8:43Speaker 1: Okay, and the numeric prediction example predicting income from the
- 8:46Speaker 1: same doctor Aus data.
- 8:48Speaker 2: Set here age was the most important variable by far
- 8:51Speaker 2: makes intuitive sense, right. Yeah, people often earn more as
- 8:53Speaker 2: they get older.
- 8:53Speaker 1: Sure, and the numbers.
- 8:55Speaker 2: Strong correlation on the training data point eight three, but
- 8:58Speaker 2: again a drop on the test data down to point
- 9:00Speaker 2: four to eight.
- 9:00Speaker 1: Still a decent drop, and the error ms.
- 9:04Speaker 2: MSc was low on the training set point zero four
- 9:06Speaker 2: and higher on the test set point one to one.
- 9:08Speaker 1: So similar story. Good at learning the training data, but
- 9:11Speaker 1: only moderately good at generalizing the income prediction.
- 9:14Speaker 2: Yeah, pretty much. It captures the relationship it sees, but
- 9:17Speaker 2: applying it to new unseen individuals is where the real
- 9:20Speaker 2: test lies.
- 9:20Speaker 1: Okay, moving on K nearest neighbor or k NN, this
- 9:24Speaker 1: one sounds neighborly, huh.
- 9:26Speaker 2: Yeah, it's actually quite intuitive. The core idea is predicting
- 9:29Speaker 2: by proximity, like.
- 9:31Speaker 1: Your example of walking into a classroom of twelve year olds.
- 9:34Speaker 1: If another kid walks in, you guess they're also twelve.
- 9:37Speaker 2: Exactly like that, Yeah, kNN looks at an unknown data
- 9:41Speaker 2: point and finds the k known data points that are
- 9:43Speaker 2: nearest to it in the feature space.
- 9:45Speaker 1: And nearest is usually measured by.
- 9:47Speaker 2: Typically Euclidian distance, just the straight line distance between points
- 9:52Speaker 2: in that multi dimensional space.
- 9:53Speaker 1: So K is just how many neighbors you look at
- 9:55Speaker 1: like the three nearest or five nearest.
- 9:58Speaker 2: Yep, K is the number of you consider.
- 10:01Speaker 1: And how do those neighbors make the prediction for classification?
- 10:03Speaker 2: They vote. That's why K is usually an odd number
- 10:06Speaker 2: to avoid ties. For numeric prediction, it's just the average
- 10:10Speaker 2: of the neighbor's values.
- 10:11Speaker 1: What's it good for.
- 10:12Speaker 2: It's pretty good with non linear data where the boundary
- 10:14Speaker 2: is in a straight line. And it's non parametric, meaning
- 10:18Speaker 2: it doesn't make strong assumptions about how your data is distributed.
- 10:21Speaker 2: Makes it flexible.
- 10:22Speaker 1: Sounds simple enough. Any hidden traps.
- 10:25Speaker 2: Well, it's sometimes called a lazy learning algorithm. Lazy because
- 10:29Speaker 2: it doesn't really build a model during training. It basically
- 10:32Speaker 2: just stores all the training data. The real work happens
- 10:35Speaker 2: only when you ask for a prediction.
- 10:37Speaker 1: Ah. So it doesn't give you much insight into why
- 10:40Speaker 1: variables are important.
- 10:41Speaker 2: Not really no abstraction. And because it stores everything, it
- 10:45Speaker 2: can struggle with really large data sets. Needs a lot
- 10:49Speaker 2: of memory.
- 10:50Speaker 1: And there was another crucial point something about scale.
- 10:54Speaker 2: Yes, critically important for K and N it is scale sensitive.
- 10:58Speaker 1: Okay, break that down. What does scale sensitive mean? Practically?
- 11:02Speaker 2: Imagine you have age maybe zero to one hundred, and
- 11:05Speaker 2: another variable like own scar, which is just zero or one.
- 11:09Speaker 2: When Cainean calculates distance, the age difference will totally swamp
- 11:12Speaker 2: the own scar difference, just because the numbers are so
- 11:14Speaker 2: much bigger.
- 11:15Speaker 1: So age will have way more influence on who's considered nearest.
- 11:18Speaker 2: Exactly, even if owning a car is actually super important
- 11:21Speaker 2: for the prediction. Yeah, so you have to scale your
- 11:23Speaker 2: data first.
- 11:24Speaker 1: How do you scale it?
- 11:25Speaker 2: Common ways are a minmax scaling where you squish everything
- 11:28Speaker 2: into a zero to one range, or standardization where you
- 11:30Speaker 2: give variables a mean of zero and standard deviation of one.
- 11:34Speaker 2: It puts everything on a level playing.
- 11:35Speaker 1: Field, right, So age isn't shouting louder than the other variables.
- 11:38Speaker 1: Makes sense. The example use the turnout data set to
- 11:41Speaker 1: predict if someone voted yeah.
- 11:42Speaker 2: And they specifically mentioned scaling the data first. The model
- 11:46Speaker 2: got almost eighty percent accuracy on training, and importantly, it
- 11:50Speaker 2: held up really well on the test data too.
- 11:51Speaker 1: That consistency is good, right, suggests it's generalizing.
- 11:54Speaker 2: Exactly what you want to see, not just memorizing.
- 11:57Speaker 1: And for predicting income from that same turnout.
- 12:00Speaker 2: The correlation was zer point six y three on training
- 12:03Speaker 2: dropped a bit too. Point four to eight on test,
- 12:05Speaker 2: but the MSc values were very close point zero two
- 12:08Speaker 2: to one for training, point zero two nine for testing.
- 12:11Speaker 1: So again, even if the correlation isn't super strong, the
- 12:14Speaker 1: similar error rates suggest it's performing consistently on new data.
- 12:18Speaker 2: YEP indicates stable performance, which is often more important than
- 12:21Speaker 2: hitting the absolute highest correlation number.
- 12:23Speaker 1: Okay, Next algorithm, Support vector machines SVM. This sounds a
- 12:28Speaker 1: bit more complex.
- 12:29Speaker 2: It combines ideas from K and N and linear models,
- 12:32Speaker 2: but with a really clever twist for classification, which is
- 12:36Speaker 2: its main goal is to find the best dividing line
- 12:38Speaker 2: or plane or hyperplane in higher dimensions.
- 12:41Speaker 1: Hyperplane fancy word for boundary.
- 12:43Speaker 2: Right. It wants the boundary that creates the biggest possible
- 12:47Speaker 2: gap or margin between the different classes.
- 12:50Speaker 1: A bigger buffer zone exactly.
- 12:52Speaker 2: And the data points that sit right on the edge
- 12:54Speaker 2: of that margin. Those are the support vectors. They're the
- 12:57Speaker 2: critical ones that actually define the boundary.
- 12:59Speaker 1: Interesting, does it always have to be a straight line?
- 13:01Speaker 1: What if the data is all mixed up?
- 13:03Speaker 2: Good question. It prefers straight lines, but it has tricks.
- 13:07Speaker 2: It can allow some misclassifications using a slack variable, a
- 13:11Speaker 2: bit of wiggle room, or for really messy nonlinear data.
- 13:15Speaker 2: It uses the kernel trick.
- 13:16Speaker 1: The kernel trick sounds like magic, It kind of is.
- 13:19Speaker 2: It projects the data into much higher dimensional space where
- 13:22Speaker 2: hopefully a simple linear boundary can separate the classes.
- 13:26Speaker 1: Like unfolding a crumpled paper to separate dots. I like
- 13:29Speaker 1: that analogy. So sbms are flexible, can handle messy data.
- 13:33Speaker 2: Very flexible, Yeah, I think can be incredibly accurate even
- 13:36Speaker 2: on complex problems.
- 13:37Speaker 1: Okay, sounds powerful. Downside, is it another black box?
- 13:41Speaker 2: Often? Yes, especially with those kernel tricks and higher dimensions.
- 13:45Speaker 2: Explaining why SBM made a particular decision gets very abstract, very.
- 13:49Speaker 1: Quickly right, hard to explain to the boss.
- 13:51Speaker 2: Can be Also, choosing the right kernel isn't always obvious,
- 13:55Speaker 2: And like K and N, it's scales sensitive. You need
- 13:57Speaker 2: to rescale your data.
- 13:59Speaker 1: Got it scaling again. The example was predicting mortgage status.
- 14:03Speaker 1: Yes know from the working hours data.
- 14:05Speaker 2: Set correct and data prep was key. They combined some
- 14:08Speaker 2: child related variables and rescaled everything. Then they compared two
- 14:11Speaker 2: common kernels, linear and RBF radio basis function.
- 14:15Speaker 1: Oh do they do?
- 14:16Speaker 2: The linear kernel hit eighty seven percent accuracy on training
- 14:20Speaker 2: and impressively held that exact same accuracy on the test data.
- 14:24Speaker 1: Wow, perfect generalization in that case.
- 14:26Speaker 2: Fantastic result. The RBF kernel was just slightly lower and.
- 14:29Speaker 1: For SVM regression predicting education level from the same data set.
- 14:33Speaker 2: Yeah. Again. Comparing linear and RBF kernels, the linear one
- 14:36Speaker 2: showed a pretty weak correlation, only point three to eight
- 14:40Speaker 2: on training and point four zero on tests. That doesn't
- 14:43Speaker 2: sound great, but the MS values were low and very
- 14:47Speaker 2: stable point zero one five eight nine on training, point
- 14:50Speaker 2: zero one eight three to two on testing.
- 14:52Speaker 1: So weak relationship overall, but the model makes consistent predictions exactly.
- 14:57Speaker 2: Suggests it's generalizing well, even if it's not explaining huge
- 15:00Speaker 2: amount of the variants. The source also mentioned outliers might
- 15:03Speaker 2: be affecting the correlation metric more here.
- 15:05Speaker 1: Interesting. So metrics can sometimes tell slightly different stories.
- 15:08Speaker 2: Definitely, you need to look at them together, all right.
- 15:10Speaker 1: Artificial neural networks ann's the brain inspired ones kind of.
- 15:16Speaker 2: They were initially inspired by biological neurons. Yeah. You have inputs,
- 15:20Speaker 2: some processing happens, and you get outputs.
- 15:22Speaker 1: And deep learning is just when you have lots of
- 15:24Speaker 1: layers of these neurons.
- 15:25Speaker 2: Multiple hidden layers. Yes, that's the essence of deep learning.
- 15:28Speaker 1: So how do they actually work? Simply put, think.
- 15:30Speaker 2: Of inputs like signals arriving. Each input gets a weight
- 15:33Speaker 2: how important it is. They get summed up and then
- 15:35Speaker 2: hit an activation function like.
- 15:37Speaker 1: The neuron deciding whether to fire.
- 15:39Speaker 2: Sort of yeah, that function decides if the signal is
- 15:42Speaker 2: strong enough to pass on to the next layer. Usually
- 15:45Speaker 2: the information flows one way feed forward. It's a whole
- 15:49Speaker 2: cascade of these simple weighted sums and activations.
- 15:51Speaker 1: And the big advantage why all the hype, They.
- 15:54Speaker 2: Really shine with massive amounts of data. Given enough data,
- 15:58Speaker 2: they can learn incredibly complex suttle patterns that other algorithms
- 16:02Speaker 2: might miss entirely.
- 16:03Speaker 1: So flexibility is huge. Image recognition, self driving cars.
- 16:07Speaker 2: Exactly, they tower a lot of cutting edge AI tasks.
- 16:11Speaker 1: But the catch they need tons of data.
- 16:13Speaker 2: Typically, yes, massive data sets for optimal performance, and training
- 16:18Speaker 2: them can take a lot of computing power and time.
- 16:21Speaker 2: Plus sometimes simpler networks can struggle to converge, meaning they
- 16:24Speaker 2: don't actually learn effectively.
- 16:25Speaker 1: Okay, example time predicting union membership from the wages data set.
- 16:29Speaker 2: Right, And a key step here was turning categorical variables
- 16:32Speaker 2: like occupation into dummy.
- 16:35Speaker 1: Variables numerical flags basically.
- 16:37Speaker 2: Yep, zeros and ones. The network can understand. The model
- 16:40Speaker 2: achieved a solid seventy percent accuracy, and importantly, it was
- 16:43Speaker 2: consistent between the training and test data.
- 16:46Speaker 1: Good stability again and the regression example predicting wages.
- 16:50Speaker 2: Also from the wages data set, it showed a moderate
- 16:52Speaker 2: correlation point five to six on training, very close point
- 16:56Speaker 2: five to four odd tests, and the MSS values were
- 16:59Speaker 2: also very so between the two.
- 17:00Speaker 1: Sets, So reasonably good generalization there too. Consistent if not
- 17:05Speaker 1: spectacular prediction.
- 17:06Speaker 2: Looks like it reliable performance on new data.
- 17:09Speaker 1: Now for something completely different, k means you said this
- 17:12Speaker 1: one's unsupervised learning.
- 17:13Speaker 2: That's right. This is fascinating because, unlike everything else we've discussed,
- 17:17Speaker 2: there's no right answer or target variable we're trying.
- 17:20Speaker 1: To predict, no gender, no income, no voted label exactly.
- 17:24Speaker 2: KMES isn't trying to predict anything specific. It's just trying
- 17:27Speaker 2: to find natural groupings or clusters within the data itself,
- 17:30Speaker 2: based on similarity.
- 17:32Speaker 1: How does it find these groups?
- 17:33Speaker 2: It starts by randomly guessing the locations of K cluster
- 17:36Speaker 2: centers or centroids, K.
- 17:38Speaker 1: Being the number of clusters you think are in the data.
- 17:41Speaker 2: Precisely. Then it assigns each data point to its nearest centroid.
- 17:46Speaker 2: After that, it recalculates the position of each centroid to
- 17:50Speaker 2: be the actual center of all the points assigned to it.
- 17:52Speaker 1: And it repeats that. Assigned points move centers yep.
- 17:55Speaker 2: It iterates back and forth, assigned points update centroids until
- 17:59Speaker 2: the centroids start moving much until things stabilize, and.
- 18:02Speaker 1: The researcher has to decide on K the number of clusters.
- 18:05Speaker 1: That sounds tricky, it can be.
- 18:06Speaker 2: It's a key challenge.
- 18:07Speaker 1: So what's the big benefit of doing this.
- 18:09Speaker 2: It's fantastic for exploring your data, for segmentation, finding hidden
- 18:13Speaker 2: patterns you didn't even know existed, understanding what makes different
- 18:17Speaker 2: subgroups within your data.
- 18:18Speaker 1: Distinct, discovering natural segments exactly.
- 18:21Speaker 2: But the drawbacks stem from that unsupervised nature. Since there's
- 18:25Speaker 2: no right answer, evaluating how good the clusters are is
- 18:28Speaker 2: more subjective, relying on the researcher's interpretation.
- 18:31Speaker 1: Hell me guess scale sensitive.
- 18:34Speaker 2: You got it requires data normalization or scaling, just like
- 18:37Speaker 2: kNN and SVM because it relies on distance calculations, and yeah,
- 18:43Speaker 2: choosing the right K is tough.
- 18:44Speaker 1: Are there ways to help choose K.
- 18:46Speaker 2: There are methods, yeah, like the elbow method, where you
- 18:48Speaker 2: plot a measure of cluster cohesion against different values of
- 18:51Speaker 2: K and look for an elbow point where adding more
- 18:54Speaker 2: clusters doesn't improve things much.
- 18:56Speaker 1: Okay, the example used the act SAT scores age education.
- 19:03Speaker 2: Yes, and they stressed normalizing the data first, because scores,
- 19:07Speaker 2: age and education level are all on different scales.
- 19:10Speaker 1: Did the elbow method work?
- 19:12Speaker 2: It suggested K two two clusters, but the author actually
- 19:15Speaker 2: decided to go with K three, looking for maybe a
- 19:17Speaker 2: bit more detail in the groupings.
- 19:18Speaker 1: And what did those three clusters reveal.
- 19:21Speaker 2: Pluster zero had the oldest students, most education, highest test
- 19:25Speaker 2: scores generally, Cluster one was kind of medium across the board.
- 19:28Speaker 2: It's second oldest, second highest education, and Cluster two was
- 19:31Speaker 2: much younger with weaker test performance.
- 19:33Speaker 1: Seems logical age and achievement grouping together.
- 19:36Speaker 2: Yeah, But here's the kicker, the really surprising part. When
- 19:39Speaker 2: they looked closer, what did they find? A really clear
- 19:42Speaker 2: separation by gender emerged Cluster one all males, Cluster zero
- 19:48Speaker 2: and two all females.
- 19:49Speaker 1: WHOA, The algorithm wasn't told about gender, right, It just
- 19:53Speaker 1: found that pattern exactly.
- 19:54Speaker 2: It revealed this underlying structure, and it helped explain things
- 19:59Speaker 2: like why cluster one one all males had particularly high
- 20:02Speaker 2: quantitative set scores tying into statistical trends.
- 20:05Speaker 1: That's amazing. So kymines didn't just group by scores. It
- 20:09Speaker 1: uncovered this fundamental demographic split. What does that tell us? Generally?
- 20:13Speaker 2: It shows how these unsupervised methods can reveal really deep insights,
- 20:18Speaker 2: sometimes biases or strong correlations we weren't even looking for.
- 20:22Speaker 2: It forces you to ask why the data is structured
- 20:24Speaker 2: that way.
- 20:24Speaker 1: So the final interpretation was cluster.
- 20:26Speaker 2: Zero older educated women, cluster one young males, Cluster two
- 20:31Speaker 2: very young women, a much richer picture than just high medium,
- 20:33Speaker 2: low scores.
- 20:34Speaker 1: Incredible. Okay, so we've seen all these algorithms, but just
- 20:38Speaker 1: getting an answer isn't enough. We need to know if
- 20:40Speaker 1: the answer is good.
- 20:40Speaker 2: Assessing models absolutely critical, otherwise you're just generating numbers without
- 20:44Speaker 2: knowing if they're meaningful.
- 20:45Speaker 1: And for classification, the go to tool is the confusion matrix.
- 20:49Speaker 2: Definitely a fundamental starting point. It just lays out your
- 20:52Speaker 2: predictions against the actual truth in a simple table.
- 20:55Speaker 1: Remind us of the four boxes you've.
- 20:57Speaker 2: Got true negatives TN correctly saying some thing isn't there.
- 21:00Speaker 2: False negatives FN missing something that is there. Big problem
- 21:04Speaker 2: in medical tests.
- 21:05Speaker 1: For instance, right false positives.
- 21:07Speaker 2: FP seeing something is there when it isn't, like a
- 21:10Speaker 2: spam filter blocking a real email.
- 21:12Speaker 1: And true positives TP.
- 21:14Speaker 2: Correctly identifying something that is there the spam filter catching
- 21:17Speaker 2: actual spam.
- 21:18Speaker 1: So the cancer decision tree example had four ten zero FN,
- 21:22Speaker 1: twenty eight FP, and eighty four TP. Lots of numbers.
- 21:26Speaker 1: If I'm building, say a system to detect faulty products
- 21:29Speaker 1: on an assembly line, which metrics should I care most about?
- 21:32Speaker 2: Ooh, good question. For faulty products, you probably care a
- 21:35Speaker 2: lot about. Recall also called sensitivity. That's true positives divided
- 21:39Speaker 2: by all the actual positives TP plus FN. You want
- 21:42Speaker 2: to catch as many faulty products as possible, even if
- 21:44Speaker 2: you accidentally flag a few good ones. Missing a faulty
- 21:47Speaker 2: one could be costly or dangerous.
- 21:49Speaker 1: So recall is about minimizing misses. For the cancer example,
- 21:52Speaker 1: recall was point seventy six yes.
- 21:55Speaker 2: And precision, which is true positives divided by all the
- 21:59Speaker 2: ones the model predicted is positive TP plus FP was
- 22:02Speaker 2: also point seven to six. Precision is about how trustworthy
- 22:07Speaker 2: A positive prediction is when the model says it's cancer.
- 22:10Speaker 2: How often is it right? And the F measure that
- 22:12Speaker 2: just combines precision and recall into one score also point
- 22:15Speaker 2: seventy six in this case because there were zero false negatives,
- 22:18Speaker 2: which is unusual.
- 22:19Speaker 1: What about the negative specificity?
- 22:21Speaker 2: Specificity is true negatives divided by all the actual negatives
- 22:24Speaker 2: tmplus FP. It measures how well the model identifies the
- 22:27Speaker 2: true negatives. For the cancer example, it was only point
- 22:30Speaker 2: one three, quite poor, meaning it wasn't good at correctly
- 22:33Speaker 2: identifying healthy people as healthy.
- 22:35Speaker 1: Okay, so different metrics matter depending on the goal. What
- 22:38Speaker 1: about plain old accuracy?
- 22:40Speaker 2: Accuracy is just tmplus TP divided by the total all
- 22:44Speaker 2: the correct predictions. It was seventy six percent here. But
- 22:47Speaker 2: accuracy can be misleading, especially if one class is way
- 22:50Speaker 2: more common.
- 22:50Speaker 1: Than the other, like if ninety nine percent of emails
- 22:53Speaker 1: aren't spam. A model predicting not spam all the time
- 22:56Speaker 1: gets ninety nine percent accuracy, but is useless Exactly.
- 23:00Speaker 2: That's why you need these other metrics. An error is
- 23:02Speaker 2: just one minus accuracy, so twenty four percent here.
- 23:05Speaker 1: And finally, kappa.
- 23:06Speaker 2: Kappa measures accuracy but accounts for how much agreement you'd
- 23:09Speaker 2: expect just by chance, closer to one is better. Here
- 23:13Speaker 2: was point one seven to one, which is pretty low.
- 23:16Speaker 2: Suggests the model's performance wasn't much better than random guessing
- 23:19Speaker 2: once you factor chance in. Wow.
- 23:21Speaker 1: Okay, so looking at all of them gives a much
- 23:23Speaker 1: fuller picture.
- 23:24Speaker 2: Definitely.
- 23:25Speaker 1: Now the ROC curve this is a cool backstory. Wwii.
- 23:29Speaker 2: Yeah. Radar engineers used it to figure out if a
- 23:32Speaker 2: blip was an enemy plane a true positive or just
- 23:35Speaker 2: noise a false positive. High stake stuff.
- 23:37Speaker 1: Now we use it for models.
- 23:39Speaker 2: Yep. It plots the true positive rate against the false
- 23:41Speaker 2: positive rate at various thresholds. Ideally, you want the curve
- 23:45Speaker 2: to shoot up towards the top left corner high true positives,
- 23:48Speaker 2: low false positives.
- 23:50Speaker 1: A diagonal line is bad like random guessing.
- 23:52Speaker 2: Exactly the area under this curve AUC gives a single number,
- 23:56Speaker 2: zero to one. The cancer model had an AEC of
- 23:59Speaker 2: zero point seven nine, which is generally considered acceptable. Maybe
- 24:02Speaker 2: fairy good okay.
- 24:03Speaker 1: Another key technique cross validation. This is for checking generalizability.
- 24:08Speaker 2: Right. Instead of just one train test split, you divide
- 24:10Speaker 2: your training data into say five or ten folds like slices. Yeah.
- 24:14Speaker 2: Then you train the model five times. Each time, you
- 24:17Speaker 2: train on four folds and test on the one fold
- 24:19Speaker 2: left out.
- 24:20Speaker 1: Using a different slice for testing each time exactly.
- 24:23Speaker 2: Then you average the results from those five tests. It
- 24:26Speaker 2: gives you a much better idea of how stable the
- 24:28Speaker 2: performance is and how well it's likely to perform on
- 24:30Speaker 2: genuinely new data.
- 24:32Speaker 1: And the real test set, the one you held back
- 24:34Speaker 1: at the start.
- 24:35Speaker 2: Crucially, you only touch that once, right at the very end,
- 24:38Speaker 2: after you've done all your model selection and tuning, using
- 24:41Speaker 2: cross validation on the training data, don't peak.
- 24:43Speaker 1: Got it? For the cancer decision tree, cross validation showed
- 24:47Speaker 1: about seventy three percent accuracy, but with a standard deviation
- 24:51Speaker 1: of eleven percent.
- 24:52Speaker 2: Yeah, that standard deviation tells you there was quite a
- 24:54Speaker 2: bit of variability and performance across the different folds. Maybe
- 24:57Speaker 2: do the small sample size making the full quite different
- 25:00Speaker 2: from each other.
- 25:01Speaker 1: Okay, shifting to assessing regression models predicting numbers. No confusion
- 25:06Speaker 1: matrix here, right.
- 25:07Speaker 2: Right, we use different metrics. You'd start by comparing basic dats,
- 25:10Speaker 2: mean standard deviation, core tiles of the actual values versus
- 25:14Speaker 2: your predicted values. Ideally they should look pretty similar.
- 25:17Speaker 1: Like for the age prediction the means were close, but
- 25:20Speaker 1: standard deviations.
- 25:21Speaker 2: Differed a bit. Hey exactly. Then you look at the
- 25:23Speaker 2: correlation between actual and predicted for age that was point
- 25:27Speaker 2: five to four on the training set.
- 25:29Speaker 1: We also saw means squared error MS.
- 25:31Speaker 2: Lower is better, lower is better, Yes, for age, it
- 25:35Speaker 2: was sixty one point eight on train eighty five point
- 25:37Speaker 2: twenty four on test. The different shows that drop in
- 25:40Speaker 2: performance on unseen data and are squared. Our squared tells
- 25:43Speaker 2: you how much of the variation in the outcome variable
- 25:45Speaker 2: your model explains. Ranges from zero to one. For age prediction,
- 25:49Speaker 2: that was point twenty nine, which the source called not exciting.
- 25:52Speaker 2: It means the model only explained about twenty nine percent
- 25:55Speaker 2: of the variation in age.
- 25:56Speaker 1: And cross validation applies here too.
- 25:58Speaker 2: Absolutely, you'd cross validate metrics like MSE and R two
- 26:01Speaker 2: for that SVM regression predicting education. The cross validated MS
- 26:05Speaker 2: was similar to the original test MAC, which is good,
- 26:08Speaker 2: But the R two, the cross validated R two had
- 26:11Speaker 2: a high standard deviation point one nine. So I guess
- 26:14Speaker 2: the model's ability to explain the variance wasn't very stable
- 26:16Speaker 2: across different subsets of the data. Again, maybe sample size
- 26:19Speaker 2: issues or just a complex relationship.
- 26:21Speaker 1: Okay, we've built models, we've assessed them now making them better.
- 26:25Speaker 1: We know we can get more data, change variables, switch algorithms,
- 26:29Speaker 1: But what about tuning.
- 26:31Speaker 2: Ah hyper parameter tuning. This is really interesting. It's about
- 26:35Speaker 2: tweaking the settings of the algorithm itself before it starts learning.
- 26:39Speaker 1: So not changing the data, but changing how the algorithm
- 26:42Speaker 1: learns from the data exactly.
- 26:44Speaker 2: Things like the K and kN N, or the C
- 26:47Speaker 2: penalty termin SVM, or how deep you let a decision
- 26:50Speaker 2: tree grow. These are hyper.
- 26:52Speaker 1: Parameters, and how do you tune them?
- 26:54Speaker 2: Guessing more systematic than that, Usually you define a grid
- 26:58Speaker 2: of possible values for the hyper parameters you want to tune,
- 27:01Speaker 2: like Trika values from one to twenty, try different distance.
- 27:04Speaker 1: Metrics, and the computer tries all the combinations.
- 27:06Speaker 2: YEP, often using cross validation within the grid search. It
- 27:09Speaker 2: runs models for all combinations and tells you which set
- 27:12Speaker 2: of hyper parameters gave the best average performance on the
- 27:15Speaker 2: cross validation folds. It's a bit a trial and error
- 27:18Speaker 2: guided by data.
- 27:19Speaker 1: Let's look at the kNN tuning example. Original model predicting
- 27:22Speaker 1: voted was around seventy nine percent accurate.
- 27:25Speaker 2: Right, the tuned K one thirteen the weights uniform versus
- 27:29Speaker 2: distance based and the distance metric Manhattan versus Kowski clidion.
- 27:33Speaker 2: That created ninety six different kNN models to test.
- 27:36Speaker 1: Wow, and the best one.
- 27:38Speaker 2: The best combination found through cross validation, achieved seventy four
- 27:42Speaker 2: percent accuracy.
- 27:43Speaker 1: That's lower than the original seventy nine percent.
- 27:45Speaker 2: On cross validation. Yes, but here's the crucial part. When
- 27:49Speaker 2: they tested that tuned model back on its own training data,
- 27:52Speaker 2: its accuracy jumped to ninety nine point seven percent.
- 27:55Speaker 1: WHOA nearly perfect. But that sounds suspicious.
- 27:59Speaker 2: I really suspicion. That huge gap between the near perfect
- 28:02Speaker 2: training accuracy and the seventy four percent cross validated accuracy
- 28:05Speaker 2: screams overfitting. The tuning process found settings that basically memorize
- 28:10Speaker 2: the training data.
- 28:11Speaker 1: So this is a massive warning sign about just looking
- 28:13Speaker 1: at training accuracy, especially after tuning.
- 28:15Speaker 2: Absolutely reinforces why cross validation and that final untouched test
- 28:19Speaker 2: set are non negotiable.
- 28:20Speaker 1: Okay, and the SVM regression tuning predicting education original mESC
- 28:24Speaker 1: was about point zero one five eight nine.
- 28:26Speaker 2: They tune the c's cost parameter, the kernel type linear
- 28:29Speaker 2: versus RBF and degree for polynomial kernels and the result
- 28:32Speaker 2: the best combo nudged the MSE down slightly to point
- 28:36Speaker 2: zero one, five, three, eight.
- 28:37Speaker 1: A tiny improvement worth it.
- 28:39Speaker 2: Depends entirely on the context. Sometimes shaving even a tiny
- 28:42Speaker 2: bit off the air can be hugely valuable. It shows
- 28:46Speaker 2: the potential, even if it's not always dramatic.
- 28:48Speaker 1: Finally, taking it one step further, combining algorithms the ensemble
- 28:52Speaker 1: approach or stacking.
- 28:54Speaker 2: Yes, stacking is a pretty sophisticated way to combine models.
- 28:58Speaker 2: The basic idea is you train several different models, like
- 29:01Speaker 2: a random forest, a SVM, a kNN okay. Then instead
- 29:05Speaker 2: of just averaging their outputs, you use their predictions as
- 29:07Speaker 2: inputs for a final metal model.
- 29:10Speaker 1: So a model that learns from the predictions of other.
- 29:12Speaker 2: Models exactly, it learns how to best combine the strengths
- 29:15Speaker 2: and potentially correct the weaknesses of the base models to
- 29:18Speaker 2: make a final, hopefully better prediction.
- 29:20Speaker 1: Trying to create a supermodel.
- 29:21Speaker 2: That's the goal maximize strengths, minimize weaknesses.
- 29:24Speaker 1: The example used the doctor Aus data set again predicting gender,
- 29:28Speaker 1: combining random forest, SVM, and kNN YEP individually.
- 29:33Speaker 2: Cross validation showed random forest was the best performer on
- 29:36Speaker 2: its own well, SVM struggled a.
- 29:37Speaker 1: Bit, So did stacking them beat the random forest?
- 29:41Speaker 2: Interestingly? No, No, the initial stacked model actually performed worse
- 29:46Speaker 2: than just using the random forest by itself.
- 29:48Speaker 1: Huh So more complex isn't always better?
- 29:51Speaker 2: Definitely not automatically. Just throwing models together can sometimes add
- 29:55Speaker 2: noise or compound errors.
- 29:56Speaker 1: Did they try tuning the models within the stack?
- 30:00Speaker 2: Did they tuned hyper parameters for the random forest, the SVM,
- 30:04Speaker 2: and the kN N within the ensemble.
- 30:06Speaker 1: Structure and the final result after all that work, The.
- 30:09Speaker 2: Final tuned ensemble model landed at about seventy percent accuracy
- 30:13Speaker 2: on both training and test data.
- 30:15Speaker 1: So after all that stacking and tuning, it didn't really
- 30:19Speaker 1: improve much, if at all, over the simpler single random
- 30:22Speaker 1: forest model.
- 30:23Speaker 2: Pretty much in this specific case, the added complexity didn't
- 30:26Speaker 2: yield significantly better performance.
- 30:28Speaker 1: That's a really important takeaway, A huge one. Wow. Okay,
- 30:31Speaker 1: we've covered a lot of ground today, from decision trees
- 30:33Speaker 1: kind of the building block.
- 30:34Speaker 2: To random forests using the wisdom of the.
- 30:36Speaker 1: Crowd, kNN using proximity, SVM finding that optimal boundary an
- 30:42Speaker 1: n's sort of mimicking the brain, then.
- 30:44Speaker 2: K meanes finding hidden groups without even being told what
- 30:47Speaker 2: to look.
- 30:48Speaker 1: For and crucially, how to actually measure if these models
- 30:51Speaker 1: are any good using things like the confusion matrix, RC curves,
- 30:55Speaker 1: cross validation.
- 30:57Speaker 2: And how to potentially improve them through careful hyper parameter
- 31:00Speaker 2: tuning and even stacking them together.
- 31:02Speaker 1: And remember, listener, these aren't just you know, abstract ideas.
- 31:06Speaker 1: This deep dive should give you a real foundation for
- 31:08Speaker 1: understanding how these tools work, yeah.
- 31:10Speaker 2: Their quirks, their strengths, their weaknesses, and importantly, how to
- 31:14Speaker 2: think critically about their results and choose the right approach
- 31:18Speaker 2: for your specific problem.
- 31:19Speaker 1: And that final point from the ensemble example really sticks
- 31:22Speaker 1: with me. As the source material concludes, and this is powerful.
- 31:26Speaker 1: Complexity is not a cure all for better performance, and
- 31:30Speaker 1: simple is almost always better.
- 31:32Speaker 2: It's a fantastic lesson, it really is.
- 31:33Speaker 1: And it leaves us with a question for you to
- 31:35Speaker 1: maybe mull over in whatever you're working on, whether it's
- 31:38Speaker 1: data or just a life, where might you be over
- 31:41Speaker 1: complicating things? Where could a simpler approach actually lead to clearer,
- 31:46Speaker 1: more powerful insights.
- 31:47Speaker 2: Something to think about.