1

下面是几行代码:

import pandas as pd
import numpy as np
from mlxtend.frequent_patterns import apriori 
from mlxtend.frequent_patterns import association_rules

def encode_c(x):
if x <= 0:
    return 0
if x >= 1:
    return 1
return 0


def get_product_consequents():
  all_transactions_df = get_dataframe()
  basket = (all_transactions_df.groupby(['a', 'b'])['c']
    .sum().unstack().reset_index().fillna(0)
    .set_index('a'))
  basket = basket.applymap(encode_c)
  frequent_itemsets = apriori(basket, min_support=0.07, use_colnames=True)
  rules = association_rules(frequent_itemsets, metric="lift", 
    min_threshold=1)
  rules['antecedants_length'] = rules['antecedents'].str.len()
  rules['consequents_length'] = rules['consequents'].str.len()
  rules = rules[(rules['lift'] >= 4) &  # 6
              (rules['confidence'] >= 0.4)]  # 0.8
  rules = rules[(rules['antecedants_length'] == 1) & 
    (rules['consequents_length'] == 1)]
  rules = (rules.groupby(['antecedants'])['consequents'])

图片 1

不推荐使用的错误图像

图 2

关键错误图像

当我这样做时: 先验地学习

rules["antecedant_len"] = rules["antecedents"].apply(lambda x: len(x))

我得到:
图 1

C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\_pydevd_bundle
\pydevd_resolver.py:71:
FutureWarning: Series.strides is deprecated
and will be removed in a future version
  return getattr(var, attribute)

我是从:mlxtend association_rules 和 apriori docs
做 的,正如错误所说:Series.strides已弃用。那么我怎样才能找到每个系列
的长度呢?frozenset即和上面一样吗?

主要错误

图 2

rules = (rules.groupby(['antecedants'])['consequents'])

在我评估以上行之后:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm         
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 376, in 
evaluate_expression compiled = compile(expression, '<string>', 'eval')
File "<string>", line 1
rules = (rules.groupby(['antecedants'])['consequents'])
      ^
SyntaxError: invalid syntax  

-----为什么语法错误,但在 JUPYTER NOTEBOOK 上工作正常?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1159, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, 
self.expression, self.doExec)
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 378, in 
evaluate_expression
Exec(expression, updated_globals, frame.f_locals)
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 1, in <module>
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\generic.py", line 6659, in groupby
observed=observed, **kwargs)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 2152, in groupby
return klass(obj, by, **kwds)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 599, in __init__
mutated=self.mutated)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 3291, in _get_grouper
raise KeyError(gpr)
KeyError: 'antecedants'

--- 最后它说关键错误,在处理syntax错误期间key发生错误,确保它不是关键错误,因为我可以在 中看到列sciView,也可以访问它,就像上面代码行中所做的那样。

其他信息:我也在使用Django

4

0 回答 0