需求:把类别或字符串类型的特征转化为多热编码,特征是逗号、竖线等方式分割
import numpy as np
import pandas as pd
from scipy import sparse
class MultiHotEncoder:
"""
Encode categorical features as a multi-hot numeric array.
Parameters
----------
sep : string, default='|', the separation string.
Attributes
----------
categories_ : a dictionary of encoding results.
Examples
--------
>>> enc = MultiHotEncoder()
>>> X = ['red|green', 'green', 'red|yellow']
>>> enc.fit(X)
>>> enc.categories_
{'red': 0, 'green': 1, 'yellow': 2}
>>> enc.transform(['green', 'yellow|red', None, 'red|green|yellow'])
array([[0., 1., 0.],
[1., 0., 1.],
[0., 0., 0.],
[1., 1., 1.]])
"""
def __init__(self, sep='|')

本文介绍如何将类别或字符串类型的特征,如由逗号或竖线分隔的数据,转换为多热编码。这种转换在机器学习中常见,用于将非数值特征转化为可以输入模型的形式。
最低0.47元/天 解锁文章
1858

被折叠的 条评论
为什么被折叠?



