我一直在尝试使用稀疏矩阵进行关联规则挖掘,但它一直在抛出AttributeError: dtypes not found
. 我知道错误的含义,但我无法解决它。这就是我所拥有的:
import pandas as pd
import scipy
from mlxtend.frequent_patterns import fpgrowth
from mlxtend.frequent_patterns import apriori, association_rules
test_data = pd.read_csv('.../test_data.csv')
test_data = pd.DataFrame(test_data, dtype = bool)
sparse_df = scipy.sparse.csr_matrix(test_data.values)
sparse_df
结果:
<1000x10 sparse matrix of type '<class 'numpy.bool_'>'
with 1257 stored elements in Compressed Sparse Row format>
直到这里一切都很好并且工作正常,但是当我运行下一个块时,它会弹出错误。
apriori(sparse_df, min_support = 0.3, use_colnames = True, verbose = 1)
我已经在其他问题中检查了该问题的其他“解决方案”,但没有一个对我有用。
整个错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-259-ac3b4693270b> in <module>
----> 1 apriori(sparse_df, min_support = 0.3, use_colnames = True, verbose = 1)
~\anaconda3\lib\site-packages\mlxtend\frequent_patterns\apriori.py in apriori(df, min_support, use_colnames, max_len, verbose, low_memory)
236 'Got %s.' % min_support)
237
--> 238 fpc.valid_input_check(df)
239
240 if hasattr(df, "sparse"):
~\anaconda3\lib\site-packages\mlxtend\frequent_patterns\fpcommon.py in valid_input_check(df)
98
99 # Fast path: if all columns are boolean, there is nothing to checks
--> 100 all_bools = df.dtypes.apply(pd.api.types.is_bool_dtype).all()
101 if not all_bools:
102 # Pandas is much slower than numpy, so use np.where on Numpy arrays
~\anaconda3\lib\site-packages\scipy\sparse\base.py in __getattr__(self, attr)
685 return self.getnnz()
686 else:
--> 687 raise AttributeError(attr + " not found")
688
689 def transpose(self, axes=None, copy=False):
AttributeError: dtypes not found