Classical Machine Learning Classification


Classical Machine Learning Classification

Anna
Welcome to our lesson on classical machine learning, and specifically classification. Last time, the focus was regression: using a model to predict a numerical value for a new data point. Classification is different. It predicts which category a data point belongs to.

Simon
So instead of asking, “What number should the model predict?” we might ask, “Is this penguin male or female?” That’s the example we’ll use with the Palmer Penguins dataset, which contains three penguin species living on three Antarctic islands.

Anna
The general workflow is to prepare the data, split it into training and testing sets, train a model, and then evaluate how well it predicts. A common split is about 80 percent for training and 20 percent for testing. The training data teaches the model; the held-out testing data lets us check its performance on data it hasn’t trained on.

Simon
The workshop uses Python tools, especially pandas for working with data and scikit-learn for machine-learning models. It also uses libraries such as NumPy, Matplotlib, and Seaborn for numerical work and visualization. The data is loaded from a CSV file into a pandas data frame.

Anna
Before fitting anything, we need to inspect and clean the data. The dataset starts with 344 rows and seven columns: species, island, bill length, bill depth, flipper length, body mass, and sex. Some rows have missing values in measurements or sex, so the lesson checks the data frame’s information and missing-value counts, then removes rows containing missing values.

Simon
After that, it finds one remaining row where sex is recorded as a dot rather than a usable category, and filters that row out too. The result is a clean dataset with 333 rows. Checking counts is useful here: after removing missing entries, the species counts are 146 Adelie, 120 Gentoo, and 68 Chinstrap; the island counts are 164, 123, and 47; and the sex counts are 168 male and 165 female.

Anna
Next comes categorical encoding. The species, island, and sex columns contain labels rather than numbers. The workshop uses a label encoder to assign numeric codes: three categories become 0, 1, and 2, while two categories become 0 and 1. That gives the model numeric inputs and a numeric target.

Simon
The lesson also checks for outliers in the four measured physical features. An outlier is an unusually extreme value, but it isn’t automatically a mistake; it might reflect an error, or it might be meaningful, so it deserves investigation. The method shown uses quartiles and the interquartile range, or IQR, to define inner fences at Q1 minus 1.5 times the IQR and Q3 plus 1.5 times the IQR, and outer fences at Q1 minus 3 times the IQR and Q3 plus 3 times the IQR.

Anna
In this dataset, the checks find no outliers in bill length, bill depth, flipper length, or body mass. The notebook also plots histograms and density estimates for these measurements, then separates the distributions by species. Those plots help us see how the measurements are distributed and compare the three species.

Simon
Now we can define the prediction task. The features, conventionally called X, are bill length, bill depth, flipper length, body mass, island, and species. The target, called y, is sex. So the model’s job is to use those six features to predict whether a penguin is male or female.

Anna
The data is split into 266 training rows and 67 testing rows, corresponding to roughly 80 and 20 percent. The example sets the random state to 42 so that the random split can be reproduced. That’s handy when you want to rerun the notebook and get the same partition.

Simon
The numerical features are then standardized with scikit-learn’s StandardScaler. Standardization puts features on a comparable scale by centering them around zero and scaling them to have a standard deviation of one. That matters here because body mass is in the thousands, while other measurements are much smaller; without scaling, differences in units could affect some algorithms.

Anna
The notebook demonstrates three classifiers. The first is k-nearest neighbors, configured with three neighbors. The second is a decision tree with a maximum depth of three. The third is a support vector classifier with a linear kernel and probability estimates enabled. Each model is fitted on the training features and training sex labels, then used to predict sex for the test features.

Simon
And predictions won’t always match the actual test labels, so we need evaluation measures. One useful starting point is the confusion matrix, which compares predicted categories with actual categories. For a binary classification, it organizes outcomes as true positives, false positives, true negatives, and false negatives.

Anna
A true positive is a positive case correctly predicted as positive; a true negative is a negative case correctly predicted as negative. A false positive is actually negative but predicted positive, and a false negative is actually positive but predicted negative. The distinction matters because different applications may care more about one kind of mistake than another.

Simon
The confusion matrices for the three models make those mistakes visible, and a Seaborn heatmap makes the counts easier to read. For the nearest-neighbors model, the matrix shows 32 true positives, 5 false negatives, 2 false positives, and 28 true negatives. The decision tree gives 31, 6, 5, and 25 in those same categories; the support vector classifier gives 33, 4, 3, and 27.

Anna
Those counts can be turned into several metrics. Precision asks, among the cases predicted positive, what fraction really were positive: true positives divided by true positives plus false positives. Recall asks, among all actual positive cases, what fraction did the model find: true positives divided by true positives plus false negatives.

Simon
The classification report also gives the F1 score, which combines precision and recall, along with support, the number of actual examples in each class, and overall accuracy. For the nearest-neighbors model, the report gives precision of about 0.94 for one class and 0.85 for the other, recall of about 0.86 and 0.93, and F1 scores of about 0.90 and 0.89. Its overall accuracy is about 0.90 on the 67 test examples.

Anna
The report may also show macro and weighted averages. And there are many other measures available, including specificity, the Jaccard index, and different forms of accuracy. The key point is that there isn’t one metric that is automatically best for every task. Choose measures that answer the questions that matter in your research field or application.

Simon
Another evaluation tool in the lesson is the ROC curve, or receiver operating characteristic curve. It’s used here for binary classification and plots the true-positive rate against the false-positive rate. The true-positive rate is true positives divided by all actual positives; the false-positive rate is false positives divided by all actual negatives.

Anna
The area under the ROC curve summarizes the curve’s performance. In the lesson’s explanation, an area of 0.5 corresponds to random classification, while an area of 1 represents perfect separation. The three models score about 0.94 for nearest neighbors, 0.93 for the decision tree, and 0.91 for the support vector classifier.

Simon
So, among these three models and using this ROC-area comparison, nearest neighbors is the top choice. But that doesn’t mean it will always be the best classifier for every dataset or goal. It’s a comparison for this particular task and evaluation.

Anna
The example predicts one of two categories, sex, so it’s binary classification. If instead the target were species, there would be three categories, and the confusion matrix would be three by three rather than two by two. The lesson also points to one-hot encoding as another encoding approach to explore, alongside label encoding.

Simon
The overall lesson is a reusable process: clean and inspect the data, encode categories, examine distributions and possible outliers, separate features from the target, split the data, scale the numerical features, train classifiers, and compare their predictions with appropriate metrics. Scikit-learn offers many classifiers and evaluation measures, so the choice depends on the problem and what you need to learn from the results.

Anna
And next time, the workshop moves toward unsupervised learning and dimensionality reduction: asking whether a dataset with many variables can be represented more simply, perhaps with two dimensions, and which information is most useful to retain. For today, we’ve followed the full classification example, from raw penguin data to comparing three models.

Ask anything
We've read this whole study set, so questions about it get the best answers.