Featured Image

How to Train to the Test Set in Machine Learning

Training to the test set is a type of overfitting where a model is prepared that intentionally achieves good performance on a given test set at the expense of increased generalization error.

It is a type of overfitting that is common in machine learning competitions where a complete training dataset is provided and where only the input portion of a test set is provided. One approach to training to the test set involves constructing a training set that most resembles the test set and then using it as the basis for training a model. The model is expected to have better performance on the test set, but most likely worse performance on the training dataset and on any new data in the future.

Although overfitting the test set is not desirable, it can be interesting to explore as a thought experiment and provide more insight into both machine learning competitions and avoiding overfitting generally.

In this tutorial, you will discover how to intentionally train to the test set for classification and regression problems.

After completing this tutorial, you will know:

  • Training to the test set is a type of data leakage that may occur in machine learning competitions.
  • One approach to training to the test set involves creating a training dataset that is most similar to a provided test set.
  • How to use a KNN model to construct a training dataset and train to the test set with a real dataset.

Kick-start your project with my new book Data Preparation for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

How to Train to the Test Set in Machine Learning

How to Train to the Test Set in Machine Learning
Photo by ND Strupler, some rights reserved.

Tutorial Overview

This tutorial is divided into three parts; they are:

  • Train to the Test Set
  • Train to Test Set for Classification
  • Train to Test Set for Regression
  • Train to the Test Set

    In applied machine learning, we seek a model that learns the relationship between the input and output variables using the training dataset.

    The hope and goal is that we learn a relationship that generalizes to new examples beyond the training dataset. This goal motivates why we use resampling techniques like k-fold cross-validation to estimate the performance of the model when making predictions on data not used during training.

    In the case of machine learning competitions, like those on Kaggle, we are given access to the complete training dataset and the inputs of the test dataset and are required to make predictions for the test dataset.

    This leads to a possible situation where we may accidentally or choose to train a model to the test set. That is, tune the model behavior to achieve the best performance on the test dataset rather than develop a model that performs well in general, using a technique like k-fold cross-validation.

    Another, more overt path to information leakage, can sometimes be seen in machine learning competitions where the training and test set data are given at the same time.

    — Page 56, Feature Engineering and Selection: A Practical Approach for Predictive Models, 2019.

    Training to the test set is often a bad idea.

    It is an explicit type of data leakage. Nevertheless, it is an interesting thought experiment.

    One approach to training to the test set is to contrive a training dataset that is most similar to the test set. For example, we could discard all rows in the training set that are too different from the test set and only train on those rows in the training set that are maximally similar to rows in the test set.

    While the test set data often have the outcome data blinded, it is possible to “train to the test” by only using the training set samples that are most similar to the test set data. This may very well improve the model’s performance scores for this particular test set but might ruin the model for predicting on a broader data set.

    — Page 56, Feature Engineering and Selection: A Practical Approach for Predictive Models, 2019.

    We would expect the model to overfit the test set, but this is the whole point of this thought experiment.

    Let’s explore this approach to training to the test set in this tutorial.

    We can use a k-nearest neighbor model to select those instances of the training set that are most similar to the test set. The KNeighborsRegressor and KNeighborsClassifier both provide the kneighbors() function that will return indexes into the training dataset for rows that are most similar to a given data, such as a test set.


    We might want to try removing duplicates from the selected row indexes.


    We can then use those row indexes to construct a custom training dataset and fit a model.


    Given that we are using a KNN model to construct the training set from the test set, we will also use the same type of model to make predictions on the test set. This is not required, but it makes the examples simpler.

    Using this approach, we can now experiment with training to the test set for both classification and regression datasets.



    Want to Get Started With Data Preparation?

    Take my free 7-day email crash course now (with sample code).

    Click to sign-up and also get a free PDF Ebook version of the course.

    Download Your FREE Mini-Course


    Train to Test Set for Classification

    We will use the diabetes dataset as the basis for exploring training for the test set for classification problems.

    Each record describes the medical details of a female and the prediction is the onset of diabetes within the next five years.

    The dataset has eight input variables and 768 rows of data; the input variables are all numeric and the target has two class labels, e.g. it is a binary classification task.

    Below provides a sample of the first five rows of the dataset.


    First, we can load the dataset directly from the URL, split it into input and output elements, then split the dataset into train and test sets, holding thirty percent back for the test set. We can then evaluate a KNN model with default model hyperparameters by training it on the training set and making predictions on the test set.

    The complete example is listed below.


    Running the example first loads the dataset and summarizes the number of rows and columns, matching our expectations. The shape of the train and test sets are then reported, showing we have about 230 rows in the test set.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    Finally, the classification accuracy of the model is reported to be about 77.056 percent.


    Now, let’s see if we can achieve better performance on the test set by preparing a model that is trained directly for it.

    First, we will construct a training dataset using the simpler example in the training set for each row in the test set.


    Next, we will train the model on this new dataset and evaluate it on the test set as we did before.


    The complete example is listed below.


    Running the example, we can see that the reported size of the new training dataset is the same size as the test set, as we expected.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    We can see that we have achieved a lift in performance by training to the test set over training the model on the entire training dataset. In this case, we achieved a classification accuracy of about 79.654 percent compared to 77.056 percent when the entire training dataset is used.


    You might want to try selecting different numbers of neighbors from the training set for each example in the test set to see if you can achieve better performance.

    Also, you might want to try keeping unique row indexes in the training set and see if that makes a difference.

    Finally, it might be interesting to hold back a final validation dataset and compare how different “train-to-the-test-set” techniques affect performance on the holdout dataset. E.g. see how training to the test set impacts generalization error.

    Report your findings in the comments below.

    Now that we know how to train to the test set for classification, let’s look at an example for regression.

    Train to Test Set for Regression

    We will use the housing dataset as the basis for exploring training for the test set for regression problems.

    The housing dataset involves the prediction of a house price in thousands of dollars given details of the house and its neighborhood.

    It is a regression problem, meaning we are predicting a numerical value. There are 506 observations with 13 input variables and one output variable.

    A sample of the first five rows is listed below.


    First, we can load the dataset, split it, and evaluate a KNN model on it directly using the entire training dataset. We will report performance on this regression class using mean absolute error (MAE).

    The complete example is listed below.


    Running the example first loads the dataset and summarizes the number of rows and columns, matching our expectations. The shape of the train and test sets are then reported, showing we have about 150 rows in the test set.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    Finally, the MAE of the model is reported to be about 4.488.


    Now, let’s see if we can achieve better performance on the test set by preparing a model that is trained to it.

    First, we will construct a training dataset using the simpler example in the training set for each row in the test set.


    Next, we will train the model on this new dataset and evaluate it on the test set as we did before.


    The complete example is listed below.


    Running the example, we can see that the reported size of the new training dataset is the same size as the test set, as we expected.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    We can see that we have achieved a lift in performance by training to the test set over training the model on the entire training dataset. In this case, we achieved a MAE of about 4.433 compared to 4.488 when the entire training dataset is used.

    Again, you might want to explore using a different number of neighbors when constructing the new training set and see if keeping unique rows in the training dataset makes a difference. Report your findings in the comments below.


    Further Reading

    This section provides more resources on the topic if you are looking to go deeper.

    Books
    APIs

    Summary

    In this tutorial, you discovered how to intentionally train to the test set for classification and regression problems.

    Specifically, you learned:

    • Training to the test set is a type of data leakage that may occur in machine learning competitions.
    • One approach to training to the test set involves creating a training dataset that is most similar to a provided test set.
    • How to use a KNN model to construct a training dataset and train to the test set with a real dataset.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

    Get a Handle on Modern Data Preparation!

    Data Preparation for Machine Learning

    Prepare Your Machine Learning Data in Minutes

    …with just a few lines of python code

    Discover how in my new Ebook:
    Data Preparation for Machine Learning

    It provides self-study tutorials with full working code on:
    Feature Selection, RFE, Data Cleaning, Data Transforms, Scaling, Dimensionality Reduction,
    and much more…

    Bring Modern Data Preparation Techniques to
    Your Machine Learning Projects

    See What’s Inside

    Covid Abruzzo Basilicata Calabria Campania Emilia Romagna Friuli Venezia Giulia Lazio Liguria Lombardia Marche Molise Piemonte Puglia Sardegna Sicilia Toscana Trentino Alto Adige Umbria Valle d’Aosta Veneto Italia Agrigento Alessandria Ancona Aosta Arezzo Ascoli Piceno Asti Avellino Bari Barletta-Andria-Trani Belluno Benevento Bergamo Biella Bologna Bolzano Brescia Brindisi Cagliari Caltanissetta Campobasso Carbonia-Iglesias Caserta Catania Catanzaro Chieti Como Cosenza Cremona Crotone Cuneo Enna Fermo Ferrara Firenze Foggia Forlì-Cesena Frosinone Genova Gorizia Grosseto Imperia Isernia La Spezia L’Aquila Latina Lecce Lecco Livorno Lodi Lucca Macerata Mantova Massa-Carrara Matera Messina Milano Modena Monza e della Brianza Napoli Novara Nuoro Olbia-Tempio Oristano Padova Palermo Parma Pavia Perugia Pesaro e Urbino Pescara Piacenza Pisa Pistoia Pordenone Potenza Prato Ragusa Ravenna Reggio Calabria Reggio Emilia Rieti Rimini Roma Rovigo Salerno Medio Campidano Sassari Savona Siena Siracusa Sondrio Taranto Teramo Terni Torino Ogliastra Trapani Trento Treviso Trieste Udine Varese Venezia Verbano-Cusio-Ossola Vercelli Verona Vibo Valentia Vicenza Viterbo

    Featured Image

    How to Set Axis for Rows and Columns in NumPy

    NumPy arrays provide a fast and efficient way to store and manipulate data in Python.

    They are particularly useful for representing data as vectors and matrices in machine learning.

    Data in NumPy arrays can be accessed directly via column and row indexes, and this is reasonably straightforward. Nevertheless, sometimes we must perform operations on arrays of data such as sum or mean of values by row or column and this requires the axis of the operation to be specified.

    Unfortunately, the column-wise and row-wise operations on NumPy arrays do not match our intuitions gained from row and column indexing, and this can cause confusion for beginners and seasoned machine learning practitioners alike. Specifically, operations like sum can be performed column-wise using axis=0 and row-wise using axis=1.

    In this tutorial, you will discover how to access and operate on NumPy arrays by row and by column.

    After completing this tutorial, you will know:

    • How to define NumPy arrays with rows and columns of data.
    • How to access values in NumPy arrays by row and column indexes.
    • How to perform operations on NumPy arrays by row and column axis.

    Let’s get started.

    How to Set NumPy Axis for Rows and Columns in Python

    How to Set NumPy Axis for Rows and Columns in Python
    Photo by Jonathan Cutrer, some rights reserved.

    Tutorial Overview

    This tutorial is divided into three parts; they are:

  • NumPy Array With Rows and Columns
  • Rows and Columns of Data in NumPy Arrays
  • NumPy Array Operations By Row and Column
  • Axis=None Array-Wise Operation
  • Axis=0 Column-Wise Operation
  • Axis=1 Row-Wise Operation
  • NumPy Array With Rows and Columns

    Before we dive into the NumPy array axis, let’s refresh our knowledge of NumPy arrays.

    Typically in Python, we work with lists of numbers or lists of lists of numbers. For example, we can define a two-dimensional matrix of two rows of three numbers as a list of numbers as follows:


    A NumPy array allows us to define and operate upon vectors and matrices of numbers in an efficient manner, e.g. a lot more efficient than simply Python lists. NumPy arrays are called NDArrays and can have virtually any number of dimensions, although, in machine learning, we are most commonly working with 1D and 2D arrays (or 3D arrays for images).

    For example, we can convert our list of lists matrix to a NumPy array via the asarray() function:


    We can print the array directly and expect to see two rows of numbers, where each row has three numbers or columns.


    We can summarize the dimensionality of an array by printing the “shape” property, which is a tuple, where the number of values in the tuple defines the number of dimensions, and the integer in each position defines the size of the dimension.

    For example, we expect the shape of our array to be (2,3) for two rows and three columns.


    Tying this all together, a complete example is listed below.


    Running the example defines our data as a list of lists, converts it to a NumPy array, then prints the data and shape.

    We can see that when the array is printed, it has the expected shape of two rows with three columns. We can then see that the printed shape matches our expectations.


    For more on the basics of NumPy arrays, see the tutorial:

    So far, so good.

    But how do we access data in the array by row or column? More importantly, how can we perform operations on the array by-row or by-column?

    Let’s take a closer look at these questions.

    Rows and Columns of Data in NumPy Arrays

    The “shape” property summarizes the dimensionality of our data.

    Importantly, the first dimension defines the number of rows and the second dimension defines the number of columns. For example (2,3) defines an array with two rows and three columns, as we saw in the last section.

    We can enumerate each row of data in an array by enumerating from index 0 to the first dimension of the array shape, e.g. shape[0]. We can access data in the array via the row and column index.

    For example, data[0, 0] is the value at the first row and the first column, whereas data[0, :] is the values in the first row and all columns, e.g. the complete first row in our matrix.

    The example below enumerates all rows in the data and prints each in turn.


    As expected, the results show the first row of data, then the second row of data.


    We can achieve the same effect for columns.

    That is, we can enumerate data by columns. For example, data[:, 0] accesses all rows for the first column. We can enumerate all columns from column 0 to the final column defined by the second dimension of the “shape” property, e.g. shape[1].

    The example below demonstrates this by enumerating all columns in our matrix.


    Running the example enumerates and prints each column in the matrix.

    Given that the matrix has three columns, we can see that the result is that we print three columns, each as a one-dimensional vector. That is column 1 (index 0) that has values 1 and 4, column 2 (index 1) that has values 2 and 5, and column 3 (index 2) that has values 3 and 6.

    It just looks funny because our columns don’t look like columns; they are turned on their side, rather than vertical.


    Now we know how to access data in a numpy array by column and by row.

    So far, so good, but what about operations on the array by column and array? That’s next.

    NumPy Array Operations By Row and Column

    We often need to perform operations on NumPy arrays by column or by row.

    For example, we may need to sum values or calculate a mean for a matrix of data by row or by column.

    This can be achieved by using the sum() or mean() NumPy function and specifying the “axis” on which to perform the operation.

    We can specify the axis as the dimension across which the operation is to be performed, and this dimension does not match our intuition based on how we interpret the “shape” of the array and how we index data in the array.

    As such, this causes maximum confusion for beginners.

    That is, axis=0 will perform the operation column-wise and axis=1 will perform the operation row-wise. We can also specify the axis as None, which will perform the operation for the entire array.

    In summary:

    • axis=None: Apply operation array-wise.
    • axis=0: Apply operation column-wise, across all rows for each column.
    • axis=1: Apply operation row-wise, across all columns for each row.

    Let’s make this concrete with a worked example.

    We will sum values in our array by each of the three axes.

    Axis=None Array-Wise Operation

    Setting the axis=None when performing an operation on a NumPy array will perform the operation for the entire array.

    This is often the default for most operations, such as sum, mean, std, and so on.


    The example below demonstrates summing all values in an array, e.g. an array-wise operation.


    Running the example first prints the array, then performs the sum operation array-wise and prints the result.

    We can see the array has six values that would sum to 21 if we add them manually and that the result of the sum operation performed array-wise matches this expectation.


    Axis=0 Column-Wise Operation

    Setting the axis=0 when performing an operation on a NumPy array will perform the operation column-wise, that is, across all rows for each column.


    For example, given our data with two rows and three columns:


    We expect a sum column-wise with axis=0 will result in three values, one for each column, as follows:

    • Column 1: 1 + 4 = 5
    • Column 2: 2 + 5 = 7
    • Column 3: 3 + 6 = 9

    The example below demonstrates summing values in the array by column, e.g. a column-wise operation.


    Running the example first prints the array, then performs the sum operation column-wise and prints the result.

    We can see the array has six values with two rows and three columns as expected; we can then see the column-wise operation result in a vector with three values, one for the sum of each column matching our expectation.


    Axis=1 Row-Wise Operation

    Setting the axis=1 when performing an operation on a NumPy array will perform the operation row-wise, that is across all columns for each row.


    For example, given our data with two rows and three columns:


    We expect a sum row-wise with axis=1 will result in two values, one for each row, as follows:

    • Row 1: 1 + 2 + 3 = 6
    • Row 2: 4 + 5 + 6 = 15

    The example below demonstrates summing values in the array by row, e.g. a row-wise operation.


    Running the example first prints the array, then performs the sum operation row-wise and prints the result.

    We can see the array has six values with two rows and three columns as expected; we can then see the row-wise operation result in a vector with two values, one for the sum of each row matching our expectation.


    We now have a concrete idea of how to set axis appropriately when performing operations on our NumPy arrays.

    Further Reading

    This section provides more resources on the topic if you are looking to go deeper.

    Tutorials
    APIs

    Summary

    In this tutorial, you discovered how to access and operate on NumPy arrays by row and by column.

    Specifically, you learned:

    • How to define NumPy arrays with rows and columns of data.
    • How to access values in NumPy arrays by row and column indexes.
    • How to perform operations on NumPy arrays by row and column axis.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

    Get a Handle on Linear Algebra for Machine Learning!

    Linear Algebra for Machine Learning
    Develop a working understand of linear algebra

    …by writing lines of code in python

    Discover how in my new Ebook:
    Linear Algebra for Machine Learning

    It provides self-study tutorials on topics like:
    Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more…

    Finally Understand the Mathematics of Data

    Skip the Academics. Just Results.

    See What’s Inside

    Covid Abruzzo Basilicata Calabria Campania Emilia Romagna Friuli Venezia Giulia Lazio Liguria Lombardia Marche Molise Piemonte Puglia Sardegna Sicilia Toscana Trentino Alto Adige Umbria Valle d’Aosta Veneto Italia Agrigento Alessandria Ancona Aosta Arezzo Ascoli Piceno Asti Avellino Bari Barletta-Andria-Trani Belluno Benevento Bergamo Biella Bologna Bolzano Brescia Brindisi Cagliari Caltanissetta Campobasso Carbonia-Iglesias Caserta Catania Catanzaro Chieti Como Cosenza Cremona Crotone Cuneo Enna Fermo Ferrara Firenze Foggia Forlì-Cesena Frosinone Genova Gorizia Grosseto Imperia Isernia La Spezia L’Aquila Latina Lecce Lecco Livorno Lodi Lucca Macerata Mantova Massa-Carrara Matera Messina Milano Modena Monza e della Brianza Napoli Novara Nuoro Olbia-Tempio Oristano Padova Palermo Parma Pavia Perugia Pesaro e Urbino Pescara Piacenza Pisa Pistoia Pordenone Potenza Prato Ragusa Ravenna Reggio Calabria Reggio Emilia Rieti Rimini Roma Rovigo Salerno Medio Campidano Sassari Savona Siena Siracusa Sondrio Taranto Teramo Terni Torino Ogliastra Trapani Trento Treviso Trieste Udine Varese Venezia Verbano-Cusio-Ossola Vercelli Verona Vibo Valentia Vicenza Viterbo

    Recent Posts

    Archives