Skip to content

Commit b63d336

Browse files
committed
iter
1 parent e000d93 commit b63d336

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

examples/under-sampling/plot_illustration_nearmiss.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,71 @@
33
Sample selection in NearMiss
44
============================
55
6-
This example illustrates the different way of selecting example in NearMiss.
7-
6+
This example illustrates the different way of selecting example in
7+
:class:`~imblearn.under_sampling.NearMiss`.
88
"""
99

1010
# Authors: Guillaume Lemaitre <[email protected]>
1111
# License: MIT
1212

13-
import matplotlib.pyplot as plt
14-
import numpy as np
13+
# %%
14+
print(__doc__)
1515

16-
from sklearn.neighbors import NearestNeighbors
16+
import seaborn as sns
1717

18-
print(__doc__)
18+
sns.set_context("poster")
1919

20-
rng = np.random.RandomState(18)
20+
# %% [markdown]
21+
# We define a function allowing to make some nice decoration on the plot.
2122

22-
###############################################################################
23-
# This function allows to make nice plotting
23+
# %%
2424

2525

2626
def make_plot_despine(ax):
27-
ax.spines["top"].set_visible(False)
28-
ax.spines["right"].set_visible(False)
29-
ax.get_xaxis().tick_bottom()
30-
ax.get_yaxis().tick_left()
31-
ax.spines["left"].set_position(("outward", 10))
32-
ax.spines["bottom"].set_position(("outward", 10))
33-
ax.set_xlim([0.0, 3.5])
34-
ax.set_ylim([0.0, 3.5])
27+
sns.despine(ax=ax, offset=10)
28+
ax.set_xlim([0, 3.5])
29+
ax.set_ylim([0, 3.5])
30+
ax.set_xticks(np.arange(0, 3.6, 0.5))
31+
ax.set_yticks(np.arange(0, 3.6, 0.5))
3532
ax.set_xlabel(r"$X_1$")
3633
ax.set_ylabel(r"$X_2$")
37-
ax.legend()
34+
ax.legend(loc="upper left")
3835

3936

40-
###############################################################################
37+
# %% [markdown]
4138
# We can start by generating some data to later illustrate the principle of
42-
# each NearMiss heuritic rules.
39+
# each :class:`~imblearn.under_sampling.NearMiss` heuristic rules.
40+
41+
# %%
42+
import numpy as np
43+
44+
rng = np.random.RandomState(18)
4345

44-
# minority class
4546
X_minority = np.transpose(
4647
[[1.1, 1.3, 1.15, 0.8, 0.8, 0.6, 0.55], [1.0, 1.5, 1.7, 2.5, 2.0, 1.2, 0.55]]
4748
)
48-
# majority class
4949
X_majority = np.transpose(
5050
[
5151
[2.1, 2.12, 2.13, 2.14, 2.2, 2.3, 2.5, 2.45],
5252
[1.5, 2.1, 2.7, 0.9, 1.0, 1.4, 2.4, 2.9],
5353
]
5454
)
5555

56-
###############################################################################
56+
# %% [mardown]
5757
# NearMiss-1
58-
###############################################################################
59-
60-
###############################################################################
58+
# ----------
59+
#
6160
# NearMiss-1 selects samples from the majority class for which the average
6261
# distance to some nearest neighbours is the smallest. In the following
6362
# example, we use a 3-NN to compute the average distance on 2 specific samples
6463
# of the majority class. Therefore, in this case the point linked by the
6564
# green-dashed line will be selected since the average distance is smaller.
6665

67-
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
66+
# %%
67+
import matplotlib.pyplot as plt
68+
from sklearn.neighbors import NearestNeighbors
69+
70+
fig, ax = plt.subplots(figsize=(8, 8))
6871
ax.scatter(
6972
X_minority[:, 0],
7073
X_minority[:, 1],
@@ -99,18 +102,18 @@ def make_plot_despine(ax):
99102
ax.set_title("NearMiss-1")
100103
make_plot_despine(ax)
101104

102-
###############################################################################
105+
# %% [mardown]
103106
# NearMiss-2
104-
###############################################################################
105-
106-
###############################################################################
107+
# ----------
108+
#
107109
# NearMiss-2 selects samples from the majority class for which the average
108110
# distance to the farthest neighbors is the smallest. With the same
109111
# configuration as previously presented, the sample linked to the green-dashed
110112
# line will be selected since its distance the 3 farthest neighbors is the
111113
# smallest.
112114

113-
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
115+
# %%
116+
fig, ax = plt.subplots(figsize=(8, 8))
114117
ax.scatter(
115118
X_minority[:, 0],
116119
X_minority[:, 1],
@@ -147,17 +150,17 @@ def make_plot_despine(ax):
147150
ax.set_title("NearMiss-2")
148151
make_plot_despine(ax)
149152

150-
###############################################################################
153+
# %% [mardown]
151154
# NearMiss-3
152-
###############################################################################
153-
154-
###############################################################################
155+
# ----------
156+
#
155157
# NearMiss-3 can be divided into 2 steps. First, a nearest-neighbors is used to
156158
# short-list samples from the majority class (i.e. correspond to the
157159
# highlighted samples in the following plot). Then, the sample with the largest
158160
# average distance to the *k* nearest-neighbors are selected.
159161

160-
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
162+
# %%
163+
fig, ax = plt.subplots(figsize=(8.5, 8.5))
161164
ax.scatter(
162165
X_minority[:, 0],
163166
X_minority[:, 1],

0 commit comments

Comments
 (0)