目标:在二进制值数据集上运行关联规则
d = {'col1': [0, 0,1], 'col2': [1, 0,0], 'col3': [0,1,1]}
df = pd.DataFrame(data=d)
这会为相应的列值生成一个带有 0 和 1 的数据框。
问题是当我使用如下代码时:
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
frequent_itemsets = apriori(pattern_dataset, min_support=0.50,use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
rules
通常这运行得很好,但是这次运行它时遇到了错误。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-46ec6f572255> in <module>()
4 frequent_itemsets = apriori(pattern_dataset, min_support=0.50,use_colnames=True)
5 frequent_itemsets
----> 6 rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
7 rules
D:\AnaConda\lib\site-packages\mlxtend\frequent_patterns\association_rules.py in association_rules(df, metric, min_threshold, support_only)
127 values = df['support'].values
128 frozenset_vect = np.vectorize(lambda x: frozenset(x))
--> 129 frequent_items_dict = dict(zip(frozenset_vect(keys), values))
130
131 # prepare buckets to collect frequent rules
D:\AnaConda\lib\site-packages\numpy\lib\function_base.py in __call__(self, *args, **kwargs)
1970 vargs.extend([kwargs[_n] for _n in names])
1971
-> 1972 return self._vectorize_call(func=func, args=vargs)
1973
1974 def _get_ufunc_and_otypes(self, func, args):
D:\AnaConda\lib\site-packages\numpy\lib\function_base.py in _vectorize_call(self, func, args)
2040 res = func()
2041 else:
-> 2042 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
2043
2044 # Convert args to object arrays first
D:\AnaConda\lib\site-packages\numpy\lib\function_base.py in _get_ufunc_and_otypes(self, func, args)
1996 args = [asarray(arg) for arg in args]
1997 if builtins.any(arg.size == 0 for arg in args):
-> 1998 raise ValueError('cannot call `vectorize` on size 0 inputs '
1999 'unless `otypes` is set')
2000
ValueError: cannot call `vectorize` on size 0 inputs unless `otypes` is set
这就是我在 Pandas 中的 dtypes,任何帮助将不胜感激。
col1 int64
col2 int64
col3 int64
dtype: object