Skip to main content
Ctrl+K
Version 0.13.0 - Home Version 0.13.0 - Home
  • Getting Started
  • User Guide
  • API reference
  • Examples
  • Release history
    • About us
  • GitHub
  • Getting Started
  • User Guide
  • API reference
  • Examples
  • Release history
  • About us
  • GitHub

章节导航

  • 欠采样方法
    • ClusterCentroids
    • CondensedNearestNeighbour
    • EditedNearestNeighbours
    • RepeatedEditedNearestNeighbours
    • AllKNN
    • InstanceHardnessThreshold
    • NearMiss
    • NeighbourhoodCleaningRule
    • OneSidedSelection
    • RandomUnderSampler
    • TomekLinks
  • 过采样方法
    • RandomOverSampler
    • SMOTE
    • SMOTENC
    • SMOTEN
    • ADASYN
    • BorderlineSMOTE
    • KMeansSMOTE
    • SVMSMOTE
  • 过采样和欠采样方法的组合
    • SMOTEENN
    • SMOTETomek
  • 集成方法
    • EasyEnsembleClassifier
    • RUSBoostClassifier
    • BalancedBaggingClassifier
    • BalancedRandomForestClassifier
  • Keras的批量生成器
    • BalancedBatchGenerator
    • balanced_batch_generator
  • TensorFlow的批量生成器
    • balanced_batch_generator
  • Miscellaneous
    • FunctionSampler
  • Pipeline
    • Pipeline
    • make_pipeline
  • Metrics
    • classification_report_imbalanced
    • sensitivity_specificity_support
    • sensitivity_score
    • specificity_score
    • geometric_mean_score
    • macro_averaged_mean_absolute_error
    • make_index_balanced_accuracy
    • ValueDifferenceMetric
  • Datasets
    • make_imbalance
    • fetch_datasets
  • Utilities
    • parametrize_with_checks
    • check_neighbors_object
    • check_sampling_strategy
    • check_target_type
    • parametrize_with_checks
  • API reference
  • Under-sampling methods
  • TomekLinks

TomekLinks#

class imblearn.under_sampling.TomekLinks(*, sampling_strategy='auto', n_jobs=None)[source]#

通过移除Tomek的链接进行欠采样。

了解更多信息,请参阅用户指南。

Parameters:
sampling_strategystr, list or callable

采样信息以对数据集进行采样。

  • 当 str 时,指定重采样所针对的类别。请注意,每个类别的样本数量不会相等。可能的选择有:

    'majority': 仅对多数类进行重采样;

    'not minority': 对除少数类之外的所有类进行重采样;

    'not majority': 重新采样除多数类之外的所有类;

    'all': 对所有类别进行重采样;

    'auto': 等同于 'not minority'.

  • 当list时,列表包含重采样所针对的类。

  • 当可调用时,函数接受 y 并返回一个 dict。键对应于目标类别。值对应于每个类别所需的样本数量。

n_jobsint, default=None

在交叉验证循环中使用的CPU核心数量。 None 表示1,除非在 joblib.parallel_backend 上下文中。 -1 表示使用所有处理器。更多详情请参见 术语表。

Attributes:
sampling_strategy_dict

包含用于采样数据集信息的字典。键对应于从中采样的类标签,值是要采样的样本数量。

sample_indices_ndarray of shape (n_new_samples,)

所选样本的索引。

在版本0.4中添加。

n_features_in_int

输入数据集中的特征数量。

在版本0.9中添加。

feature_names_in_ndarray of shape (n_features_in_,)

在fit期间看到的特征名称。仅在X具有全部为字符串的特征名称时定义。

在版本0.10中添加。

另请参阅

EditedNearestNeighbours

通过样本编辑进行欠采样。

CondensedNearestNeighbour

通过样本压缩进行欠采样。

RandomUnderSampler

随机对数据集进行欠采样。

注释

该方法基于[1]。

支持多类重采样。使用了一对多方案,如最初在[1]中提出的那样。

参考文献

[1] (1,2)

I. Tomek, “CNN的两个修改版本,” 在《系统、人与控制论》中,IEEE Transactions on, 卷6, 第769-772页, 1976年。

示例

>>> from collections import Counter
>>> from sklearn.datasets import make_classification
>>> from imblearn.under_sampling import TomekLinks
>>> X, y = make_classification(n_classes=2, class_sep=2,
... weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,
... n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10)
>>> print('Original dataset shape %s' % Counter(y))
Original dataset shape Counter({1: 900, 0: 100})
>>> tl = TomekLinks()
>>> X_res, y_res = tl.fit_resample(X, y)
>>> print('Resampled dataset shape %s' % Counter(y_res))
Resampled dataset shape Counter({1: 897, 0: 100})

方法

fit(X, y, **params)

检查采样器的输入和统计信息。

fit_resample(X, y, **params)

重新采样数据集。

get_feature_names_out([input_features])

获取转换的输出特征名称。

get_metadata_routing()

获取此对象的元数据路由。

get_params([deep])

获取此估计器的参数。

is_tomek(y, nn_index, class_type)

检测样本是否为Tomek的链接。

set_params(**params)

设置此估计器的参数。

fit(X, y, **params)[source]#

检查采样器的输入和统计信息。

在所有情况下,您都应该使用 fit_resample。

Parameters:
X{array-like, dataframe, sparse matrix} of shape (n_samples, n_features)

数据数组。

yarray-like of shape (n_samples,)

目标数组。

Returns:
selfobject

返回实例本身。

fit_resample(X, y, **params)[source]#

重新采样数据集。

Parameters:
X{array-like, dataframe, sparse matrix} of shape (n_samples, n_features)

包含需要采样的数据的矩阵。

yarray-like of shape (n_samples,)

X中每个样本对应的标签。

Returns:
X_resampled{array-like, dataframe, sparse matrix} of shape (n_samples_new, n_features)

包含重采样数据的数组。

y_resampledarray-like of shape (n_samples_new,)

X_resampled 对应的标签。

get_feature_names_out(input_features=None)[source]#

获取转换的输出特征名称。

Parameters:
input_featuresarray-like of str or None, default=None

输入特征。

  • 如果 input_features 是 None,则使用 feature_names_in_ 作为特征名称。如果 feature_names_in_ 未定义,则生成以下输入特征名称: ["x0", "x1", ..., "x(n_features_in_ - 1)"]。

  • 如果 input_features 是类似数组的,那么 input_features 必须 与 feature_names_in_ 匹配,如果 feature_names_in_ 已定义。

Returns:
feature_names_outndarray of str objects

与输入特征相同。

get_metadata_routing()[source]#

获取此对象的元数据路由。

请查看用户指南了解路由机制的工作原理。

Returns:
routingMetadataRequest

一个封装路由信息的MetadataRequest。

get_params(deep=True)[source]#

获取此估计器的参数。

Parameters:
deepbool, default=True

如果为True,将返回此估计器及其包含的子对象的参数。

Returns:
paramsdict

参数名称映射到它们的值。

static is_tomek(y, nn_index, class_type)[source]#

检测样本是否为Tomek链接。

更准确地说,它使用目标向量和每个样本点的第一个邻居,并寻找Tomek对。返回一个布尔向量,其中多数Tomek链接为True。

Parameters:
yndarray of shape (n_samples,)

数据集的目标向量,用于跟踪样本是否属于少数类。

nn_indexndarray of shape (len(y),)

样本点的最近邻的索引。

class_typeint or str

少数类别的标签。

Returns:
is_tomekndarray of shape (len(y), )

长度为样本数量的布尔向量,其中多数样本为Tomek链接的样本标记为True。

set_params(**params)[source]#

设置此估计器的参数。

该方法适用于简单的估计器以及嵌套对象(如Pipeline)。后者具有__形式的参数,以便可以更新嵌套对象的每个组件。

Parameters:
**paramsdict

估计器参数。

Returns:
selfestimator instance

估计器实例。

使用imblearn.under_sampling.TomekLinks的示例#

如何在imbalanced-learn中使用sampling_strategy

How to use sampling_strategy in imbalanced-learn

Tomek链接定义的图示

Illustration of the definition of a Tomek link

比较欠采样采样器

Compare under-sampling samplers

上一页

RandomUnderSampler

下一步

过采样方法

On this page
  • TomekLinks
    • TomekLinks.fit
    • TomekLinks.fit_resample
    • TomekLinks.get_feature_names_out
    • TomekLinks.get_metadata_routing
    • TomekLinks.get_params
    • TomekLinks.is_tomek
    • TomekLinks.set_params
  • 使用 imblearn.under_sampling.TomekLinks 的示例
Edit on GitHub

本页面

  • Show Source

© 版权所有 2014-2024, The imbalanced-learn 开发者.

使用 Sphinx 8.1.3 创建。

使用 PyData Sphinx Theme 0.16.1 构建。