我正在上一门机器学习课程,希望第一次上自己的课。当我的第一个功能正常工作时,我感到非常高兴,但是在第二个功能中,我想使用我制作的第一个功能。
我开始收到一条错误消息,指出“未定义自我”,我在Google上搜索后发现需要将self作为参数,然后从新函数中调用已经切除的函数。现在,我得到错误TypeError:confidence()缺少1个必需的位置参数:'consequent'。
class apriori:
# This are different apriori algorithms.
def __init__(self, data):
self.data = data
# This calculates support, col is a list of column names to calculate.
def support(self, col, cross="Both"):
support_result = {}
if cross=="Both" or cross=="No":
for item in range(len(col)):
# Initate count of the item.
count = 0
for row in range(len(self.data)):
# Choose the row, then the column we need.
if self.data.iloc[row].loc[col[item]]==1:
# If the column has 1, then itterate count.
count += 1
# Add the result to the appendix.
support_result[col[item]] = count/len(self.data)
# If cross==True, calculate cross support.
if cross=="Both" or cross=="Only":
count = 0
for row in range(len(self.data)):
# If both columns are 1, then itterate count.
if self.data.iloc[row].loc[col[0]]==self.data.iloc[row].loc[col[1]]:
count += 1
support_result[col[0] + ", " + col[1]] = count/len(self.data)
return support_result
def confidence(self, antecedent, consequent):
ant_result = self.support(antecedent)
union_result = self.support(list(antecedent + consequent))
return union_result/ant_result
我有包含两列的数据集,分别是苹果和橙子,其中每一行取1或0。我运行:
apriori = apriori(df)
apple = ["apple"]
orange = ["orange"]
apriori.confidence(apple, orange)
TypeError: confidence() missing 1 required positional argument: 'consequent'
似乎以苹果为自变量,我以为它会“跳过”自我,并以苹果为前因而以橙色为后因? self.support是否应该和写apriori.support()一样?