A Beginners Guide to Scikit-Learn | by Rebecca Vickery | Towards Data Science

Scikit-learn, first developed as a Google Summer of Code project in 2007, is the now widely considered to be the most popular Python library for machine learning.
There are a number of reasons why this library is seen as one of the best choices for machine learning projects, especially in production systems. These include, but aren’t limited to the following.
It has a high level of support and strict governance for the development of the library which means that it is an incredibly robust tool.
There is a clear, consistent code style which ensures that your machine learning code is easy to understand and reproducible, and also vastly lowers the barrier to entry for coding machine learning models.
It is widely supported by third-party tools so it is possible to enrich the functionality to suit a range of use cases.
If you are learning machine learning then Scikit-learn is probably the best library to start with. Its simplicity means that it is fairly easy to pick up and by learning how to use it you will also gain a good grasp of the key steps in a typical machine learning workflow.
The following article is a beginner-friendly introduction to the tool and should give you enough of an understanding to be able to develop a simple machine learning model.
To install the latest version of Scikit-learn run the following.
pip install scikit-learn
The code examples throughout this article are run on the classic wine data set which can be imported directly from the Scikit-learn API.
The Scitkit-learn library provides a very large variety of pre-built algorithms to perform both supervised and unsupervised machine learning. They are generally referred to as estimators.
The estimator you choose for your project will depend on the data set you have and the problem that you are trying to solve. The Scikit-learn documentation helpfully provides this diagram, shown below, to help you to determine which algorithm is right for your task.
What makes Scikit-learn so straight forward to use is that regardless of the model or algorithm you are using, the code structure for model training and prediction is the same.
To illustrate this let’s run through an example.
Let’s say you are working on a regression problem and want to train a linear regression algorithm and use the resulting model to make predictions. The first step, with Scikit-learn, is to call the logistic regression estimator and save it as an object. The example below calls the algorithm and saves it as an object called lr.
The next step is to fit the model to some training data. This is performed using the fit() method. We call lr.fit() on the features and target data and save the resulting model as an object called model. In the example below I am also using the train_test_split() method to split the dataset into test and train data.
Next, we use the model and the predict() method to predict on previously unseen data.
If we were to now use Scitkit-learn to perform a different task, say for example, we wanted to train a random forest classifier. The code would look very similar and have the same number of steps.
This consistent code structure makes developing machine learning models extremely straight forward and also produces code that is highly readable and reproducible.
In most real-world machine learning projects the data that you use will not necessarily be ready to train a model. It is highly likely that you will first need to perform some data preprocessing and transformation steps such as handling missing values, transforming categorical data into numeric or applying feature scaling.
Scikit-learn has built-in methods to perform these preprocessing steps. For example, the SimpleImputer() fills in missing values using a method of your choice.
The Scikit-learn documentation lists the full options for data preprocessing here.
Once a model has been trained you need to measure how good the model is at predicting on new data. This step is known as model evaluation and the metric that you choose will be determined by the task you are trying to solve. For example, typically in a regression problem, you might choose RMSE whilst for classification you might choose the F1-score.
All estimators include a score() method which returns a default metric that is most relevant to the machine learning task they perform.
Scikit-learn additionally has a set of metric functions that provide a more detailed evaluation for a model. As an example, for classification tasks, the library has a classification report which provides precision, recall, F1-score and overall accuracy.
The classification report code and output is shown below.
All estimators in the Scikit-learn library contain a range of parameters, for which there are multiple options. The values that you choose for a particular algorithm will impact how well the final model performs. For example, with the RandomForestClassifier you can set the max_depth of the tree to potentially any value, and depending on your data and task, different values for this parameter will produce different results.
This process of trying different combinations of parameters to find the optimal combination is known as hyperparameter optimisation.
Scikit-learn provides two tools to automatically perform this task, GridSearchCV which implements a technique known as exhaustive grid search and RandomizedSearchCV which performs randomized parameter optimisation.
The below example uses GridSearchCV to find the optimal parameters for the RandomForestClassifier. The output is shown below the code.
The Scikit-learn package provides a further convenient form of code encapsulation in the form of pipelines. This tool enables all preprocessing tasks to be chained together with the classifier step so that simply calling fit() or predict() on a single pipeline object performs all the steps in your workflow.
This makes for highly readable code and reduces repetition of steps in your machine learning workflow.
To create a pipeline we first define the steps in the object I have called pipe in the code below. We can then simply call fit on this object to train the model. The pipeline object can also be used to make predictions on new data.
If you enjoyed this article and would like to explore the Scikit-learn library further you might like to read my earlier posts shown below.
medium.com
towardsdatascience.com
Thanks for reading!
I send out a monthly newsletter if you would like to join please sign up via this link. Looking forward to being part of your learning journey!