Clojure语言异常
我一直在尝试实现垃圾邮件分类器。我写了一个函数来获得一些概率,但是当我用两个参数调用这个函数时,我得到了clojure.lang.ArityException“传递给函数的参数数目错误”。以下是我的功能:Clojure语言异常,clojure,Clojure,我一直在尝试实现垃圾邮件分类器。我写了一个函数来获得一些概率,但是当我用两个参数调用这个函数时,我得到了clojure.lang.ArityException“传递给函数的参数数目错误”。以下是我的功能: (defn weightedprob [f cat] (let [weight 1 ap 0.5 basicprob (atom 0) totals (atom 0) bp (atom 0)] (swap! bas
(defn weightedprob
[f cat]
(let [weight 1
ap 0.5
basicprob (atom 0)
totals (atom 0)
bp (atom 0)]
(swap! basicprob #(fprob f cat))
(swap! totals #(reduce + (vals (get @fc f))))
(swap! bp #(/ (+ (* weight ap) (* totals basicprob)) (+ weight totals)))
bp))
这是电话:
(weightedprob "money" "good")
现在可以了。如果您对如何实现此功能有更好的了解,我将很高兴看到这一点。以下是有效的版本:
(defn weightedprob
[f cat]
(let [weight 1
ap 0.5
basicprob (atom 0)
totals (atom 0)
bp (atom 0)]
(reset! basicprob (fprob f cat))
(reset! totals (reduce + (vals (get @fc f))))
(reset! bp (/ (+ (* weight ap) (* @totals @basicprob)) (+ weight
@totals)))
@bp))
我在Python中实现的函数如下所示:
def weightedprob(self,f,cat,prf,weight=1.0,ap=0.5):
# Calculate current probability
basicprob=prf(f,cat)
# Count the number of times this feature has appeared in
# all categories
totals=sum([self.fcount(f,c) for c in self.categories()])
# Calculate the weighted average
bp=((weight*ap)+(totals*basicprob))/(weight+totals)
return bp
这是来自《集体情报》一书的第6章,文档过滤
没有原子的功能:
(defn weightedprob1
[f cat]
(let [weight 1
ap 0.5
basicprob (fprob f cat)
totals (reduce + (vals (get @fc f)))
bp (/ (+ (* weight ap) (* totals basicprob)) (+ weight totals))]
bp))
您原来的问题是由
交换引起的代码>应为接受单个参数的函数。您提供的函数没有接受任何参数,因此出现了错误
更正后的代码将是您已经发布的代码:
(defn weightedprob [f cat]
(let [weight 1
ap 0.5
basicprob (atom 0)
totals (atom 0)
bp (atom 0)]
(reset! basicprob (fprob f cat))
(reset! totals (reduce + (vals (get @fc f))))
(reset! bp (/ (+ (* weight ap) (* @totals @basicprob)) (+ weight @totals)))
@bp))
但这根本不需要原子。原子主要用于管理线程之间的数据。你根本不需要任何突变。只需继续使用让:
(defn weightedprob [f cat]
(let [weight 1
ap 0.5
basicprob (fprob f cat)
totals (reduce + (vals (get @fc f)))]
(/ (+ (* weight ap) (* totals basicprob)) (+ weight totals))))
现在还不清楚什么是fc
,但它可能也不需要是原子。在您给出的交换函数中代码>,您没有使用提供的参数。它期望您的#()
函数将接受一个参数(%
)。你的意思是说(重置!basicprob(fprob f cat))
?但看起来你不应该在这里使用原子。如果你发了一篇文章,我们可以建议如何写。不过,目前的形式还不清楚它应该做什么。像(*totals basicprob)
这样的事情看起来完全错了。至少您必须取消引用(例如,@totals
)才能获得值。这个代码有很难闻的气味;就像有人告诉你使用原子来变异数据,现在你在任何地方都使用它们——这段代码对我来说都是毫无意义的。如前所述,请在此提供更多关于全局的信息,如果这真的是您在这里采取的正确路线。如果没有原子,您将如何解决此函数?我只能直接用let表达式或原子来解决这个问题。是的,我做了类似的事情。你可以在上面查看。“fc变量将存储不同分类中不同功能的计数。例如:{'python':{'bad':0,'good':6},':{'bad':3,'good':3}。”。在本例中,“python”一词出现在六个好的文档中,而没有出现在坏的文档中。