A linear classification model is a powerful tool in data science, aiming to categorize or classify data points into distinct classes based on their features. Using linear equations, these models separate data points by drawing straight lines (in 2D) or planes (in higher dimensions). This post will guide you through the steps to build one using scikit-learn. You can also follow along with this Colab notebook.

We'll be using the Iris plants dataset, which contains 3 classes with 50 instances each. Each class refers to a type of iris plant, and one class if linearly separable from the other two, which are not linearly separable from one another. There are four input variables: sepal length in cm, sepal width in cm, petal length in cm, and petal width in cm.

## Setting Up and Loading Data

To begin, we must first import the necessary libraries and load our dataset:

## Visualizing the Data with a 2D Chart

Often, visualizing data can offer insights that raw numbers or descriptions fail to provide. For our Iris dataset, let's plot a 2D chart of the first two features, i.e., sepal length and sepal width, and color each data point based on its class. This visualization will give us an idea of how these two features can differentiate between the Iris classes.

This scatter plot reveals how the sepal length and sepal width features relate to the different Iris classes. While it's evident that one class is quite distinct, the other two show some overlap, affirming that they're not linearly separable from one another.

## Splitting Data and Standardizing Numerical Feature Values

First, we need to split our data into a dataset of training examples and a dataset of test examples. This split will later help us evaluate the performance of our model on unseen examples that we didn't use to train the model.

For the purposes of this tutorial, we're going to just use the sepal length and sepal width features:

Data standardization is essential for algorithms that rely on the scale or magnitude of features. It ensures all numerical features have a similar scale, which we will implement by subtracting the mean and dividing by the std of each feature. This ensures that each feature has a zero mean and unit variance:

There are two things worth noting here:

**Fitting the transformation**: Always fit the transformation using only the training data. This mimics the real-world application where new, unseen data doesn't influence our scaling. Remember, when predicting on fresh data, you'll first apply the same transformation based on the training data's statistics.**Standardization benefits**: Standardizing our input features ensures that all feature are on a similar scale, which can help both training and interpretability. If we don't scale our features, those with larger magnitudes can disproportionately influence the model, particularly for gradient based methods. Standardization also ensures that coefficient magnitudes more directly indicate their importance with respect to the predicted output. Without it, coefficients could be skewed by the varying scales of the features, making it difficult to compare their significance accurately.

## Training The Linear Classifier via SGD

With our data prepped and ready to go, let's create and train the linear classifier:

We are using Stochastic Gradient Descent (SGD) to train our model. In simple terms, SGD tweaks the model's linear coefficients iteratively by assessing the direction (gradient) that minimizes the prediction error (loss) for each training example. By default, we're using hinge loss, but there are a variety of different configuration options. For a deep dive into available parameters for the model we're using, refer to the SGDClassifier documentation.

## Understanding Feature Contributions and Visualizing Decision Boundaries

Although most real-world problems are not linear, fitting a linear classification model to your data is a great first step when modeling your data because it is simple and easy to interpret. By examining the model's coefficients, we can better understand the weight or importance of each feature for each class:

You'll notice that there is a line for each class. This is because the model has learned a linear decision boundary for each class vs. the remaining classes. We can better understand exactly how this is working by visualizing the decision boundaries for each class once again using sepal length and width as our features for the visualization:

As expected, the classifier learned a decision boundary for setosa that perfectly separates the class. The other two decision boundaries have some points from the other class on the wrong side, which makes sense because the two classes are not linearly separable.

## Generate Test Predictions and Measure Performance

Now that we have a trained model, we can use it to generate predictions for our test set. We can then evaluate the performance of our model by comparing our predictions to the actual labels. One of the simplest performance metrics is accuracy, which measures the proportion of correctly classified instances:

For classification tasks, the confusion matrix is another valuable tool for understanding model performance by breaking it down into the following four categories:

**True Positives (TP)**: The model correctly predicted the positive class.**True Negatives (TN)**: The model correctly predicted the negative class.**False Positives (FP)**: The model incorrectly predicted the positive class.**False Negatives (FN)**: The model incorrectly predicted the negative class.

For multi-class classification problems like the Iris dataset, the matrix is extended to represent each class against the others:

## K-Fold Cross-Validation

When we first split our dataset, we randomly sectioned off a portion of the data to use as test data; however, this means that our measured performance is dependent on this random split and not particularly stable. Cross-validation is a technique to more robustly assess the performance of the model by dividing the dataset into K different subsets (or "folds"). The model is trained on all but one of the K folds and tested on the remaining fold. This process is then repeated K times, each time with a different fold as the test set.

Here are two reasons why cross-validation is useful:

**Mitigating Overfitting**: By training and testing on different data splits, you ensure the model doesn't overly adapt to a single train-test split.**Better Utilization of Data**: Since each data point gets to be in the test set exactly once, we're making the most of the available data.

We will use 5-fold cross-validation:

## Conclusion

And that's it! We've trained a linear classification model using scikit-learn on the Iris plants dataset. We've also covered some essential methods for evaluating and visualizing your results. Knowing how to train linear models and analyze them is a foundational skill for any data scientist or machine learning engineer.

But don't stop here! Take this newfound knowledge and apply it to other datasets. Challenge yourself by tweaking parameters, exploring other algorithms, or delving into more advanced topics. And if you found this guide helpful, consider sharing it or leaving a comment (link to Medium for commenting). We love hearing from readers and are eager to know how you're putting these skills to use in your projects!

*If you want to comment on the post, you can **comment here**!*