贝叶斯分类,来自《Clojure For Machine Learning》的示例

我目前正在学习贝叶斯分类的算法,当我试图按照书中的示例操作时,得到了一些奇怪的结果,这些结果与书中的示例不一致。

我认为我的代码没有问题(因为我基本上是手动抄写的),但我在REPL中仍然得到了不可能的结果,例如:

> (+ (evidence-of-sea-bass) (evidence-of-salmon)) ==> 2.8139728009700775

它应该返回1.000…,只有一些小的浮点精度误差。

这是代码:

(defn make-sea-bass []  #{:sea-bass    (if (< (rand) 0.2) :fat :thin)    (if (< (rand) 0.7) :long :short)    (if (< (rand) 0.8) :light :dark)})(defn make-salmon []  #{:salmon    (if (< (rand) 0.8) :fat :thin)    (if (< (rand) 0.5) :long :short)    (if (< (rand) 0.3) :light :dark)})(defn make-sample-fish []  (if (< (rand) 0.3) (make-sea-bass) (make-salmon)))(def fish-training-data  (for [i (range 10000)] (make-sample-fish)))(defn probability  [attribute & {:keys                [category prior-positive prior-negative data]                :or {category nil                     data fish-training-data}}]  (let [by-category (if category                    (filter category data)                    data)        positive (count (filter attribute by-category))        negative (- (count by-category) positive)        total (+ positive negative)]    (/ positive negative)))(defn evidence-of-salmon [& attrs]  (let [attr-prob (map #(probability % :category :salmon) attrs)        class-and-attr-prob (conj attr-prob (probability :salmon))]    (float (apply * class-and-attr-prob))))(defn evidence-of-sea-bass [& attrs]  (let [attr-prob (map #(probability % :category :sea-bass) attrs)        class-and-attr-prob (conj attr-prob (probability :sea-bass))]    (float (apply * class-and-attr-prob))))

回答:

如果你期望结果是1.0,那么你的probability函数的结果应该是(/ positive total)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注