TransactionEncoder: 将项目列表转换为交易数据用于频繁项集挖掘

用于 Python 列表形式交易数据的编码器类

from mlxtend.preprocessing import TransactionEncoder

概述

将 Python 列表的列表形式的数据库交易数据编码为 NumPy 数组。

示例 1

假设我们有以下交易数据

from mlxtend.preprocessing import TransactionEncoder

dataset = [['Apple', 'Beer', 'Rice', 'Chicken'],
           ['Apple', 'Beer', 'Rice'],
           ['Apple', 'Beer'],
           ['Apple', 'Bananas'],
           ['Milk', 'Beer', 'Rice', 'Chicken'],
           ['Milk', 'Beer', 'Rice'],
           ['Milk', 'Beer'],
           ['Apple', 'Bananas']]

使用 TransactionEncoder 对象,我们可以将此数据集转换为适合典型机器学习 API 的数组格式。通过 fit 方法,TransactionEncoder 学习数据集中的唯一标签;通过 transform 方法,它将输入数据集(一个 Python 列表的列表)转换为独热编码的 NumPy 布尔数组

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
te_ary
array([[ True, False,  True,  True, False,  True],
       [ True, False,  True, False, False,  True],
       [ True, False,  True, False, False, False],
       [ True,  True, False, False, False, False],
       [False, False,  True,  True,  True,  True],
       [False, False,  True, False,  True,  True],
       [False, False,  True, False,  True, False],
       [ True,  True, False, False, False, False]], dtype=bool)

为提高处理大型数据集时的内存效率,NumPy 数组是布尔型的。如果需要经典的整数表示,我们可以将数组转换为适当的类型

te_ary.astype("int")
array([[1, 0, 1, 1, 0, 1],
       [1, 0, 1, 0, 0, 1],
       [1, 0, 1, 0, 0, 0],
       [1, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1],
       [0, 0, 1, 0, 1, 1],
       [0, 0, 1, 0, 1, 0],
       [1, 1, 0, 0, 0, 0]])

拟合后,可以通过 columns_ 属性访问与上面所示数据数组对应的唯一列名

te.columns_
['Apple', 'Bananas', 'Beer', 'Chicken', 'Milk', 'Rice']

为了方便起见,我们可以将编码后的数组转换为 pandas DataFrame

import pandas as pd

pd.DataFrame(te_ary, columns=te.columns_)
Apple Bananas Beer Chicken Milk Rice
0 True False True True False True
1 True False True False False True
2 True False True False False False
3 True True False False False False
4 False False True True True True
5 False False True False True True
6 False False True False True False
7 True True False False False False

如果需要,我们可以通过 inverse_transform 函数将独热编码的数组转回交易列表的列表形式

first4 = te_ary[:4]
te.inverse_transform(first4)
[['Apple', 'Beer', 'Chicken', 'Rice'],
 ['Apple', 'Beer', 'Rice'],
 ['Apple', 'Beer'],
 ['Apple', 'Bananas']]

API

TransactionEncoder()

用于 Python 列表形式交易数据的编码器类

参数

属性

columns_: list 输入列表的列表 X 中的唯一名称列表

示例

有关用法示例,请参阅 https://mlxtend.cn/mlxtend/user_guide/preprocessing/TransactionEncoder/

方法


fit(X)

从交易 DataFrame 中学习唯一的列名

参数

  • X : 列表的列表

    一个 python 列表的列表,其中外部列表存储 n 个交易,内部列表存储每个交易中的项目。

    例如,[['Apple', 'Beer', 'Rice', 'Chicken'], ['Apple', 'Beer', 'Rice'], ['Apple', 'Beer'], ['Apple', 'Bananas'], ['Milk', 'Beer', 'Rice', 'Chicken'], ['Milk', 'Beer', 'Rice'], ['Milk', 'Beer'], ['Apple', 'Bananas']]


fit_transform(X, sparse=False)

拟合 TransactionEncoder 编码器并转换数据集。


get_params(deep=True)

获取此评估器的参数。

参数

  • deep : 布尔值,可选

    如果为 True,将返回此评估器及其包含的子对象(也是评估器)的参数。

返回

  • params : 字符串到任意类型的映射

    参数名称映射到其值。


inverse_transform(array)

将编码后的 NumPy 数组转回交易数据。

参数

  • array : NumPy 数组 [n_transactions, n_unique_items]

    输入交易数据的 NumPy 独热编码布尔数组,其中列按字母顺序表示在输入数组中找到的唯一项目

    例如,

    array([[True , False, True , True , False, True ],
    [True , False, True , False, False, True ],
    [True , False, True , False, False, False],
    [True , True , False, False, False, False],
    [False, False, True , True , True , True ],
    [False, False, True , False, True , True ],
    [False, False, True , False, True , False],
    [True , True , False, False, False, False]])
The corresponding column labels are available as self.columns_,
e.g., ['Apple', 'Bananas', 'Beer', 'Chicken', 'Milk', 'Rice']

返回

  • X : 列表的列表

    一个 python 列表的列表,其中外部列表存储 n 个交易,内部列表存储每个交易中的项目。

    例如,

    [['Apple', 'Beer', 'Rice', 'Chicken'],
    ['Apple', 'Beer', 'Rice'],
    ['Apple', 'Beer'],
    ['Apple', 'Bananas'],
    ['Milk', 'Beer', 'Rice', 'Chicken'],
    ['Milk', 'Beer', 'Rice'],
    ['Milk', 'Beer'],
    ['Apple', 'Bananas']]

set_params(params)

设置此评估器的参数。

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

返回

self


transform(X, sparse=False)

将交易转换为独热编码的 NumPy 数组。

参数

  • X : 列表的列表

    一个 python 列表的列表,其中外部列表存储 n 个交易,内部列表存储每个交易中的项目。

    例如,[['Apple', 'Beer', 'Rice', 'Chicken'], ['Apple', 'Beer', 'Rice'], ['Apple', 'Beer'], ['Apple', 'Bananas'], ['Milk', 'Beer', 'Rice', 'Chicken'], ['Milk', 'Beer', 'Rice'], ['Milk', 'Beer'], ['Apple', 'Bananas']]

    sparse: bool (default=False) 如果为 True,transform 将返回压缩稀疏行矩阵而不是常规矩阵。

返回

  • array : NumPy 数组 [n_transactions, n_unique_items]

    如果 sparse=False(默认)。否则为压缩稀疏行矩阵。输入交易数据的独热编码布尔数组,其中列按字母顺序表示在输入数组中找到的唯一项目。具体表示取决于 sparse 参数

    例如,array([[True , False, True , True , False, True ], [True , False, True , False, False, True ], [True , False, True , False, False, False], [True , True , False, False, False, False], [False, False, True , True , True , True ], [False, False, True , False, True , True ], [False, False, True , False, True , False], [True , True , False, False, False, False]]) 相应的列标签可通过 self.columns_ 获取,例如 ['Apple', 'Bananas', 'Beer', 'Chicken', 'Milk', 'Rice']

ython