Prefix-Span 在 pickle 文件中找不到任何模式

问题描述 投票:0回答:1

我在 Pickle 文件中有一个序列列表,并尝试使用 Prefix-Span 来数据挖掘常见模式。我没有收到错误,但也没有收到任何模式输出。

pickle 文件包含如下数据:

序列 1:[-1, 2, 3, 4, 0, ...] 序列 2:[0, 4, 1, 2, 0, ...] 序列 3:[-1, 2, 0, -1, 0, ...]

序列的最大长度约为 300,并且有 30 个不同的序列

我尝试了以下代码:

import time
from prefixspan import PrefixSpan
import pickle


def pattern_finder(list_of_sequences, max_pattern_length=5):
    start_time = time.time()

    print(
        f' pattern mining with PreFixSpan {len(list_of_sequences[0] *       len(list_of_sequences))} points...')

    with open('my_pickle_file.pkl', 'rb') as pickle_file:
        data = pickle.load(pickle_file)
    ps = PrefixSpan(data)

    ps.maxlen = max_pattern_length

    patterns = ps.frequent(2, closed=True)

    end_time = time.time()
    duration = end_time - start_time
    print(f" Completion Time: {'{:.2f}'.format(duration / 60)} minutes.")

    print(f'Patterns found :{patterns}')

    return patterns


if __name__ in "__main__":
    with open('my_pickle_file.pkl', 'rb') as file:
        list_of_sequences = pickle.load(file)
    pattern_finder(list_of_sequences)

模式输出为:找到模式:[]

我不知道为什么它没有找到任何模式。

python sequence pickle
1个回答
0
投票

您的代码似乎可能存在一些问题。让我们来看看它们:

  1. 数据格式:确保从pickle文件加载的数据格式正确。每个序列应该是一个项目列表,整个数据集应该是这样的序列的列表。

  2. PrefixSpan 配置:确保 PrefixSpan 的配置适合您的数据。设置

    minlen
    maxlen
    可能会影响找到的模式。

  3. 参数调整:您可能需要根据数据大小和预期模式频率调整支持阈值(在您的情况下为

    2
    )。

这是代码的修改版本,进行了一些调整:

import time
from prefixspan import PrefixSpan
import pickle


def pattern_finder(list_of_sequences, min_sup=2, max_pattern_length=5):
    start_time = time.time()

    print(f'Pattern mining with PrefixSpan for {len(list_of_sequences)} sequences...')

    ps = PrefixSpan(list_of_sequences)

    ps.maxlen = max_pattern_length

    patterns = ps.frequent(min_sup, closed=True)

    end_time = time.time()
    duration = end_time - start_time
    print(f"Completion Time: {'{:.2f}'.format(duration / 60)} minutes.")

    print(f'Patterns found: {patterns}')

    return patterns


if __name__ == "__main__":
    with open('my_pickle_file.pkl', 'rb') as file:
        list_of_sequences = pickle.load(file)
    pattern_finder(list_of_sequences)

确保

my_pickle_file.pkl
包含如上所述的正确数据格式。如果您仍然没有得到任何模式,请尝试调整
min_sup
参数或检查数据以更好地了解其特征。此外,请考虑使用较小的数据集进行测试,以更有效地调试问题。

© www.soinside.com 2019 - 2024. All rights reserved.