Simple way to find a suitable algorithm for your data in scikit-learn (Python)
By: Zolzaya Luvsandorj
Tags: Getting Started, Data Science, Supervised Learning, Machine Learning, Python

1. Prepare data 📦

If you want to follow along with the code on your computer, make sure you have numpy, pandas, seaborn, sklearn and xgboost installed.

Let’s imagine we want to find a suitable machine learning algorithm for a classification problem. For our example, we will use a subset of features from titanic dataset. Let’s import relevant packages and sample data:

Let’s partition the data into train and test set. We will also define feature groups which will be useful for preprocessing the data later:

We will now prepare a few custom transformers to preprocess the data:

◼ ️️ Imputer : Imputes with a constant value and returns the imputed data in a pandas DataFrame

◼ CardinalityReducer : Aggregates infrequent categories into ‘other’ category and returns the transformed data in a pandas DataFrame

You will notice that most custom functions or methods defined in this post return DataFrames as they are easier to look at.

Now, let’s transform the data. We will preprocess the numerical and categorical features in parallel. Using ColumnTransformer and Pipeline , we will:

split the data into two groups: categorical and numerical

apply different sets of transformers to each group

paste the results together

If this is your first time seeing ColumnTransformer and/or Pipeline , the code below may not be easy to understand. If you want to learn more about them, I have dedicated a separate post to explain these two.

partial output

2. Assess multiple algorithms 🔧

Now, we have arrived at the key section of this post. Using your knowledge about the data and modelling techniques, you can choose a set of potential candidates to try on your data. For instance, exploratory data analysis may have hinted some directions on what kind of algorithm could potentially work well on the data. In addition, your theoretical knowledge of machine learning algorithms can also guide you. Cheat sheets likes this may come in handy too.

Once you have a set of algorithms to try, you could try each algorithm on the data one by one. However, using a few functions, we can keep this process more organised. The idea of these functions is inspired from here.

We will need to define all the algorithms we decided to try on the data inside create_baseline_classifiers function. In this example, we are using selected classifiers out of the box without any hyperparameter tuning. However, you could tweak hyperparameters at this stage if you desire. We have included dummy classifier here as a benchmark:

assess_models() is currently defined in a way such that it can create performance summary on multiple metrics. However, you could also include only one metric if you prefer to. Alternatively, we will shortly look at how to extract single metric performance from the output. Now, it’s time to assess the models on the training data and compare their result:

Using the third function we created, let’s compare all the models according to an individual metric: Area under the ROC curve.

It’s quite a useful summary, isn’t it? Looking at mean performance alone is not sufficient. Hence, we have included the other columns here to give us a more detailed information on the performance across folds. After analysing the output, we should shortlist and direct our efforts on fine tuning only one or if you desire, a selected top few algorithms.

This approach can also be modified with little effort to regression problems. For instance, here’s an example function that creates multiple regressors:

I find these functions to be useful for data science projects! ⭐️ I hope you do too.