函数采样器#

class imblearn.FunctionSampler(*, func=None, accept_sparse=True, kw_args=None, validate=True)[source]#

通过调用任意可调用对象来构建一个采样器。

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

Parameters:
funccallable, default=None

用于转换的可调用对象。这将传递与transform相同的参数,args和kwargs将被转发。如果func为None,则func将是恒等函数。

accept_sparsebool, default=True

是否支持稀疏输入。默认情况下,支持稀疏输入。

kw_argsdict, default=None

func 期望的关键字参数。

validatebool, default=True

是否绕过Xy的验证。关闭验证允许使用FunctionSampler处理任何类型的数据。

在版本0.6中添加。

Attributes:
sampling_strategy_dict

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

n_features_in_int

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

在版本0.9中添加。

feature_names_in_ndarray of shape (n_features_in_,)

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

在版本0.10中添加。

另请参阅

sklearn.preprocessing.FunctionTransfomer

无状态转换器。

注释

参见 自定义采样器以实现异常值拒绝估计器

示例

>>> import numpy as np
>>> from sklearn.datasets import make_classification
>>> from imblearn import FunctionSampler
>>> 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)

我们可以创建以仅选择前十个样本为例。

>>> def func(X, y):
...   return X[:10], y[:10]
>>> sampler = FunctionSampler(func=func)
>>> X_res, y_res = sampler.fit_resample(X, y)
>>> np.all(X_res == X[:10])
True
>>> np.all(y_res == y[:10])
True

我们也可以创建一个接受一些参数的特定函数。

>>> from collections import Counter
>>> from imblearn.under_sampling import RandomUnderSampler
>>> def func(X, y, sampling_strategy, random_state):
...   return RandomUnderSampler(
...       sampling_strategy=sampling_strategy,
...       random_state=random_state).fit_resample(X, y)
>>> sampler = FunctionSampler(func=func,
...                           kw_args={'sampling_strategy': 'auto',
...                                    'random_state': 0})
>>> X_res, y_res = sampler.fit_resample(X, y)
>>> print(f'Resampled dataset shape {sorted(Counter(y_res).items())}')
Resampled dataset shape [(0, 100), (1, 100)]

方法

fit(X, y)

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

fit_resample(X, y)

重新采样数据集。

get_feature_names_out([input_features])

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

get_metadata_routing()

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

get_params([deep])

获取此估计器的参数。

set_params(**params)

设置此估计器的参数。

fit(X, y)[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)[source]#

重新采样数据集。

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

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

yarray-like of shape (n_samples,)

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

Returns:
X_resampled{array-like, 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_featuresNone,则使用 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

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

set_params(**params)[source]#

设置此估计器的参数。

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

Parameters:
**paramsdict

估计器参数。

Returns:
selfestimator instance

估计器实例。

使用imblearn.FunctionSampler的示例#

自定义采样器以实现异常值拒绝估计器

Customized sampler to implement an outlier rejections estimator

在人脸识别任务中基准过采样方法

Benchmark over-sampling methods in a face recognition task

使用采样器的Bagging分类器

Bagging classifiers using sampler

比较过采样采样器

Compare over-sampling samplers

比较欠采样采样器

Compare under-sampling samplers