91  Near-Miss

The NearMiss method is a method that is used to remove observations from the majority classes (Mani and Zhang 2003). Like with many of the other methods in this section, we are going to focus on points based on their distance to instances of other classes.

What we are doing is that we are removing points from the majority class that are far away from the minority class. We are keeping points from the majority class that are close to the minority class. In other words, we are only keeping the β€œnear misses”. While the idea is fairly straightforward, it has 3 different recognized variants based on how we define a near miss.

While the distinction between the 1 and 2 variants might seem small, what you are getting is that with NearMiss-1, you are preserving samples that are most similar to the minority class, and with NearMiss-2, you are preserving samples that are least similar to the minority class. NearMiss-3 is trying to have a more blended selection.

This method, like many of the other methods, is technically not restricted to be used on the majority class, and could, in theory, be used on any number of classes in the data set.

Doing so requires settling a question the original description leaves open. All three variants are defined by distances from majority observations to the minority class, which is unambiguous only when there are two classes. NearMiss resolves this with the one-vs-rest strategy from Section 86.2.

Figure 91.1 walks through NearMiss-1, which is the most direct of the three.

Four scatter plots in a 2x2 grid showing a gray majority class in the lower left and a pink minority class in the upper right. Panel 1: three lines run from one majority point to the three minority points closest to it. Panel 2: every majority point is drawn with a size proportional to its average distance to the minority class, so points far from the minority class in the lower left are large. Panel 3: the highest-scoring majority points are marked with dark crosses, covering most of the lower left. Panel 4: only the majority points nearest the minority class remain, and they hug the boundary.
Figure 91.1: NearMiss-1 scores every majority observation by its average distance to the three closest minority observations, and keeps the lowest scores.

One of the convenient things about this method is that it technically works by giving the observations an ordering, And we just have to find the cut point that determines how many points to retain.

Like all the methods in this section, this changes the distribution of the training data set, which is covered in Section 86.3.

TipAnimated version

The same walkthrough, one step at a time, is available as an animated slide.

91.2 Pros and Cons

91.2.1 Pros

91.2.2 Cons

91.3 R Examples

We will be using the same subset of the ames data set as in the up-sampling chapter.

library(recipes)
library(themis)
library(modeldata)
library(dplyr)
data("ames")

ames_imbalance <- ames |>
  filter(MS_Zoning %in% c("Residential_Low_Density", "Residential_Medium_Density")) |>
  mutate(MS_Zoning = droplevels(MS_Zoning)) |>
  select(MS_Zoning, Lot_Area, Year_Built, Gr_Liv_Area, Full_Bath)

ames_imbalance |>
  count(MS_Zoning)
# A tibble: 2 Γ— 2
  MS_Zoning                      n
  <fct>                      <int>
1 Residential_Low_Density     2273
2 Residential_Medium_Density   462

{themis} provides step_nearmiss(), and the version argument selects which of the three variants to use.

recipe(MS_Zoning ~ ., data = ames_imbalance) |>
  step_nearmiss(MS_Zoning, version = 1) |>
  prep() |>
  bake(new_data = NULL) |>
  count(MS_Zoning)
# A tibble: 2 Γ— 2
  MS_Zoning                      n
  <fct>                      <int>
1 Residential_Low_Density      462
2 Residential_Medium_Density   462

91.4 Python Examples