Data Science

Covariance Shift

Changing the distributions of various variables from train to test is called dataset shifting. This can lead to various problems when forecasting values in the deployed model. Shifting comes in a variety of forms. One of the shifts is covariance shifting, which happens inside the independent variables of train and test data. We will look into dataset shifting in detail, including its explanation, causes, identification, and covariant shifting.

What are Variance and Covariance?

The dispersion of data is measured by variance. It tells us how distributed the data is around a certain ‘true’ or ‘correct’ number (central tendency – one measure of which is the mean of the data. In univariate analysis, the term variance is used to describe the behavior of a single variable. Covariance is used in multivariate analysis to examine the joint behavior of two variables. When two variables move in the same direction, their covariance is positive; it is negative when they move in opposing directions.

What is Dataset Shifting?

When the distribution of your train and test data differs, this is known as dataset shifting. Because the model was trained on one distribution and is now being used to predict different data distributions, resulting in lower accuracy on the test data, as a result, you should always test your train and test data distributions and make them as similar as feasible.

Types of Data Shifting

  1. Changes in the independent variables or features of the dataset: Covariate Shift
  2. Changes in the target variable or the dependent variable in the dataset:
    Prior Probability Shift
  3. Change in the connection between the independent and the target variable across datasets: Concept Shift

Why Does Dataset Shift happen?

Sample Selection Bias: The variation in distribution is attributable to the fact that training data was obtained via a biased method and does not accurately represent the operational environment from which test data was obtained.

Non-Stationary Environments: The training environment differs from the test environment, either time or space.

What is Covariate Shift in Machine Learning?

The difference between training and test data set distributions is known as covariate shift. This means that the training of the dataset is performed on one kind of distribution, and the model is being used to predict the data of some other distribution. Covariate shift can indicate that the model cannot generalize well enough. The ability of a model to apply itself to new data using features acquired from training data is known as generalization. You’d think they’d come from the same distribution, but that’s nearly never the case. As a result, you must keep your models up to date with the most recent train set. This is typically caused by changes in the state of latent variables, which might be temporal (including changes in the stationarity of a temporal process), spatial, or less evident. It’s also possible to think of it as seeing into an uncharted “region” of the data universe. It’s a fascinating field of research because it can be observed in various ways in nature. We can deal with it in the data space by creative extrapolation, but this rarely works, and alternatives like re-estimating latent variables or attempting to make a prediction function adaptive to the domain. Special circumstances, such as stationary time variables and, occasionally, pure numeric data, are required to see if we have truly gone outside our original covariate space. In this scenario, we can calculate the convex hull’s data space and see if our new data point falls outside it. Of course, this is computationally expensive, so it’s rarely done until our forecasts are incorrect. It is, of course, application-dependent.

Examples of Covariant Shift

The detection of covariate drift and other types of model drift is a key step in enhancing the model’s test accuracy. The following are some examples of covariate shift in common machine learning use cases:

Image classification and facial recognition: A model may have been trained on images of only a few dog breeds, but it will perform poorly when used to forecast breeds that were not present in the training data.

Speech detection and translation: A model can be trained on speakers with a particular accent. When used with speech with new dialects or accents, the model may attain a high level of accuracy with the training data, but it will become inaccurate when used with new dialects or accents.

Healthcare: A model trained on accessible training data from patients in their 20s will be less accurate when screening data from patients aged 60 and up.

Handling Covariance Shift

We drop the features categorized as drifting in our strategy for dealing with dataset shift. However, merely removing features could result in some data loss. Later, we can simply drop the less important features. As a result, features with a drift value larger than a certain threshold are removed. Below is the code that calculates and displays the feature importance for a linear regression model.

from sklearn.datasets import make_regression
from skl
earn.linear_model import LinearRegression
from matplotlib import pyplot

X, y = make_regression(n_samples=2000, n_features=15, n_informative=5, random_state=1)

model = LinearRegression(), y)

coef_array = model.coef_

for i,v in enumerate(coef_array):
  print('Feature: %0d, Score: %.5f' % (i,v))[x for x in range(len(coef_array))], coef_array)


Feature: 0, Score: 0.00000
Feature: 1, Score: 0.00000
Feature: 2, Score: 51.76768
Feature: 3, Score: 0.00000
Feature: 4, Score: 0.00000
Feature: 5, Score: 0.00000
Feature: 6, Score: 77.69109
Feature: 7, Score: 0.00000
Feature: 8, Score: 41.53725
Feature: 9, Score: 0.00000
Feature: 10, Score: 14.19662
Feature: 11, Score: 80.91086
Feature: 12, Score: -0.00000
Feature: 13, Score: -0.00000
Feature: 14, Score: -0.00000


This article looked through many concepts, reasons, and remedies connected to dataset shifting. The shifting of data distributions from training to test data is called dataset shifting. Different training and testing conditions might shift covariance between the independent variables. After estimating feature importance, we can utilize feature dropping to eliminate dataset shifting.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.