Python apriori返回Generator而不是Dataframe



我正在编写代码,它占用数据集(购物篮(的一小部分,将其转换为热编码的数据帧,我想在它上运行mlxtend的apriori算法来获取频繁项目集。

然而,每当我运行apriori算法时,它似乎会立即运行,并返回生成器对象而不是数据帧。我遵循了文档中的说明,在他们的示例中,它显示apriori返回一个数据帧。我做错了什么?

这是我的代码:

import numpy as np
import pandas as pd
import csv
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
from mlxtend.preprocessing import TransactionEncoder
from apyori import apriori
def simpleRandomisedSample(filename, support_frac, sample_frac):
df1 = pd.read_csv("%s.csv" % filename, header=None) #Saving csv file into a dataframe in memory
size = len(df1)
support = support_frac * len(df1) #Sets the original support value to x% of the original dataset
sample_support = support * sample_frac #Support for our reduced sample as a fraction of the original support
sample = df1.sample(frac=sample_frac) #Saving x% (randomised) of the dataset as our sample
sample = sample.reset_index(drop = True) #Reseting indexes (which previously got randomised along with the data)
del df1 #Deleting original dataframe from memory to clear up space
sample_size = len(sample)
return size, support, sample_size, sample_support, sample
def main():
size, support, sample_size, sample_support, sample = simpleRandomisedSample("chess",0.01,0.1)
print("The original dataset had %d rows and a support of %.2f" % (size, support))
print("The dataset was reduced to %d rows and the sample has a support of %.2f" % (sample_size, sample_support)) 
sample_list = sample.values.tolist() #Converting Dataframe to list of lists for use with Apriori
te = TransactionEncoder()
te_ary = te.fit(sample_list).transform(sample_list) #Preprocessing our sample to work with Apriori algorithm
df = pd.DataFrame(te_ary, columns=te.columns_)
print(df)
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
print(frequent_itemsets)

if __name__ == "__main__":
main()

您的导入中存在名称冲突:

from mlxtend.frequent_patterns import apriori
[...]
from apyori import apriori

您的代码不是使用mlxtend算法,而是使用apyori提供的算法,导入的算法会覆盖上一个算法。

你可以删除你没有使用的一个,或者,如果你以后想同时访问这两个,你可以给一个不同的名称:

from mlxtend.frequent_patterns import apriori as mlx_apriori
from apyori import apriori as apy_apriori

相关内容

  • 没有找到相关文章

最新更新