Photo by deep mind in Unsplash
It’s pretty obvious that ML teams developing new models or algorithms expect optimal model performance on test data.
But many times that just doesn’t happen.
The reasons can be many, but the main culprits are:
- Lack of sufficient data
- poor quality data
- overfitting
- subequipment
- Bad choice of algorithm.
- Hyperparameter tuning
- Bias in the data set
However, the above list is not exhaustive.
In this article, we will discuss the process that can solve multiple issues mentioned above and ML teams should be very careful when executing it.
It is data preprocessing.
It is widely accepted in the machine learning community that data preprocessing is an important step in the ML workflow and can improve model performance.
There are many studies and articles that have shown the importance of data preprocessing in machine learning, such as:
“A study by Bezdek et al. (1984) found that data preprocessing improved the accuracy of various clustering algorithms by up to 50%.”
“A study by Chollet (2018) found that data preprocessing techniques such as normalization and data augmentation can improve the performance of deep learning models.”
It is also worth mentioning that preprocessing techniques are not only important to improve model performance, but also to make the model more interpretable and robust.
For example, handling missing values, removing outliers, and scaling the data can help prevent overfitting, which can lead to models that generalize better to new data.
In either case, it is important to note that the specific preprocessing techniques and extent of preprocessing required for a given data set will depend on the nature of the data and the specific requirements of the algorithm.
It is also important to note that in some cases, data pre-processing may not be necessary or may even harm model performance.
Preprocessing data before applying it to a machine learning (ML) algorithm is a crucial step in the ML workflow.
This step helps ensure that the data is in a format that the algorithm can understand and that it does not have errors or outliers that could negatively affect the performance of the model.
In this article, we’ll discuss some of the benefits of data preprocessing and provide a code example of how to preprocess data using the popular Python library, Pandas.
One of the main advantages of data preprocessing is that it helps improve model accuracy. By cleaning and formatting the data, we can ensure that the algorithm only considers relevant information and is not influenced by any irrelevant or incorrect data.
This can lead to a more accurate and robust model.
Another benefit of data preprocessing is that it can help reduce the time and resources required to train the model. By removing irrelevant or redundant data, we can reduce the amount of data the algorithm needs to process, which can greatly reduce the amount of time and resources required to train the model.
Data preprocessing can also help avoid overfitting. Overfitting occurs when a model is trained on a data set that is too specific, and as a result performs well on the training data but poorly on new, unseen data.
By preprocessing the data and removing irrelevant or redundant information, we can help reduce the risk of overfitting and improve the model’s ability to generalize to new data.
Data preprocessing can also improve the interpretability of the model. By cleaning and formatting the data, we can make it easier to understand the relationships between different variables and how they influence model predictions.
This can help us better understand the behavior of the model and make more informed decisions about how to improve it.
Example
Now, let’s look at an example of data preprocessing using Pandas. We will use a dataset that contains information about the quality of the wine. The data set has several characteristics, such as alcohol, chlorides, density, etc., and one objective variable, the quality of the wine.
import pandas as pd
# Load the data
data = pd.read_csv("winequality.csv")
# Check for missing values
print(data.isnull().sum())
# Drop rows with missing values
data = data.dropna()
# Check for duplicate rows
print(data.duplicated().sum())
# Drop duplicate rows
data = data.drop_duplicates()
# Check for outliers
Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
IQR = Q3 - Q1
data = data[
~((data < (Q1 - 1.5 * IQR)) | (data > (Q3 + 1.5 * IQR))).any(axis=1)
]
# Scale the data
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
# Split the data into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
data_scaled, data["quality"], test_size=0.2, random_state=42
)
In this example, we first load the data using the Pandas read_csv function and then check for missing values using the isnull function. We then remove the rows with missing values using the dropna function.
Next, we search for duplicate rows using the duplicate function and remove them using the drop_duplicates function.
We then check for outliers using the interquartile range (IQR) method, which calculates the difference between the first and third quartiles. All data points that fall outside of 1.5 times the IQR are considered outliers and are removed from the data set.
After handling missing values, duplicate rows, and outliers, we scale the data using the StandardScaler function from the sklearn.preprocessing library. Scaling the data is important because it helps ensure that all variables are on the same scale, which is necessary for most machine learning algorithms to work properly.
Finally, we split the data into training and test sets using the train_test_split function from the sklearn.model_selection library. This step is necessary to evaluate the performance of the model on unseen data.
Not preprocessing the data before applying it to a machine learning algorithm can have several negative consequences. Some of the main problems that can arise are:
- Poor model performance – If the data is not properly cleaned and formatted, the algorithm may not be able to understand it correctly, which can lead to poor model performance. This can be due to missing values, outliers, or irrelevant data that is not removed from the data set.
- Overfitting: If the dataset is not cleaned and preprocessed, it may contain irrelevant or redundant information that can lead to overfitting. Overfitting occurs when a model is trained on a data set that is too specific, and as a result performs well on the training data but poorly on new, unseen data.
- Longer training times – Not preprocessing the data can lead to longer training times, as the algorithm may need to process more data than necessary, which can take a long time.
- Difficulty understanding the model: If the data is not preprocessed, it can be difficult to understand the relationships between the different variables and how they influence the model’s predictions. This can make it difficult to identify errors or areas for improvement in the model.
- Biased results: If the data is not pre-processed, it may contain errors or biases that can lead to unfair or inaccurate results. For example, if the data contains missing values, the algorithm may be working with a biased sample of the data, which may lead to incorrect conclusions.
In general, not preprocessing the data can result in models that are less accurate, less interpretable, and more difficult to work with. Data preprocessing is an important step in the machine learning workflow that should not be skipped.
In conclusion, preprocessing data before applying it to a machine learning algorithm is a crucial step in the ML workflow. It helps improve accuracy, reduce the time and resources required to train the model, avoid overfitting, and improve model interpretability.
The code example above demonstrates how to preprocess data using the popular Python library Pandas, but there are many other libraries available for preprocessing data, such as NumPy and Scikit-learn, that can be used depending on the specific needs of your project.
sumit singh is a serial entrepreneur working towards Data Centric AI. He co-founded the next generation training data platform. labeler. Labellerr’s platform enables AI-ML teams to easily automate their data preparation process.