1
1
"""
2
- ===========================
3
- make_imbalance function
4
- ===========================
2
+ ============================
3
+ Create an imbalanced dataset
4
+ ============================
5
5
6
- An illustration of the make_imbalance function
6
+ An illustration of the :func:`imblearn.datasets.make_imbalance` function to
7
+ create an imbalanced dataset from a balanced dataset. We show the ability of
8
+ :func:`imblearn.datasets.make_imbalance` of dealing with Pandas DataFrame.
7
9
8
10
"""
9
11
14
16
15
17
from collections import Counter
16
18
19
+ import pandas as pd
17
20
import matplotlib .pyplot as plt
21
+
18
22
from sklearn .datasets import make_moons
19
23
20
24
from imblearn .datasets import make_imbalance
21
25
22
26
print (__doc__ )
23
27
24
-
25
- def plot_decoration (ax ):
26
- ax .spines ['top' ].set_visible (False )
27
- ax .spines ['right' ].set_visible (False )
28
- ax .get_xaxis ().tick_bottom ()
29
- ax .get_yaxis ().tick_left ()
30
- ax .spines ['left' ].set_position (('outward' , 10 ))
31
- ax .spines ['bottom' ].set_position (('outward' , 10 ))
32
- ax .set_xlim ([- 4 , 4 ])
33
-
34
-
35
28
# Generate the dataset
36
29
X , y = make_moons (n_samples = 200 , shuffle = True , noise = 0.5 , random_state = 10 )
30
+ X = pd .DataFrame (X , columns = ["feature 1" , "feature 2" ])
37
31
38
32
# Two subplots, unpack the axes array immediately
39
33
f , axs = plt .subplots (2 , 3 )
40
34
41
35
axs = [a for ax in axs for a in ax ]
42
36
43
- axs [0 ].scatter (X [y == 0 , 0 ], X [y == 0 , 1 ], label = "Class #0" , alpha = 0.5 )
44
- axs [0 ].scatter (X [y == 1 , 0 ], X [y == 1 , 1 ], label = "Class #1" , alpha = 0.5 )
37
+ X .plot .scatter (
38
+ x = 'feature 1' , y = 'feature 2' , c = y , ax = axs [0 ], colormap = 'viridis' ,
39
+ colorbar = False
40
+ )
45
41
axs [0 ].set_title ('Original set' )
46
- plot_decoration (axs [0 ])
47
42
48
43
49
44
def ratio_func (y , multiplier , minority_class ):
@@ -58,10 +53,11 @@ def ratio_func(y, multiplier, minority_class):
58
53
X_ , y_ = make_imbalance (X , y , sampling_strategy = ratio_func ,
59
54
** {"multiplier" : multiplier ,
60
55
"minority_class" : 1 })
61
- ax .scatter (X_ [y_ == 0 , 0 ], X_ [y_ == 0 , 1 ], label = "Class #0" , alpha = 0.5 )
62
- ax .scatter (X_ [y_ == 1 , 0 ], X_ [y_ == 1 , 1 ], label = "Class #1" , alpha = 0.5 )
63
- ax .set_title ('sampling_strategy = {}' .format (multiplier ))
64
- plot_decoration (ax )
56
+ X_ .plot .scatter (
57
+ x = 'feature 1' , y = 'feature 2' , c = y_ , ax = ax , colormap = 'viridis' ,
58
+ colorbar = False
59
+ )
60
+ ax .set_title ('Sampling ratio = {}' .format (multiplier ))
65
61
66
62
plt .tight_layout ()
67
63
plt .show ()
0 commit comments