本文整理汇总了Python中imblearn.under_sampling.NearMiss类的典型用法代码示例。如果您正苦于以下问题:Python NearMiss类的具体用法?Python NearMiss怎么用?Python NearMiss使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NearMiss类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nm3_fit_sample_nn_obj
def test_nm3_fit_sample_nn_obj():
"""Test fit-sample with nn object"""
# Define the parameter for the under-sampling
ratio = 'auto'
# Create the object
nn = NearestNeighbors(n_neighbors=3)
nn3 = NearestNeighbors(n_neighbors=3)
nm3 = NearMiss(
ratio=ratio,
random_state=RND_SEED,
version=VERSION_NEARMISS,
return_indices=True,
n_neighbors=nn,
n_neighbors_ver3=nn3)
# Fit and sample
X_resampled, y_resampled, idx_under = nm3.fit_sample(X, Y)
X_gt = np.array([[0.91464286, 1.61369212], [-0.80809175, -1.09917302],
[-0.20497017, -0.26630228], [1.17737838, -0.2002118],
[-0.60413357, 0.24628718], [0.03142011, 0.12323596],
[1.15157493, -1.2981518], [-0.54619583, 1.73009918],
[0.99272351, -0.11631728]])
y_gt = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
idx_gt = np.array([3, 10, 11, 0, 2, 3, 5, 1, 4])
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
assert_array_equal(idx_under, idx_gt)
示例2: test_nm_fit_resample_auto
def test_nm_fit_resample_auto():
sampling_strategy = 'auto'
X_gt = [
np.array([[0.91464286, 1.61369212], [-0.80809175, -1.09917302], [
-0.20497017, -0.26630228
], [-0.05903827, 0.10947647], [0.03142011, 0.12323596],
[-0.60413357, 0.24628718], [0.50701028, -0.17636928],
[0.4960075, 0.86130762], [0.45713638, 1.31069295]]),
np.array([[0.91464286, 1.61369212], [-0.80809175, -1.09917302], [
-0.20497017, -0.26630228
], [-0.05903827, 0.10947647], [0.03142011, 0.12323596],
[-0.60413357, 0.24628718], [0.50701028, -0.17636928],
[0.4960075, 0.86130762], [0.45713638, 1.31069295]]),
np.array([[0.91464286, 1.61369212], [-0.80809175, -1.09917302], [
-0.20497017, -0.26630228
], [1.17737838, -0.2002118], [-0.60413357, 0.24628718],
[0.03142011, 0.12323596], [1.15157493, -1.2981518],
[-0.54619583, 1.73009918], [0.99272351, -0.11631728]])
]
y_gt = [
np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]),
np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]),
np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
]
for version_idx, version in enumerate(VERSION_NEARMISS):
nm = NearMiss(sampling_strategy=sampling_strategy, version=version)
X_resampled, y_resampled = nm.fit_resample(X, Y)
assert_array_equal(X_resampled, X_gt[version_idx])
assert_array_equal(y_resampled, y_gt[version_idx])
示例3: test_multiclass_fit_sample
def test_multiclass_fit_sample():
"""Test fit sample method with multiclass target"""
# Make y to be multiclass
y = Y.copy()
y[0:1000] = 2
# Resample the data
nm = NearMiss(random_state=RND_SEED, version=VERSION_NEARMISS)
X_resampled, y_resampled = nm.fit_sample(X, y)
# Check the size of y
count_y_res = Counter(y_resampled)
assert_equal(count_y_res[0], 400)
assert_equal(count_y_res[1], 166)
assert_equal(count_y_res[2], 144)
示例4: test_nm2_fit
def test_nm2_fit():
"""Test the fitting method"""
# Define the parameter for the under-sampling
ratio = 'auto'
# Create the object
nm2 = NearMiss(ratio=ratio, random_state=RND_SEED,
version=VERSION_NEARMISS)
# Fit the data
nm2.fit(X, Y)
# Check if the data information have been computed
assert_equal(nm2.min_c_, 0)
assert_equal(nm2.maj_c_, 1)
assert_equal(nm2.stats_c_[0], 500)
assert_equal(nm2.stats_c_[1], 4500)
示例5: test_nm2_fit_sample_half
def test_nm2_fit_sample_half():
"""Test fit and sample routines with .5 ratio"""
# Define the parameter for the under-sampling
ratio = .5
# Create the object
nm2 = NearMiss(ratio=ratio, random_state=RND_SEED,
version=VERSION_NEARMISS)
# Fit and sample
X_resampled, y_resampled = nm2.fit_sample(X, Y)
currdir = os.path.dirname(os.path.abspath(__file__))
X_gt = np.load(os.path.join(currdir, 'data', 'nm2_x_05.npy'))
y_gt = np.load(os.path.join(currdir, 'data', 'nm2_y_05.npy'))
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
示例6: test_nm2_fit_sample_auto_indices
def test_nm2_fit_sample_auto_indices():
"""Test fit and sample routines with auto ratio and indices support"""
# Define the parameter for the under-sampling
ratio = 'auto'
# Create the object
nm2 = NearMiss(ratio=ratio, random_state=RND_SEED,
version=VERSION_NEARMISS, return_indices=True)
# Fit and sample
X_resampled, y_resampled, idx_under = nm2.fit_sample(X, Y)
currdir = os.path.dirname(os.path.abspath(__file__))
X_gt = np.load(os.path.join(currdir, 'data', 'nm2_x.npy'))
y_gt = np.load(os.path.join(currdir, 'data', 'nm2_y.npy'))
idx_gt = np.load(os.path.join(currdir, 'data', 'nm2_idx.npy'))
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
assert_array_equal(idx_under, idx_gt)
示例7: test_nm3_fit_sample_auto
def test_nm3_fit_sample_auto():
"""Test fit and sample routines with auto ratio"""
# Define the parameter for the under-sampling
ratio = 'auto'
# Create the object
nm3 = NearMiss(
ratio=ratio, random_state=RND_SEED, version=VERSION_NEARMISS)
# Fit and sample
X_resampled, y_resampled = nm3.fit_sample(X, Y)
X_gt = np.array([[0.91464286, 1.61369212], [-0.80809175, -1.09917302],
[-0.20497017, -0.26630228], [1.17737838, -0.2002118],
[-0.60413357, 0.24628718], [0.03142011, 0.12323596],
[1.15157493, -1.2981518], [-0.54619583, 1.73009918],
[0.99272351, -0.11631728]])
y_gt = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)
示例8: test_nm_wrong_nn_obj
def test_nm_wrong_nn_obj():
sampling_strategy = 'auto'
nn = 'rnd'
nm = NearMiss(
sampling_strategy=sampling_strategy,
version=VERSION_NEARMISS,
return_indices=True,
n_neighbors=nn)
with raises(ValueError, match="has to be one of"):
nm.fit_resample(X, Y)
nn3 = 'rnd'
nn = NearestNeighbors(n_neighbors=3)
nm3 = NearMiss(
sampling_strategy=sampling_strategy,
version=3,
return_indices=True,
n_neighbors=nn,
n_neighbors_ver3=nn3)
with raises(ValueError, match="has to be one of"):
nm3.fit_resample(X, Y)
示例9: test_nearmiss_wrong_version
def test_nearmiss_wrong_version():
version = 1000
nm = NearMiss(version=version)
with raises(ValueError, match="must be 1, 2 or 3"):
nm.fit_resample(X, Y)
示例10: test_deprecation_random_state
def test_deprecation_random_state():
nm = NearMiss(random_state=0)
with warns(
DeprecationWarning, match="'random_state' is deprecated from 0.4"):
nm.fit_resample(X, Y)