函数采样器#
- 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
是否绕过
X
和y
的验证。关闭验证允许使用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_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_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
。