|
| 1 | +"""Test the module repeated edited nearest neighbour.""" |
| 2 | +from __future__ import print_function |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from numpy.testing import assert_raises |
| 8 | +from numpy.testing import assert_equal |
| 9 | +from numpy.testing import assert_array_equal |
| 10 | +from numpy.testing import assert_warns |
| 11 | + |
| 12 | +from sklearn.datasets import make_classification |
| 13 | +from sklearn.utils.estimator_checks import check_estimator |
| 14 | + |
| 15 | +from unbalanced_dataset.under_sampling import RepeatedEditedNearestNeighbours |
| 16 | + |
| 17 | +# Generate a global dataset to use |
| 18 | +RND_SEED = 0 |
| 19 | +X, Y = make_classification(n_classes=2, class_sep=2, weights=[0.1, 0.9], |
| 20 | + n_informative=3, n_redundant=1, flip_y=0, |
| 21 | + n_features=20, n_clusters_per_class=1, |
| 22 | + n_samples=5000, random_state=RND_SEED) |
| 23 | + |
| 24 | + |
| 25 | +def test_renn_init(): |
| 26 | + """Test the initialisation of the object""" |
| 27 | + |
| 28 | + # Define a ratio |
| 29 | + verbose = True |
| 30 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED, |
| 31 | + verbose=verbose) |
| 32 | + |
| 33 | + assert_equal(renn.size_ngh, 3) |
| 34 | + assert_equal(renn.kind_sel, 'all') |
| 35 | + assert_equal(renn.n_jobs, -1) |
| 36 | + assert_equal(renn.rs_, RND_SEED) |
| 37 | + assert_equal(renn.verbose, verbose) |
| 38 | + assert_equal(renn.min_c_, None) |
| 39 | + assert_equal(renn.maj_c_, None) |
| 40 | + assert_equal(renn.stats_c_, {}) |
| 41 | + |
| 42 | + |
| 43 | +def test_renn_fit_single_class(): |
| 44 | + """Test either if an error when there is a single class""" |
| 45 | + |
| 46 | + # Create the object |
| 47 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED) |
| 48 | + # Resample the data |
| 49 | + # Create a wrong y |
| 50 | + y_single_class = np.zeros((X.shape[0], )) |
| 51 | + assert_raises(RuntimeError, renn.fit, X, y_single_class) |
| 52 | + |
| 53 | + |
| 54 | +def test_renn_fit(): |
| 55 | + """Test the fitting method""" |
| 56 | + |
| 57 | + # Create the object |
| 58 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED) |
| 59 | + # Fit the data |
| 60 | + renn.fit(X, Y) |
| 61 | + |
| 62 | + # Check if the data information have been computed |
| 63 | + assert_equal(renn.min_c_, 0) |
| 64 | + assert_equal(renn.maj_c_, 1) |
| 65 | + assert_equal(renn.stats_c_[0], 500) |
| 66 | + assert_equal(renn.stats_c_[1], 4500) |
| 67 | + |
| 68 | + |
| 69 | +def test_renn_transform_wt_fit(): |
| 70 | + """Test either if an error is raised when transform is called before |
| 71 | + fitting""" |
| 72 | + |
| 73 | + # Create the object |
| 74 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED) |
| 75 | + assert_raises(RuntimeError, renn.transform, X, Y) |
| 76 | + |
| 77 | + |
| 78 | +def test_renn_fit_transform(): |
| 79 | + """Test the fit transform routine""" |
| 80 | + |
| 81 | + # Resample the data |
| 82 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED) |
| 83 | + X_resampled, y_resampled = renn.fit_transform(X, Y) |
| 84 | + |
| 85 | + currdir = os.path.dirname(os.path.abspath(__file__)) |
| 86 | + X_gt = np.load(os.path.join(currdir, 'data', 'renn_x.npy')) |
| 87 | + y_gt = np.load(os.path.join(currdir, 'data', 'renn_y.npy')) |
| 88 | + assert_array_equal(X_resampled, X_gt) |
| 89 | + assert_array_equal(y_resampled, y_gt) |
| 90 | + |
| 91 | + |
| 92 | +def test_renn_fit_transform_with_indices(): |
| 93 | + """Test the fit transform routine with indices support""" |
| 94 | + |
| 95 | + # Resample the data |
| 96 | + renn = RepeatedEditedNearestNeighbours(return_indices=True, |
| 97 | + random_state=RND_SEED) |
| 98 | + X_resampled, y_resampled, idx_under = renn.fit_transform(X, Y) |
| 99 | + |
| 100 | + currdir = os.path.dirname(os.path.abspath(__file__)) |
| 101 | + X_gt = np.load(os.path.join(currdir, 'data', 'renn_x.npy')) |
| 102 | + y_gt = np.load(os.path.join(currdir, 'data', 'renn_y.npy')) |
| 103 | + idx_gt = np.load(os.path.join(currdir, 'data', 'renn_idx.npy')) |
| 104 | + assert_array_equal(X_resampled, X_gt) |
| 105 | + assert_array_equal(y_resampled, y_gt) |
| 106 | + assert_array_equal(idx_under, idx_gt) |
| 107 | + |
| 108 | + |
| 109 | +def test_renn_fit_transform_mode(): |
| 110 | + """Test the fit transform routine using the mode as selection""" |
| 111 | + |
| 112 | + # Resample the data |
| 113 | + renn = RepeatedEditedNearestNeighbours(random_state=RND_SEED, |
| 114 | + kind_sel='mode') |
| 115 | + X_resampled, y_resampled = renn.fit_transform(X, Y) |
| 116 | + |
| 117 | + currdir = os.path.dirname(os.path.abspath(__file__)) |
| 118 | + X_gt = np.load(os.path.join(currdir, 'data', 'renn_x_mode.npy')) |
| 119 | + y_gt = np.load(os.path.join(currdir, 'data', 'renn_y_mode.npy')) |
| 120 | + assert_array_equal(X_resampled, X_gt) |
| 121 | + assert_array_equal(y_resampled, y_gt) |
0 commit comments