MultiLabelBinarizer#

class sklearn.preprocessing.MultiLabelBinarizer(*, classes=None, sparse_output=False)[source]#

在可迭代对象和多标签格式之间进行转换。

尽管列表(集合或元组)对于多标签数据来说是一种非常直观的格式,但处理起来却很笨拙。此转换器将此直观格式转换为支持的多标签格式:一个(样本 x 类别)二进制矩阵,指示是否存在类别标签。

参数:
classesarray-like of shape (n_classes,), default=None

指示类别标签的顺序。所有条目都应是唯一的(不能包含重复的类别)。

sparse_outputbool, default=False

如果希望输出二进制数组采用 CSR 稀疏格式,则设置为 True。

属性:
classes_ndarray of shape (n_classes,)

如果提供,则为 classes 参数的副本。否则,它对应于拟合时找到的已排序类别集合。

另请参阅

OneHotEncoder

使用 one-hot 或 one-of-K 方案对分类特征进行编码。

示例

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> mlb.fit_transform([(1, 2), (3,)])
array([[1, 1, 0],
       [0, 0, 1]])
>>> mlb.classes_
array([1, 2, 3])
>>> mlb.fit_transform([{'sci-fi', 'thriller'}, {'comedy'}])
array([[0, 1, 1],
       [1, 0, 0]])
>>> list(mlb.classes_)
['comedy', 'sci-fi', 'thriller']

一个常见的错误是传入一个列表,这会导致以下问题

>>> mlb = MultiLabelBinarizer()
>>> mlb.fit(['sci-fi', 'thriller', 'comedy'])
MultiLabelBinarizer()
>>> mlb.classes_
array(['-', 'c', 'd', 'e', 'f', 'h', 'i', 'l', 'm', 'o', 'r', 's', 't',
    'y'], dtype=object)

要纠正此问题,应将标签列表传入为

>>> mlb = MultiLabelBinarizer()
>>> mlb.fit([['sci-fi', 'thriller', 'comedy']])
MultiLabelBinarizer()
>>> mlb.classes_
array(['comedy', 'sci-fi', 'thriller'], dtype=object)
fit(y)[source]#

拟合标签集二值化器,存储 classes_

参数:
yiterable of iterables

每个样本的标签集(任何可排序和可哈希的对象)。如果设置了 classes 参数,则不会对 y 进行迭代。

返回:
selfobject

拟合的估计器。

fit_transform(y)[source]#

拟合标签集二值化器并转换给定的标签集。

参数:
yiterable of iterables

每个样本的标签集(任何可排序和可哈希的对象)。如果设置了 classes 参数,则不会对 y 进行迭代。

返回:
y_indicator{ndarray, sparse matrix} of shape (n_samples, n_classes)

一个矩阵,其中 y_indicator[i, j] = 1 当且仅当 classes_[j]y[i] 中,否则为 0。稀疏矩阵将采用 CSR 格式。

get_metadata_routing()[source]#

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

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

返回:
routingMetadataRequest

一个包含路由信息的 MetadataRequest 对象。

get_params(deep=True)[source]#

获取此估计器的参数。

参数:
deepbool, default=True

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

返回:
paramsdict

参数名称映射到其值。

inverse_transform(yt)[source]#

将给定的指示矩阵转换回标签集。

参数:
yt{ndarray, sparse matrix} of shape (n_samples, n_classes)

一个只包含 1 和 0 的矩阵。

返回:
y_originallist of tuples

每个样本的标签集,其中 y[i] 包含每个 yt[i, j] == 1 对应的 classes_[j]

set_output(*, transform=None)[source]#

设置输出容器。

有关如何使用 API 的示例,请参阅 Introducing the set_output API

参数:
transform{“default”, “pandas”, “polars”}, default=None

配置 transformfit_transform 的输出。

  • "default": 转换器的默认输出格式

  • "pandas": DataFrame 输出

  • "polars": Polars 输出

  • None: 转换配置未更改

版本 1.4 新增: 添加了 "polars" 选项。

返回:
selfestimator instance

估计器实例。

set_params(**params)[source]#

设置此估计器的参数。

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

参数:
**paramsdict

估计器参数。

返回:
selfestimator instance

估计器实例。

transform(y)[source]#

转换给定的标签集。

参数:
yiterable of iterables

每个样本的标签集(任何可排序和可哈希的对象)。如果设置了 classes 参数,则不会对 y 进行迭代。

返回:
y_indicatorarray or CSR matrix, shape (n_samples, n_classes)

一个矩阵,其中 y_indicator[i, j] = 1 当且仅当 classes_[j]y[i] 中,否则为 0。