File:Naive Bayes Classifier.gif
Summary
Description |
English: A "Naive Bayes Classifier" is a simple and computationally inexpensive way to estimate how likely it is that a certain combination of features can be attributed to a given class. As more measurements are fed to the classifier, the estimate is updated. |
Date | |
Source | https://twitter.com/j_bertolotti/status/1123268194758811648 |
Author | Jacopo Bertolotti |
Permission (Reusing this file) |
https://twitter.com/j_bertolotti/status/1030470604418428929 |
Mathematica 11.0 code
n = 20; red[z_] := {Opacity[z], Red}; purple[z_] := {Opacity[z], Purple}; cyan[z_] := {Opacity[z], Cyan} pop1 = RandomVariate[MultinormalDistribution[{3, 4}, {{1, 1}, {1, 2}}], n]; pop2 = RandomVariate[MultinormalDistribution[{6, 4}, {{0.5, 0}, {0, 1}}], n]; pop3 = RandomVariate[MultinormalDistribution[{6, 8}, {{2, 0}, {0, 0.5}}], n]; meas = RandomSample[Join[pop1, pop2, pop3]]; meascol = Table[Which[MemberQ[pop1, meas[[j]]], 1, MemberQ[pop2, meas[[j]]], 2, MemberQ[pop3, meas[[j]]], 3], {j, 1, Dimensions[meas][[1]]}] plots = Table[ p1 = Count[meascol[[1 ;; j]], 1]/j; \[Mu]x1 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 1]][[All, 1]]; \[Mu]y1 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 1]][[All, 2]]; \[Sigma]x1 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 1]][[All, 1]] - \[Mu]x1)^2]/(p1*j)]; \[Sigma]y1 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 1]][[All, 2]] - \[Mu]y1)^2]/(p1*j)]; p1x = If[\[Sigma]x1 == 0, 0, PDF[NormalDistribution[\[Mu]x1, \[Sigma]x1], x]]; p1y = If[\[Sigma]y1 == 0, 0, PDF[NormalDistribution[\[Mu]y1, \[Sigma]y1], y]]; p2 = Count[meascol[[1 ;; j]], 2]/j; \[Mu]x2 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 2]][[All, 1]]; \[Mu]y2 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 2]][[All, 2]]; \[Sigma]x2 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 2]][[All, 1]] - \[Mu]x2)^2]/(p2*j)]; \[Sigma]y2 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 2]][[All, 2]] - \[Mu]y2)^2]/(p2*j)]; p2x = If[\[Sigma]x2 == 0, 0, PDF[NormalDistribution[\[Mu]x2, \[Sigma]x2], x]]; p2y = If[\[Sigma]y2 == 0, 0, PDF[NormalDistribution[\[Mu]y2, \[Sigma]y2], y]]; p3 = Count[meascol[[1 ;; j]], 3]/j; \[Mu]x3 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 3]][[All, 1]]; \[Mu]y3 = Mean@Extract[meas, Position[meascol[[1 ;; j]], 3]][[All, 2]]; \[Sigma]x3 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 3]][[All, 1]] - \[Mu]x3)^2]/(p3*j)]; \[Sigma]y3 = Sqrt[Total[(Extract[meas, Position[meascol[[1 ;; j]], 3]][[All, 2]] - \[Mu]y3)^2]/(p3*j)]; p3x = If[\[Sigma]x3 == 0, 0, PDF[NormalDistribution[\[Mu]x3, \[Sigma]x3], x]]; p3y = If[\[Sigma]y3 == 0, 0, PDF[NormalDistribution[\[Mu]y3, \[Sigma]y3], y]]; Legended[Show[ ContourPlot[p1*p1x*p1y, {x, 0, 10}, {y, 0, 10}, ColorFunction -> red, PlotRange -> All, PlotPoints -> 50] , ContourPlot[p2*p2x*p2y, {x, 0, 10}, {y, 0, 10}, ColorFunction -> purple, PlotRange -> All, PlotPoints -> 50] , ContourPlot[p3*p3x*p3y, {x, 0, 10}, {y, 0, 10}, ColorFunction -> cyan, PlotRange -> All, PlotPoints -> 50] , Graphics[{PointSize[0.02], Black, Point[Join[Extract[meas, Position[meascol[[1 ;; j]], 1]], Extract[meas, Position[meascol[[1 ;; j]], 2]], Extract[meas, Position[meascol[[1 ;; j]], 3]]]], PointSize[0.015], Red, Point[Extract[meas, Position[meascol[[1 ;; j]], 1]]], Purple, Point[Extract[meas, Position[meascol[[1 ;; j]], 2]]], Cyan, Point[Extract[meas, Position[meascol[[1 ;; j]], 3]]]}] , PlotRange -> All, FrameLabel -> {"Feature 1", "Feature 2"}, LabelStyle -> {Bold, Black} ], SwatchLegend[{Red, Purple, Cyan}, {"Class Lucas", "Class B", "Class C"}] ] , {j, 1, n*3}];
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
![]() ![]() |
This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication. |
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
|