5. Classification I: training & predicting#

5.1. Overview#

In previous chapters, we focused solely on descriptive and exploratory data analysis questions. This chapter and the next together serve as our first foray into answering predictive questions about data. In particular, we will focus on classification, i.e., using one or more variables to predict the value of a categorical variable of interest. This chapter will cover the basics of classification, how to preprocess data to make it suitable for use in a classifier, and how to use our observed data to make predictions. The next chapter will focus on how to evaluate how accurate the predictions from our classifier are, as well as how to improve our classifier (where possible) to maximize its accuracy.

5.2. Chapter learning objectives#

By the end of the chapter, readers will be able to do the following:

  • Recognize situations where a classifier would be appropriate for making predictions.

  • Describe what a training data set is and how it is used in classification.

  • Interpret the output of a classifier.

  • Compute, by hand, the straight-line (Euclidean) distance between points on a graph when there are two predictor variables.

  • Explain the K-nearest neighbors classification algorithm.

  • Perform K-nearest neighbors classification in Python using scikit-learn.

  • Use methods from scikit-learn to center, scale, balance, and impute data as a preprocessing step.

  • Combine preprocessing and model training into a Pipeline using make_pipeline.

5.3. The classification problem#

In many situations, we want to make predictions based on the current situation as well as past experiences. For instance, a doctor may want to diagnose a patient as either diseased or healthy based on their symptoms and the doctor’s past experience with patients; an email provider might want to tag a given email as “spam” or “not spam” based on the email’s text and past email text data; or a credit card company may want to predict whether a purchase is fraudulent based on the current purchase item, amount, and location as well as past purchases. These tasks are all examples of classification, i.e., predicting a categorical class (sometimes called a label) for an observation given its other variables (sometimes called features).

Generally, a classifier assigns an observation without a known class (e.g., a new patient) to a class (e.g., diseased or healthy) on the basis of how similar it is to other observations for which we do know the class (e.g., previous patients with known diseases and symptoms). These observations with known classes that we use as a basis for prediction are called a training set; this name comes from the fact that we use these data to train, or teach, our classifier. Once taught, we can use the classifier to make predictions on new data for which we do not know the class.

There are many possible methods that we could use to predict a categorical class/label for an observation. In this book, we will focus on the widely used K-nearest neighbors algorithm [Cover and Hart, 1967, Fix and Hodges, 1951]. In your future studies, you might encounter decision trees, support vector machines (SVMs), logistic regression, neural networks, and more; see the additional resources section at the end of the next chapter for where to begin learning more about these other methods. It is also worth mentioning that there are many variations on the basic classification problem. For example, we focus on the setting of binary classification where only two classes are involved (e.g., a diagnosis of either healthy or diseased), but you may also run into multiclass classification problems with more than two categories (e.g., a diagnosis of healthy, bronchitis, pneumonia, or a common cold).

5.4. Exploring a data set#

In this chapter and the next, we will study a data set of digitized breast cancer image features, created by Dr. William H. Wolberg, W. Nick Street, and Olvi L. Mangasarian [Street et al., 1993]. Each row in the data set represents an image of a tumor sample, including the diagnosis (benign or malignant) and several other measurements (nucleus texture, perimeter, area, and more). Diagnosis for each image was conducted by physicians.

As with all data analyses, we first need to formulate a precise question that we want to answer. Here, the question is predictive: can we use the tumor image measurements available to us to predict whether a future tumor image (with unknown diagnosis) shows a benign or malignant tumor? Answering this question is important because traditional, non-data-driven methods for tumor diagnosis are quite subjective and dependent upon how skilled and experienced the diagnosing physician is. Furthermore, benign tumors are not normally dangerous; the cells stay in the same place, and the tumor stops growing before it gets very large. By contrast, in malignant tumors, the cells invade the surrounding tissue and spread into nearby organs, where they can cause serious damage [Stanford Health Care, 2021]. Thus, it is important to quickly and accurately diagnose the tumor type to guide patient treatment.

5.4.1. Loading the cancer data#

Our first step is to load, wrangle, and explore the data using visualizations in order to better understand the data we are working with. We start by loading the pandas and altair packages needed for our analysis.

import pandas as pd
import altair as alt

In this case, the file containing the breast cancer data set is a .csv file with headers. We’ll use the read_csv function with no additional arguments, and then inspect its contents:

cancer = pd.read_csv("data/wdbc.csv")
cancer
ID Class Radius Texture Perimeter Area Smoothness Compactness Concavity Concave_Points Symmetry Fractal_Dimension
0 842302 M 1.096100 -2.071512 1.268817 0.983510 1.567087 3.280628 2.650542 2.530249 2.215566 2.253764
1 842517 M 1.828212 -0.353322 1.684473 1.907030 -0.826235 -0.486643 -0.023825 0.547662 0.001391 -0.867889
2 84300903 M 1.578499 0.455786 1.565126 1.557513 0.941382 1.052000 1.362280 2.035440 0.938859 -0.397658
3 84348301 M -0.768233 0.253509 -0.592166 -0.763792 3.280667 3.399917 1.914213 1.450431 2.864862 4.906602
4 84358402 M 1.748758 -1.150804 1.775011 1.824624 0.280125 0.538866 1.369806 1.427237 -0.009552 -0.561956
... ... ... ... ... ... ... ... ... ... ... ... ...
564 926424 M 2.109139 0.720838 2.058974 2.341795 1.040926 0.218868 1.945573 2.318924 -0.312314 -0.930209
565 926682 M 1.703356 2.083301 1.614511 1.722326 0.102368 -0.017817 0.692434 1.262558 -0.217473 -1.057681
566 926954 M 0.701667 2.043775 0.672084 0.577445 -0.839745 -0.038646 0.046547 0.105684 -0.808406 -0.894800
567 927241 M 1.836725 2.334403 1.980781 1.733693 1.524426 3.269267 3.294046 2.656528 2.135315 1.042778
568 92751 B -1.806811 1.220718 -1.812793 -1.346604 -3.109349 -1.149741 -1.113893 -1.260710 -0.819349 -0.560539

569 rows × 12 columns

5.4.2. Describing the variables in the cancer data set#

Breast tumors can be diagnosed by performing a biopsy, a process where tissue is removed from the body and examined for the presence of disease. Traditionally these procedures were quite invasive; modern methods such as fine needle aspiration, used to collect the present data set, extract only a small amount of tissue and are less invasive. Based on a digital image of each breast tissue sample collected for this data set, ten different variables were measured for each cell nucleus in the image (items 3–12 of the list of variables below), and then the mean for each variable across the nuclei was recorded. As part of the data preparation, these values have been standardized (centered and scaled); we will discuss what this means and why we do it later in this chapter. Each image additionally was given a unique ID and a diagnosis by a physician. Therefore, the total set of variables per image in this data set is:

  1. ID: identification number

  2. Class: the diagnosis (M = malignant or B = benign)

  3. Radius: the mean of distances from center to points on the perimeter

  4. Texture: the standard deviation of gray-scale values

  5. Perimeter: the length of the surrounding contour

  6. Area: the area inside the contour

  7. Smoothness: the local variation in radius lengths

  8. Compactness: the ratio of squared perimeter and area

  9. Concavity: severity of concave portions of the contour

  10. Concave Points: the number of concave portions of the contour

  11. Symmetry: how similar the nucleus is when mirrored

  12. Fractal Dimension: a measurement of how “rough” the perimeter is

Below we use the info method to preview the data frame. This method can make it easier to inspect the data when we have a lot of columns: it prints only the column names down the page (instead of across), as well as their data types and the number of non-missing entries.

cancer.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 12 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   ID                 569 non-null    int64  
 1   Class              569 non-null    object 
 2   Radius             569 non-null    float64
 3   Texture            569 non-null    float64
 4   Perimeter          569 non-null    float64
 5   Area               569 non-null    float64
 6   Smoothness         569 non-null    float64
 7   Compactness        569 non-null    float64
 8   Concavity          569 non-null    float64
 9   Concave_Points     569 non-null    float64
 10  Symmetry           569 non-null    float64
 11  Fractal_Dimension  569 non-null    float64
dtypes: float64(10), int64(1), object(1)
memory usage: 53.5+ KB

From the summary of the data above, we can see that Class is of type object. We can use the unique method on the Class column to see all unique values present in that column. We see that there are two diagnoses: benign, represented by "B", and malignant, represented by "M".

cancer["Class"].unique()
array(['M', 'B'], dtype=object)

We will improve the readability of our analysis by renaming "M" to "Malignant" and "B" to "Benign" using the replace method. The replace method takes one argument: a dictionary that maps previous values to desired new values. We will verify the result using the unique method.

cancer["Class"] = cancer["Class"].replace({
    "M" : "Malignant",
    "B" : "Benign"
})

cancer["Class"].unique()
array(['Malignant', 'Benign'], dtype=object)

5.4.3. Exploring the cancer data#

Before we start doing any modeling, let’s explore our data set. Below we use the groupby and size methods to find the number and percentage of benign and malignant tumor observations in our data set. When paired with groupby, size counts the number of observations for each value of the Class variable. Then we calculate the percentage in each group by dividing by the total number of observations and multiplying by 100. The total number of observations equals the number of rows in the data frame, which we can access via the shape attribute of the data frame (shape[0] is the number of rows and shape[1] is the number of columns). We have 357 (63%) benign and 212 (37%) malignant tumor observations.

100 * cancer.groupby("Class").size() / cancer.shape[0]
Class
Benign       62.741652
Malignant    37.258348
dtype: float64

The pandas package also has a more convenient specialized value_counts method for counting the number of occurrences of each value in a column. If we pass no arguments to the method, it outputs a series containing the number of occurences of each value. If we instead pass the argument normalize=True, it instead prints the fraction of occurrences of each value.

cancer["Class"].value_counts()
Class
Benign       357
Malignant    212
Name: count, dtype: int64
cancer["Class"].value_counts(normalize=True)
Class
Benign       0.627417
Malignant    0.372583
Name: proportion, dtype: float64

Next, let’s draw a colored scatter plot to visualize the relationship between the perimeter and concavity variables. Recall that the default palette in altair is colorblind-friendly, so we can stick with that here.

perim_concav = alt.Chart(cancer).mark_circle().encode(
    x=alt.X("Perimeter").title("Perimeter (standardized)"),
    y=alt.Y("Concavity").title("Concavity (standardized)"),
    color=alt.Color("Class").title("Diagnosis")
)
perim_concav

Fig. 5.1 Scatter plot of concavity versus perimeter colored by diagnosis label.#

In Fig. 5.1, we can see that malignant observations typically fall in the upper right-hand corner of the plot area. By contrast, benign observations typically fall in the lower left-hand corner of the plot. In other words, benign observations tend to have lower concavity and perimeter values, and malignant ones tend to have larger values. Suppose we obtain a new observation not in the current data set that has all the variables measured except the label (i.e., an image without the physician’s diagnosis for the tumor class). We could compute the standardized perimeter and concavity values, resulting in values of, say, 1 and 1. Could we use this information to classify that observation as benign or malignant? Based on the scatter plot, how might you classify that new observation? If the standardized concavity and perimeter values are 1 and 1 respectively, the point would lie in the middle of the orange cloud of malignant points and thus we could probably classify it as malignant. Based on our visualization, it seems like it may be possible to make accurate predictions of the Class variable (i.e., a diagnosis) for tumor images with unknown diagnoses.

5.5. Classification with K-nearest neighbors#

In order to actually make predictions for new observations in practice, we will need a classification algorithm. In this book, we will use the K-nearest neighbors classification algorithm. To predict the label of a new observation (here, classify it as either benign or malignant), the K-nearest neighbors classifier generally finds the \(K\) “nearest” or “most similar” observations in our training set, and then uses their diagnoses to make a prediction for the new observation’s diagnosis. \(K\) is a number that we must choose in advance; for now, we will assume that someone has chosen \(K\) for us. We will cover how to choose \(K\) ourselves in the next chapter.

To illustrate the concept of K-nearest neighbors classification, we will walk through an example. Suppose we have a new observation, with standardized perimeter of 2.0 and standardized concavity of 4.0, whose diagnosis “Class” is unknown. This new observation is depicted by the red, diamond point in Fig. 5.2.

Fig. 5.2 Scatter plot of concavity versus perimeter with new observation represented as a red diamond.#

Fig. 5.3 shows that the nearest point to this new observation is malignant and located at the coordinates (2.1, 3.6). The idea here is that if a point is close to another in the scatter plot, then the perimeter and concavity values are similar, and so we may expect that they would have the same diagnosis.

Fig. 5.3 Scatter plot of concavity versus perimeter. The new observation is represented as a red diamond with a line to the one nearest neighbor, which has a malignant label.#

Suppose we have another new observation with standardized perimeter 0.2 and concavity of 3.3. Looking at the scatter plot in Fig. 5.4, how would you classify this red, diamond observation? The nearest neighbor to this new point is a benign observation at (0.2, 2.7). Does this seem like the right prediction to make for this observation? Probably not, if you consider the other nearby points.

Fig. 5.4 Scatter plot of concavity versus perimeter. The new observation is represented as a red diamond with a line to the one nearest neighbor, which has a benign label.#

To improve the prediction we can consider several neighboring points, say \(K = 3\), that are closest to the new observation to predict its diagnosis class. Among those 3 closest points, we use the majority class as our prediction for the new observation. As shown in Fig. 5.5, we see that the diagnoses of 2 of the 3 nearest neighbors to our new observation are malignant. Therefore we take majority vote and classify our new red, diamond observation as malignant.

Fig. 5.5 Scatter plot of concavity versus perimeter with three nearest neighbors.#

Here we chose the \(K=3\) nearest observations, but there is nothing special about \(K=3\). We could have used \(K=4, 5\) or more (though we may want to choose an odd number to avoid ties). We will discuss more about choosing \(K\) in the next chapter.

5.5.1. Distance between points#

We decide which points are the \(K\) “nearest” to our new observation using the straight-line distance (we will often just refer to this as distance). Suppose we have two observations \(a\) and \(b\), each having two predictor variables, \(x\) and \(y\). Denote \(a_x\) and \(a_y\) to be the values of variables \(x\) and \(y\) for observation \(a\); \(b_x\) and \(b_y\) have similar definitions for observation \(b\). Then the straight-line distance between observation \(a\) and \(b\) on the x-y plane can be computed using the following formula:

\[\mathrm{Distance} = \sqrt{(a_x -b_x)^2 + (a_y - b_y)^2}\]

To find the \(K\) nearest neighbors to our new observation, we compute the distance from that new observation to each observation in our training data, and select the \(K\) observations corresponding to the \(K\) smallest distance values. For example, suppose we want to use \(K=5\) neighbors to classify a new observation with perimeter 0.0 and concavity 3.5, shown as a red diamond in Fig. 5.6. Let’s calculate the distances between our new point and each of the observations in the training set to find the \(K=5\) neighbors that are nearest to our new point. You will see in the code below, we compute the straight-line distance using the formula above: we square the differences between the two observations’ perimeter and concavity coordinates, add the squared differences, and then take the square root. In order to find the \(K=5\) nearest neighbors, we will use the nsmallest function from pandas.

Fig. 5.6 Scatter plot of concavity versus perimeter with new observation represented as a red diamond.#

new_obs_Perimeter = 0
new_obs_Concavity = 3.5
cancer["dist_from_new"] = (
       (cancer["Perimeter"] - new_obs_Perimeter) ** 2
     + (cancer["Concavity"] - new_obs_Concavity) ** 2
)**(1/2)
cancer.nsmallest(5, "dist_from_new")[[
    "Perimeter",
    "Concavity",
    "Class",
    "dist_from_new"
]]
Perimeter Concavity Class dist_from_new
112 0.241202 2.653051 Benign 0.880626
258 0.750277 2.870061 Malignant 0.979663
351 0.622700 2.541410 Malignant 1.143088
430 0.416930 2.314364 Malignant 1.256806
152 -1.160091 4.039155 Benign 1.279258

In Table 5.1 we show in mathematical detail how we computed the dist_from_new variable (the distance to the new observation) for each of the 5 nearest neighbors in the training data.

Table 5.1 Evaluating the distances from the new observation to each of its 5 nearest neighbors#

Perimeter

Concavity

Distance

Class

0.24

2.65

\(\sqrt{(0-0.24)^2+(3.5-2.65)^2}=0.88\)

Benign

0.75

2.87

\(\sqrt{(0-0.75)^2+(3.5-2.87)^2}=0.98\)

Malignant

0.62

2.54

\(\sqrt{(0-0.62)^2+(3.5-2.54)^2}=1.14\)

Malignant

0.42

2.31

\(\sqrt{(0-0.42)^2+(3.5-2.31)^2}=1.26\)

Malignant

-1.16

4.04

\(\sqrt{(0-(-1.16))^2+(3.5-4.04)^2}=1.28\)

Benign

The result of this computation shows that 3 of the 5 nearest neighbors to our new observation are malignant; since this is the majority, we classify our new observation as malignant. These 5 neighbors are circled in Fig. 5.7.

Fig. 5.7 Scatter plot of concavity versus perimeter with 5 nearest neighbors circled.#

5.5.2. More than two explanatory variables#

Although the above description is directed toward two predictor variables, exactly the same K-nearest neighbors algorithm applies when you have a higher number of predictor variables. Each predictor variable may give us new information to help create our classifier. The only difference is the formula for the distance between points. Suppose we have \(m\) predictor variables for two observations \(a\) and \(b\), i.e., \(a = (a_{1}, a_{2}, \dots, a_{m})\) and \(b = (b_{1}, b_{2}, \dots, b_{m})\).

The distance formula becomes

\[\mathrm{Distance} = \sqrt{(a_{1} -b_{1})^2 + (a_{2} - b_{2})^2 + \dots + (a_{m} - b_{m})^2}.\]

This formula still corresponds to a straight-line distance, just in a space with more dimensions. Suppose we want to calculate the distance between a new observation with a perimeter of 0, concavity of 3.5, and symmetry of 1, and another observation with a perimeter, concavity, and symmetry of 0.417, 2.31, and 0.837 respectively. We have two observations with three predictor variables: perimeter, concavity, and symmetry. Previously, when we had two variables, we added up the squared difference between each of our (two) variables, and then took the square root. Now we will do the same, except for our three variables. We calculate the distance as follows

\[\mathrm{Distance} =\sqrt{(0 - 0.417)^2 + (3.5 - 2.31)^2 + (1 - 0.837)^2} = 1.27.\]

Let’s calculate the distances between our new observation and each of the observations in the training set to find the \(K=5\) neighbors when we have these three predictors.

new_obs_Perimeter = 0
new_obs_Concavity = 3.5
new_obs_Symmetry = 1
cancer["dist_from_new"] = (
      (cancer["Perimeter"] - new_obs_Perimeter) ** 2
    + (cancer["Concavity"] - new_obs_Concavity) ** 2
    + (cancer["Symmetry"] - new_obs_Symmetry) ** 2
)**(1/2)
cancer.nsmallest(5, "dist_from_new")[[
    "Perimeter",
    "Concavity",
    "Symmetry",
    "Class",
    "dist_from_new"
]]
Perimeter Concavity Symmetry Class dist_from_new
430 0.416930 2.314364 0.836722 Malignant 1.267368
400 1.334664 2.886368 1.099359 Malignant 1.472326
562 0.470430 2.084810 1.154075 Malignant 1.499268
68 -1.365450 2.812359 1.092064 Benign 1.531594
351 0.622700 2.541410 2.055065 Malignant 1.555575

Based on \(K=5\) nearest neighbors with these three predictors we would classify the new observation as malignant since 4 out of 5 of the nearest neighbors are malignant class. Fig. 5.8 shows what the data look like when we visualize them as a 3-dimensional scatter with lines from the new observation to its five nearest neighbors.

data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

Fig. 5.8 3D scatter plot of the standardized symmetry, concavity, and perimeter variables. Note that in general we recommend against using 3D visualizations; here we show the data in 3D only to illustrate what higher dimensions and nearest neighbors look like, for learning purposes.#

5.5.3. Summary of K-nearest neighbors algorithm#

In order to classify a new observation using a K-nearest neighbors classifier, we have to do the following:

  1. Compute the distance between the new observation and each observation in the training set.

  2. Find the \(K\) rows corresponding to the \(K\) smallest distances.

  3. Classify the new observation based on a majority vote of the neighbor classes.

5.6. K-nearest neighbors with scikit-learn#

Coding the K-nearest neighbors algorithm in Python ourselves can get complicated, especially if we want to handle multiple classes, more than two variables, or predict the class for multiple new observations. Thankfully, in Python, the K-nearest neighbors algorithm is implemented in the scikit-learn Python package [Buitinck et al., 2013] along with many other models that you will encounter in this and future chapters of the book. Using the functions in the scikit-learn package (named sklearn in Python) will help keep our code simple, readable and accurate; the less we have to code ourselves, the fewer mistakes we will likely make. Before getting started with K-nearest neighbors, we need to tell the sklearn package that we prefer using pandas data frames over regular arrays via the set_config function.

Note

You will notice a new way of importing functions in the code below: from ... import .... This lets us import just set_config from sklearn, and then call set_config without any package prefix. We will import functions using from extensively throughout this and subsequent chapters to avoid very long names from scikit-learn that clutter the code (like sklearn.neighbors.KNeighborsClassifier, which has 38 characters!).

from sklearn import set_config

# Output dataframes instead of arrays
set_config(transform_output="pandas")

We can now get started with K-nearest neighbors. The first step is to import the KNeighborsClassifier from the sklearn.neighbors module.

from sklearn.neighbors import KNeighborsClassifier

Let’s walk through how to use KNeighborsClassifier to perform K-nearest neighbors classification. We will use the cancer data set from above, with perimeter and concavity as predictors and \(K = 5\) neighbors to build our classifier. Then we will use the classifier to predict the diagnosis label for a new observation with perimeter 0, concavity 3.5, and an unknown diagnosis label. Let’s pick out our two desired predictor variables and class label and store them with the name cancer_train:

cancer_train = cancer[["Class", "Perimeter", "Concavity"]]
cancer_train
Class Perimeter Concavity
0 Malignant 1.268817 2.650542
1 Malignant 1.684473 -0.023825
2 Malignant 1.565126 1.362280
3 Malignant -0.592166 1.914213
4 Malignant 1.775011 1.369806
... ... ... ...
564 Malignant 2.058974 1.945573
565 Malignant 1.614511 0.692434
566 Malignant 0.672084 0.046547
567 Malignant 1.980781 3.294046
568 Benign -1.812793 -1.113893

569 rows × 3 columns

Next, we create a model object for K-nearest neighbors classification by creating a KNeighborsClassifier instance, specifying that we want to use \(K = 5\) neighbors; we will discuss how to choose \(K\) in the next chapter.

Note

You can specify the weights argument in order to control how neighbors vote when classifying a new observation. The default is "uniform", where each of the \(K\) nearest neighbors gets exactly 1 vote as described above. Other choices, which weigh each neighbor’s vote differently, can be found on the scikit-learn website.

knn = KNeighborsClassifier(n_neighbors=5)
knn
KNeighborsClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

In order to fit the model on the breast cancer data, we need to call fit on the model object. The X argument is used to specify the data for the predictor variables, while the y argument is used to specify the data for the response variable. So below, we set X=cancer_train[["Perimeter", "Concavity"]] and y=cancer_train["Class"] to specify that Class is the response variable (the one we want to predict), and both Perimeter and Concavity are to be used as the predictors. Note that the fit function might look like it does not do much from the outside, but it is actually doing all the heavy lifting to train the K-nearest neighbors model, and modifies the knn model object.

knn.fit(X=cancer_train[["Perimeter", "Concavity"]], y=cancer_train["Class"]);

After using the fit function, we can make a prediction on a new observation by calling predict on the classifier object, passing the new observation itself. As above, when we ran the K-nearest neighbors classification algorithm manually, the knn model object classifies the new observation as “Malignant”. Note that the predict function outputs an array with the model’s prediction; you can actually make multiple predictions at the same time using the predict function, which is why the output is stored as an array.

new_obs = pd.DataFrame({"Perimeter": [0], "Concavity": [3.5]})
knn.predict(new_obs)
array(['Malignant'], dtype=object)

Is this predicted malignant label the actual class for this observation? Well, we don’t know because we do not have this observation’s diagnosis— that is what we were trying to predict! The classifier’s prediction is not necessarily correct, but in the next chapter, we will learn ways to quantify how accurate we think our predictions are.

5.7. Data preprocessing with scikit-learn#

5.7.1. Centering and scaling#

When using K-nearest neighbors classification, the scale of each variable (i.e., its size and range of values) matters. Since the classifier predicts classes by identifying observations nearest to it, any variables with a large scale will have a much larger effect than variables with a small scale. But just because a variable has a large scale doesn’t mean that it is more important for making accurate predictions. For example, suppose you have a data set with two features, salary (in dollars) and years of education, and you want to predict the corresponding type of job. When we compute the neighbor distances, a difference of $1000 is huge compared to a difference of 10 years of education. But for our conceptual understanding and answering of the problem, it’s the opposite; 10 years of education is huge compared to a difference of $1000 in yearly salary!

In many other predictive models, the center of each variable (e.g., its mean) matters as well. For example, if we had a data set with a temperature variable measured in degrees Kelvin, and the same data set with temperature measured in degrees Celsius, the two variables would differ by a constant shift of 273 (even though they contain exactly the same information). Likewise, in our hypothetical job classification example, we would likely see that the center of the salary variable is in the tens of thousands, while the center of the years of education variable is in the single digits. Although this doesn’t affect the K-nearest neighbors classification algorithm, this large shift can change the outcome of using many other predictive models.

To scale and center our data, we need to find our variables’ mean (the average, which quantifies the “central” value of a set of numbers) and standard deviation (a number quantifying how spread out values are). For each observed value of the variable, we subtract the mean (i.e., center the variable) and divide by the standard deviation (i.e., scale the variable). When we do this, the data is said to be standardized, and all variables in a data set will have a mean of 0 and a standard deviation of 1. To illustrate the effect that standardization can have on the K-nearest neighbors algorithm, we will read in the original, unstandardized Wisconsin breast cancer data set; we have been using a standardized version of the data set up until now. We will apply the same initial wrangling steps as we did earlier, and to keep things simple we will just use the Area, Smoothness, and Class variables:

unscaled_cancer = pd.read_csv("data/wdbc_unscaled.csv")[["Class", "Area", "Smoothness"]]
unscaled_cancer["Class"] = unscaled_cancer["Class"].replace({
   "M" : "Malignant",
   "B" : "Benign"
})
unscaled_cancer
Class Area Smoothness
0 Malignant 1001.0 0.11840
1 Malignant 1326.0 0.08474
2 Malignant 1203.0 0.10960
3 Malignant 386.1 0.14250
4 Malignant 1297.0 0.10030
... ... ... ...
564 Malignant 1479.0 0.11100
565 Malignant 1261.0 0.09780
566 Malignant 858.1 0.08455
567 Malignant 1265.0 0.11780
568 Benign 181.0 0.05263

569 rows × 3 columns

Looking at the unscaled and uncentered data above, you can see that the differences between the values for area measurements are much larger than those for smoothness. Will this affect predictions? In order to find out, we will create a scatter plot of these two predictors (colored by diagnosis) for both the unstandardized data we just loaded, and the standardized version of that same data. But first, we need to standardize the unscaled_cancer data set with scikit-learn.

The scikit-learn framework provides a collection of preprocessors used to manipulate data in the preprocessing module. Here we will use the StandardScaler transformer to standardize the predictor variables in the unscaled_cancer data. In order to tell the StandardScaler which variables to standardize, we wrap it in a ColumnTransformer object using the make_column_transformer function. ColumnTransformer objects also enable the use of multiple preprocessors at once, which is especially handy when you want to apply different preprocessing to each of the predictor variables. The primary argument of the make_column_transformer function is a sequence of pairs of (1) a preprocessor, and (2) the columns to which you want to apply that preprocessor. In the present case, we just have the one StandardScaler preprocessor to apply to the Area and Smoothness columns.

from sklearn.preprocessing import StandardScaler
from sklearn.compose import make_column_transformer

preprocessor = make_column_transformer(
    (StandardScaler(), ["Area", "Smoothness"]),
)
preprocessor
ColumnTransformer(transformers=[('standardscaler', StandardScaler(),
                                 ['Area', 'Smoothness'])])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

You can see that the preprocessor includes a single standardization step that is applied to the Area and Smoothness columns. Note that here we specified which columns to apply the preprocessing step to by individual names; this approach can become quite difficult, e.g., when we have many predictor variables. Rather than writing out the column names individually, we can instead use the make_column_selector function. For example, if we wanted to standardize all numerical predictors, we would use make_column_selector and specify the dtype_include argument to be "number". This creates a preprocessor equivalent to the one we created previously.

from sklearn.compose import make_column_selector

preprocessor = make_column_transformer(
    (StandardScaler(), make_column_selector(dtype_include="number")),
)
preprocessor
ColumnTransformer(transformers=[('standardscaler', StandardScaler(),
                                 <sklearn.compose._column_transformer.make_column_selector object at 0x7f75685d0a90>)])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

We are now ready to standardize the numerical predictor columns in the unscaled_cancer data frame. This happens in two steps. We first use the fit function to compute the values necessary to apply the standardization (the mean and standard deviation of each variable), passing the unscaled_cancer data as an argument. Then we use the transform function to actually apply the standardization. It may seem a bit unnecessary to use two steps—fit and transform—to standardize the data. However, we do this in two steps so that we can specify a different data set in the transform step if we want. This enables us to compute the quantities needed to standardize using one data set, and then apply that standardization to another data set.

preprocessor.fit(unscaled_cancer)
scaled_cancer = preprocessor.transform(unscaled_cancer)
scaled_cancer
standardscaler__Area standardscaler__Smoothness
0 0.984375 1.568466
1 1.908708 -0.826962
2 1.558884 0.942210
3 -0.764464 3.283553
4 1.826229 0.280372
... ... ...
564 2.343856 1.041842
565 1.723842 0.102458
566 0.577953 -0.840484
567 1.735218 1.525767
568 -1.347789 -3.112085

569 rows × 2 columns

It looks like our Smoothness and Area variables have been standardized. Woohoo! But there are two important things to notice about the new scaled_cancer data frame. First, it only keeps the columns from the input to transform (here, unscaled_cancer) that had a preprocessing step applied to them. The default behavior of the ColumnTransformer that we build using make_column_transformer is to drop the remaining columns. This default behavior works well with the rest of sklearn (as we will see below in Section 5.8), but for visualizing the result of preprocessing it can be useful to keep the other columns in our original data frame, such as the Class variable here. To keep other columns, we need to set the remainder argument to "passthrough" in the make_column_transformer function. Furthermore, you can see that the new column names—“standardscaler__Area” and “standardscaler__Smoothness”—include the name of the preprocessing step separated by underscores. This default behavior is useful in sklearn because we sometimes want to apply multiple different preprocessing steps to the same columns; but again, for visualization it can be useful to preserve the original column names. To keep original column names, we need to set the verbose_feature_names_out argument to False.

Note

Only specify the remainder and verbose_feature_names_out arguments when you want to examine the result of your preprocessing step. In most cases, you should leave these arguments at their default values.

preprocessor_keep_all = make_column_transformer(
    (StandardScaler(), make_column_selector(dtype_include="number")),
    remainder="passthrough",
    verbose_feature_names_out=False
)
preprocessor_keep_all.fit(unscaled_cancer)
scaled_cancer_all = preprocessor_keep_all.transform(unscaled_cancer)
scaled_cancer_all
Area Smoothness Class
0 0.984375 1.568466 Malignant
1 1.908708 -0.826962 Malignant
2 1.558884 0.942210 Malignant
3 -0.764464 3.283553 Malignant
4 1.826229 0.280372 Malignant
... ... ... ...
564 2.343856 1.041842 Malignant
565 1.723842 0.102458 Malignant
566 0.577953 -0.840484 Malignant
567 1.735218 1.525767 Malignant
568 -1.347789 -3.112085 Benign

569 rows × 3 columns

You may wonder why we are doing so much work just to center and scale our variables. Can’t we just manually scale and center the Area and Smoothness variables ourselves before building our K-nearest neighbors model? Well, technically yes; but doing so is error-prone. In particular, we might accidentally forget to apply the same centering / scaling when making predictions, or accidentally apply a different centering / scaling than what we used while training. Proper use of a ColumnTransformer helps keep our code simple, readable, and error-free. Furthermore, note that using fit and transform on the preprocessor is required only when you want to inspect the result of the preprocessing steps yourself. You will see further on in Section 5.8 that scikit-learn provides tools to automatically streamline the preprocesser and the model so that you can call fit and transform on the Pipeline as necessary without additional coding effort.

Fig. 5.9 shows the two scatter plots side-by-side—one for unscaled_cancer and one for scaled_cancer. Each has the same new observation annotated with its \(K=3\) nearest neighbors. In the original unstandardized data plot, you can see some odd choices for the three nearest neighbors. In particular, the “neighbors” are visually well within the cloud of benign observations, and the neighbors are all nearly vertically aligned with the new observation (which is why it looks like there is only one black line on this plot). Fig. 5.10 shows a close-up of that region on the unstandardized plot. Here the computation of nearest neighbors is dominated by the much larger-scale area variable. The plot for standardized data on the right in Fig. 5.9 shows a much more intuitively reasonable selection of nearest neighbors. Thus, standardizing the data can change things in an important way when we are using predictive algorithms. Standardizing your data should be a part of the preprocessing you do before predictive modeling and you should always think carefully about your problem domain and whether you need to standardize your data.

Fig. 5.9 Comparison of K = 3 nearest neighbors with unstandardized and standardized data.#

Fig. 5.10 Close-up of three nearest neighbors for unstandardized data.#

5.7.2. Balancing#

Another potential issue in a data set for a classifier is class imbalance, i.e., when one label is much more common than another. Since classifiers like the K-nearest neighbors algorithm use the labels of nearby points to predict the label of a new point, if there are many more data points with one label overall, the algorithm is more likely to pick that label in general (even if the “pattern” of data suggests otherwise). Class imbalance is actually quite a common and important problem: from rare disease diagnosis to malicious email detection, there are many cases in which the “important” class to identify (presence of disease, malicious email) is much rarer than the “unimportant” class (no disease, normal email).

To better illustrate the problem, let’s revisit the scaled breast cancer data, cancer; except now we will remove many of the observations of malignant tumors, simulating what the data would look like if the cancer was rare. We will do this by picking only 3 observations from the malignant group, and keeping all of the benign observations. We choose these 3 observations using the .head() method, which takes the number of rows to select from the top. We will then use the concat function from pandas to glue the two resulting filtered data frames back together. The concat function concatenates data frames along an axis. By default, it concatenates the data frames vertically along axis=0 yielding a single taller data frame, which is what we want to do here. If we instead wanted to concatenate horizontally to produce a wider data frame, we would specify axis=1. The new imbalanced data is shown in Fig. 5.11, and we print the counts of the classes using the value_counts function.

rare_cancer = pd.concat((
    cancer[cancer["Class"] == "Benign"],
    cancer[cancer["Class"] == "Malignant"].head(3)
))

rare_plot = alt.Chart(rare_cancer).mark_circle().encode(
    x=alt.X("Perimeter").title("Perimeter (standardized)"),
    y=alt.Y("Concavity").title("Concavity (standardized)"),
    color=alt.Color("Class").title("Diagnosis")
)
rare_plot

Fig. 5.11 Imbalanced data.#

rare_cancer["Class"].value_counts()
Class
Benign       357
Malignant      3
Name: count, dtype: int64

Suppose we now decided to use \(K = 7\) in K-nearest neighbors classification. With only 3 observations of malignant tumors, the classifier will always predict that the tumor is benign, no matter what its concavity and perimeter are! This is because in a majority vote of 7 observations, at most 3 will be malignant (we only have 3 total malignant observations), so at least 4 must be benign, and the benign vote will always win. For example, Fig. 5.12 shows what happens for a new tumor observation that is quite close to three observations in the training data that were tagged as malignant.

Fig. 5.12 Imbalanced data with 7 nearest neighbors to a new observation highlighted.#

Fig. 5.13 shows what happens if we set the background color of each area of the plot to the prediction the K-nearest neighbors classifier would make for a new observation at that location. We can see that the decision is always “benign,” corresponding to the blue color.

Fig. 5.13 Imbalanced data with background color indicating the decision of the classifier and the points represent the labeled data.#

Despite the simplicity of the problem, solving it in a statistically sound manner is actually fairly nuanced, and a careful treatment would require a lot more detail and mathematics than we will cover in this textbook. For the present purposes, it will suffice to rebalance the data by oversampling the rare class. In other words, we will replicate rare observations multiple times in our data set to give them more voting power in the K-nearest neighbors algorithm. In order to do this, we will first separate the classes out into their own data frames by filtering. Then, we will use the sample method on the rare class data frame to increase the number of Malignant observations to be the same as the number of Benign observations. We set the n argument to be the number of Malignant observations we want, and set replace=True to indicate that we are sampling with replacement. Finally, we use the value_counts method to see that our classes are now balanced. Note that sample picks which data to replicate randomly; we will learn more about properly handling randomness in data analysis in Chapter 6.

malignant_cancer = rare_cancer[rare_cancer["Class"] == "Malignant"]
benign_cancer = rare_cancer[rare_cancer["Class"] == "Benign"]
malignant_cancer_upsample = malignant_cancer.sample(
    n=benign_cancer.shape[0], replace=True
)
upsampled_cancer = pd.concat((malignant_cancer_upsample, benign_cancer))
upsampled_cancer["Class"].value_counts()
Class
Malignant    357
Benign       357
Name: count, dtype: int64

Now suppose we train our K-nearest neighbors classifier with \(K=7\) on this balanced data. Fig. 5.14 shows what happens now when we set the background color of each area of our scatter plot to the decision the K-nearest neighbors classifier would make. We can see that the decision is more reasonable; when the points are close to those labeled malignant, the classifier predicts a malignant tumor, and vice versa when they are closer to the benign tumor observations.

Fig. 5.14 Upsampled data with background color indicating the decision of the classifier.#

5.7.3. Missing data#

One of the most common issues in real data sets in the wild is missing data, i.e., observations where the values of some of the variables were not recorded. Unfortunately, as common as it is, handling missing data properly is very challenging and generally relies on expert knowledge about the data, setting, and how the data were collected. One typical challenge with missing data is that missing entries can be informative: the very fact that an entries were missing is related to the values of other variables. For example, survey participants from a marginalized group of people may be less likely to respond to certain kinds of questions if they fear that answering honestly will come with negative consequences. In that case, if we were to simply throw away data with missing entries, we would bias the conclusions of the survey by inadvertently removing many members of that group of respondents. So ignoring this issue in real problems can easily lead to misleading analyses, with detrimental impacts. In this book, we will cover only those techniques for dealing with missing entries in situations where missing entries are just “randomly missing”, i.e., where the fact that certain entries are missing isn’t related to anything else about the observation.

Let’s load and examine a modified subset of the tumor image data that has a few missing entries:

missing_cancer = pd.read_csv("data/wdbc_missing.csv")[["Class", "Radius", "Texture", "Perimeter"]]
missing_cancer["Class"] = missing_cancer["Class"].replace({
   "M" : "Malignant",
   "B" : "Benign"
})
missing_cancer
Class Radius Texture Perimeter
0 Malignant NaN NaN 1.268817
1 Malignant 1.828212 -0.353322 1.684473
2 Malignant 1.578499 NaN 1.565126
3 Malignant -0.768233 0.253509 -0.592166
4 Malignant 1.748758 -1.150804 1.775011
5 Malignant -0.475956 -0.834601 -0.386808
6 Malignant 1.169878 0.160508 1.137124

Recall that K-nearest neighbors classification makes predictions by computing the straight-line distance to nearby training observations, and hence requires access to the values of all variables for all observations in the training data. So how can we perform K-nearest neighbors classification in the presence of missing data? Well, since there are not too many observations with missing entries, one option is to simply remove those observations prior to building the K-nearest neighbors classifier. We can accomplish this by using the dropna method prior to working with the data.

no_missing_cancer = missing_cancer.dropna()
no_missing_cancer
Class Radius Texture Perimeter
1 Malignant 1.828212 -0.353322 1.684473
3 Malignant -0.768233 0.253509 -0.592166
4 Malignant 1.748758 -1.150804 1.775011
5 Malignant -0.475956 -0.834601 -0.386808
6 Malignant 1.169878 0.160508 1.137124

However, this strategy will not work when many of the rows have missing entries, as we may end up throwing away too much data. In this case, another possible approach is to impute the missing entries, i.e., fill in synthetic values based on the other observations in the data set. One reasonable choice is to perform mean imputation, where missing entries are filled in using the mean of the present entries in each variable. To perform mean imputation, we use a SimpleImputer transformer with the default arguments, and use make_column_transformer to indicate which columns need imputation.

from sklearn.impute import SimpleImputer

preprocessor = make_column_transformer(
    (SimpleImputer(), ["Radius", "Texture", "Perimeter"]),
    verbose_feature_names_out=False
)
preprocessor
ColumnTransformer(transformers=[('simpleimputer', SimpleImputer(),
                                 ['Radius', 'Texture', 'Perimeter'])],
                  verbose_feature_names_out=False)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

To visualize what mean imputation does, let’s just apply the transformer directly to the missing_cancer data frame using the fit and transform functions. The imputation step fills in the missing entries with the mean values of their corresponding variables.

preprocessor.fit(missing_cancer)
imputed_cancer = preprocessor.transform(missing_cancer)
imputed_cancer
Radius Texture Perimeter
0 0.846860 -0.384942 1.268817
1 1.828212 -0.353322 1.684473
2 1.578499 -0.384942 1.565126
3 -0.768233 0.253509 -0.592166
4 1.748758 -1.150804 1.775011
5 -0.475956 -0.834601 -0.386808
6 1.169878 0.160508 1.137124

Many other options for missing data imputation can be found in the scikit-learn documentation. However you decide to handle missing data in your data analysis, it is always crucial to think critically about the setting, how the data were collected, and the question you are answering.

5.8. Putting it together in a Pipeline#

The scikit-learn package collection also provides the Pipeline, a way to chain together multiple data analysis steps without a lot of otherwise necessary code for intermediate steps. To illustrate the whole workflow, let’s start from scratch with the wdbc_unscaled.csv data. First we will load the data, create a model, and specify a preprocessor for the data.

# load the unscaled cancer data, make Class readable
unscaled_cancer = pd.read_csv("data/wdbc_unscaled.csv")
unscaled_cancer["Class"] = unscaled_cancer["Class"].replace({
   "M" : "Malignant",
   "B" : "Benign"
})
unscaled_cancer

# create the K-NN model
knn = KNeighborsClassifier(n_neighbors=7)

# create the centering / scaling preprocessor
preprocessor = make_column_transformer(
    (StandardScaler(), ["Area", "Smoothness"]),
)

Next we place these steps in a Pipeline using the make_pipeline function. The make_pipeline function takes a list of steps to apply in your data analysis; in this case, we just have the preprocessor and knn steps. Finally, we call fit on the pipeline. Notice that we do not need to separately call fit and transform on the preprocessor; the pipeline handles doing this properly for us. Also notice that when we call fit on the pipeline, we can pass the whole unscaled_cancer data frame to the X argument, since the preprocessing step drops all the variables except the two we listed: Area and Smoothness. For the y response variable argument, we pass the unscaled_cancer["Class"] series as before.

from sklearn.pipeline import make_pipeline

knn_pipeline = make_pipeline(preprocessor, knn)
knn_pipeline.fit(
    X=unscaled_cancer,
    y=unscaled_cancer["Class"]
)
knn_pipeline
Pipeline(steps=[('columntransformer',
                 ColumnTransformer(transformers=[('standardscaler',
                                                  StandardScaler(),
                                                  ['Area', 'Smoothness'])])),
                ('kneighborsclassifier', KNeighborsClassifier(n_neighbors=7))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

As before, the fit object lists the function that trains the model. But now the fit object also includes information about the overall workflow, including the standardization preprocessing step. In other words, when we use the predict function with the knn_pipeline object to make a prediction for a new observation, it will first apply the same preprocessing steps to the new observation. As an example, we will predict the class label of two new observations: one with Area = 500 and Smoothness = 0.075, and one with Area = 1500 and Smoothness = 0.1.

new_observation = pd.DataFrame({"Area": [500, 1500], "Smoothness": [0.075, 0.1]})
prediction = knn_pipeline.predict(new_observation)
prediction
array(['Benign', 'Malignant'], dtype=object)

The classifier predicts that the first observation is benign, while the second is malignant. Fig. 5.15 visualizes the predictions that this trained K-nearest neighbors model will make on a large range of new observations. Although you have seen colored prediction map visualizations like this a few times now, we have not included the code to generate them, as it is a little bit complicated. For the interested reader who wants a learning challenge, we now include it below. The basic idea is to create a grid of synthetic new observations using the meshgrid function from numpy, predict the label of each, and visualize the predictions with a colored scatter having a very high transparency (low opacity value) and large point radius. See if you can figure out what each line is doing!

Note

Understanding this code is not required for the remainder of the textbook. It is included for those readers who would like to use similar visualizations in their own data analyses.

import numpy as np

# create the grid of area/smoothness vals, and arrange in a data frame
are_grid = np.linspace(
    unscaled_cancer["Area"].min() * 0.95, unscaled_cancer["Area"].max() * 1.05, 50
)
smo_grid = np.linspace(
    unscaled_cancer["Smoothness"].min() * 0.95, unscaled_cancer["Smoothness"].max() * 1.05, 50
)
asgrid = np.array(np.meshgrid(are_grid, smo_grid)).reshape(2, -1).T
asgrid = pd.DataFrame(asgrid, columns=["Area", "Smoothness"])

# use the fit workflow to make predictions at the grid points
knnPredGrid = knn_pipeline.predict(asgrid)

# bind the predictions as a new column with the grid points
prediction_table = asgrid.copy()
prediction_table["Class"] = knnPredGrid

# plot:
# 1. the colored scatter of the original data
unscaled_plot = alt.Chart(unscaled_cancer).mark_point(
    opacity=0.6,
    filled=True,
    size=40
).encode(
    x=alt.X("Area")
        .scale(
            nice=False,
            domain=(
                unscaled_cancer["Area"].min() * 0.95,
                unscaled_cancer["Area"].max() * 1.05
            )
        ),
    y=alt.Y("Smoothness")
        .scale(
            nice=False,
            domain=(
                unscaled_cancer["Smoothness"].min() * 0.95,
                unscaled_cancer["Smoothness"].max() * 1.05
            )
        ),
    color=alt.Color("Class").title("Diagnosis")
)

# 2. the faded colored scatter for the grid points
prediction_plot = alt.Chart(prediction_table).mark_point(
    opacity=0.05,
    filled=True,
    size=300
).encode(
    x="Area",
    y="Smoothness",
    color=alt.Color("Class").title("Diagnosis")
)
unscaled_plot + prediction_plot

Fig. 5.15 Scatter plot of smoothness versus area where background color indicates the decision of the classifier.#

5.9. Exercises#

Practice exercises for the material covered in this chapter can be found in the accompanying worksheets repository in the “Classification I: training and predicting” row. You can launch an interactive version of the worksheet in your browser by clicking the “launch binder” button. You can also preview a non-interactive version of the worksheet by clicking “view worksheet.” If you instead decide to download the worksheet and run it on your own machine, make sure to follow the instructions for computer setup found in Chapter 13. This will ensure that the automated feedback and guidance that the worksheets provide will function as intended.

5.10. References#

BLB+13

Lars Buitinck, Gilles Louppe, Mathieu Blondel, Fabian Pedregosa, Andreas Mueller, Olivier Grisel, Vlad Niculae, Peter Prettenhofer, Alexandre Gramfort, Jaques Grobler, Robert Layton, Jake VanderPlas, Arnaud Joly, Brian Holt, and Gaël Varoquaux. API design for machine learning software: experiences from the scikit-learn project. In ECML PKDD Workshop: Languages for Data Mining and Machine Learning, 108–122. 2013.

CH67

Thomas Cover and Peter Hart. Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1):21–27, 1967.

FH51

Evelyn Fix and Joseph Hodges. Discriminatory analysis. nonparametric discrimination: consistency properties. Technical Report, USAF School of Aviation Medicine, Randolph Field, Texas, 1951.

SWM93

William Nick Street, William Wolberg, and Olvi Mangasarian. Nuclear feature extraction for breast tumor diagnosis. In International Symposium on Electronic Imaging: Science and Technology. 1993.

StanfordHCare21

Stanford Health Care. What is cancer? 2021. URL: https://stanfordhealthcare.org/medical-conditions/cancer/cancer.html.