{"id":2553,"date":"2023-03-22T07:05:45","date_gmt":"2023-03-22T07:05:45","guid":{"rendered":"https:\/\/cafe2sach.com\/?p=2553"},"modified":"2023-03-22T07:43:24","modified_gmt":"2023-03-22T07:43:24","slug":"thuat-toan-phan-lop-naive-bayes-su-dung-c","status":"publish","type":"post","link":"https:\/\/cafe2sach.com\/index.php\/2023\/03\/22\/thuat-toan-phan-lop-naive-bayes-su-dung-c\/","title":{"rendered":"Thu\u1eadt to\u00e1n ph\u00e2n l\u1edbp Naive bayes s\u1eed d\u1ee5ng C#"},"content":{"rendered":"\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">N\u1ed9i dung ch&iacute;nh<\/p><ul class=\"toc_list\"><li><a href=\"#Thuat_toan_Naive_Bayes\"><span class=\"toc_number toc_depth_1\">1<\/span>  Thu\u1eadt to\u00e1n Naive Bayes <\/a><ul><li><a href=\"#11_Dinh_ly_Bayes\"><span class=\"toc_number toc_depth_2\">1.1<\/span>  1.1 \u0110\u1ecbnh l\u00fd Bayes <\/a><\/li><li><a href=\"#12_Giai_thuat_phan_lop_Naive_bayes\"><span class=\"toc_number toc_depth_2\">1.2<\/span>  1.2 Gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive bayes <\/a><\/li><\/ul><\/li><li><a href=\"#Lam_min_Laplace\"><span class=\"toc_number toc_depth_1\">2<\/span>  L\u00e0m m\u1ecbn Laplace <\/a><\/li><li><a href=\"#Xay_dung_thuat_toan_Phan_lop_Naive_Bayes_bang_C\"><span class=\"toc_number toc_depth_1\">3<\/span>  X\u00e2y d\u1ef1ng thu\u1eadt to\u00e1n Ph\u00e2n l\u1edbp Naive Bayes b\u1eb1ng C# <\/a><ul><li><a href=\"#3_Mot_vi_du_cua_viec_su_dung_giai_thuat_phan_lop_Naive_Bayes\"><span class=\"toc_number toc_depth_2\">3.1<\/span>  3. M\u1ed9t v\u00ed d\u1ee5 c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive Bayes  <\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Thuat_toan_Naive_Bayes\"> Thu\u1eadt to\u00e1n Naive Bayes <\/span><\/h1> \nBayesian l\u00e0 m\u1ed9t gi\u1ea3i thu\u1eadt d\u1ef1a tr\u00ean ph\u00e9p to\u00e1n x\u00e1c xu\u1ea5t. Ch\u00fang c\u00f3 th\u1ec3 d\u1ef1 \u0111o\u00e1n t\u1ef7 l\u1ec7 thu\u1ed9c l\u1edbp n\u00e0o \u0111\u00f3, gi\u1ed1ng nh\u01b0 t\u1ef7 l\u1ec7 m\u00e0 c\u00e1i m\u00e0 m\u1eabu thu\u1ed9c v\u1ec1 trong l\u1edbp. \nGi\u1ea3i thu\u1eadt Bayesian d\u1ef1a tr\u00ean l\u00fd thuy\u1ebft Bayes. Naive Bayes d\u01b0\u1ee3c xem nh\u01b0 l\u00e0 \u1ea3nh h\u01b0\u1edfng c\u1ee7a gi\u00e1 tr\u1ecb thu\u1ed9c t\u00ednh tr\u00ean l\u1edbp \u0111\u01b0a v\u00e0o l\u00e0 kh\u00f4ng ph\u1ee5 thu\u1ed9c v\u00e0 c\u00e1c gi\u00e1 tr\u1ecb thu\u1ed9c t\u00ednh kh\u00e1c. Gi\u1ea3 \u0111\u1ecbnh n\u00e0y \u0111\u01b0\u1ee3c g\u1ecb l\u00e0 l\u1edbp \u0111i\u1ec1u ki\u1ec7n kh\u00f4ng ph\u1ee5 thu\u1ed9c. N\u00f3 \u0111\u01b0\u1ee3c l\u00e0m \u0111\u01a1n gi\u1ea3n h\u00f3a v\u00e0o t\u00ednh to\u00e1n v\u00e0 \u0111\u01b0\u1ee3c g\u1ecdi nh\u01b0 l\u00e0 &#8220;naive&#8221; &#8211; ng\u00e2y th\u01a1. \n<br>\n<h2><span id=\"11_Dinh_ly_Bayes\"> 1.1 \u0110\u1ecbnh l\u00fd Bayes <\/span><\/h2> \n<br>\nH\u00e3y coi X ={x1, x2,&#8230;,xn} l\u00e0 m\u1ed9t m\u1eabu, c\u00e1c th\u00e0nh ph\u1ea7n n\u00e0y bi\u1ec3u di\u1ec5n gi\u00e1 tr\u1ecb tr\u00ean t\u1eadp n thu\u1ed9c t\u00ednh. Trong \u0111\u1ecbnh l\u00fd Bayes, X \u0111\u01b0\u1ee3c xem x\u00e9t l\u00e0 m\u1ed9t minh ch\u1ee9ng. H\u00e3y H l\u00e0 m\u1ed9t v\u00e0i gi\u1ea3 thuy\u1ebft, nh\u01b0 l\u00e0 d\u1eef li\u1ec7u X thu\u1ed9c v\u00e0o m\u1ed9t l\u1edbp \u0111\u1eb7c bi\u1ec7t C. Cho c\u00e1c v\u1ea5n \u0111\u1ec1 ph\u00e2n l\u1edbp, m\u1ee5c ti\u00eau c\u1ee7a ch\u00fang ta l\u00e0 quy\u1ebft \u0111inh P(H|X) c\u00e1i m\u00e0 l\u00e0 x\u00e1c xu\u1ea5t c\u00e1i m\u00e0 gi\u1ea3 thuy\u1ebft H n\u0103m \u0111\u01b0\u1ee3c c\u00e1c ch\u1ee9ng c\u1ee9. N\u00f3i m\u1ed9t c\u00e1ch kh\u00e1c, ch\u00fang ta t\u00ecm x\u00e1c xuy\u1ebft c\u00e1i m\u00e0 m\u1eabu X thu\u1ed9c v\u00e0o l\u1edbp C, c\u00e1i m\u00e0 \u0111\u01b0\u1ee3c \u0111\u01b0a ra cho ch\u00fang ta bi\u1ebft v\u1ec1 m\u00f4 t\u1ea3 thu\u1ed9c t\u00ednh c\u1ee7a X. P(H|X) ch\u00ednh l\u00e0 x\u00e1c xu\u1ea5t c\u1ee7a H m\u00e0 \u0111i\u1ec1u ki\u1ec7n l\u00e0 X. \nD\u1ef1a tr\u00ean \u0111\u1ecbnh l\u00fd Bayes, x\u00e1c xu\u1ea5t c\u00e1i m\u00e0 ch\u00fang ta mu\u1ed1n t\u00ecnh P(H|X) c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c bi\u1ec3u di\u1ec5n g\u1ed3m c\u00e1c x\u00e1c xu\u1ea5t sau: \nP(X|H) v\u00e0 P(X) v\u00e0 dduwowcj t\u00ednh nh\u01b0 sau : P(H|X) = P(X|H).P(H)\/P(X)\n<br>\n<h2><span id=\"12_Giai_thuat_phan_lop_Naive_bayes\"> 1.2 Gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive bayes <\/span><\/h2>\n<br>\nGi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Bayes \u0111\u01b0\u1ee3c l\u00e0m vi\u1ec7c theo lu\u1ed3ng sau:\n<br>\n\na. H\u00e3y cho t\u1eadp m\u1eabu training l\u00e0 T, m\u1ed7i l\u1edbp s\u1ebd c\u00f3 m\u1ed9t label, ch\u00fang ta c\u00f3 k l\u1edbp. M\u1ed7i m\u1eabu s\u1ebd \u0111\u01b0\u1ee3c bi\u1ec3u di\u1ec5n b\u1edfi m\u1ed9t vector n chi\u1ec1u, X={x1,x2,&#8230;,xn}, d\u1ef1 \u0111o\u00e1n n t\u00ednh c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a n thu\u1ed9c t\u00ednh, A1, A2, &#8230;.An t\u01b0\u01a1ng \u1ee9ng. \n<br>\n<p algin-text=\"center\"> \nb. Gi\u1ea3 s\u1eed c\u00f3 m l\u1edbp C1, C2,..,Cm. Cho m\u1ed9t ph\u1ea7n t\u1eed d\u1eef li\u1ec7u X, b\u1ed9 ph\u00e2n l\u1edbp s\u1ebd g\u00e1n nh\u00e3n cho X l\u00e0 l\u1edbp c\u00f3 x\u00e1c su\u1ea5t h\u1eadu nghi\u1ec7m l\u1edbn nh\u1ea5t. C\u1ee5 th\u1ec3, b\u1ed9 ph\u00e2n l\u1edbp Bayes s\u1ebd d\u1ef1 \u0111o\u00e1n X thu\u1ed9c v\u00e0o l\u1edbp Ci n\u1ebfu v\u00e0 ch\u1ec9 n\u1ebfu:\nP(Ci|X) &gt; P(Cj|X) (1&lt;= i, j &lt;=m, i != j)\nGi\u00e1 tr\u1ecb n\u00e0y s\u1ebd t\u00ednh d\u1ef1a tr\u00ean \u0111\u1ecbnh l\u00fd Bayes.\n<br>\nc.\u0110\u1ec3 t\u00ecm x\u00e1c su\u1ea5t l\u1edbn nh\u1ea5t, ta nh\u1eadn th\u1ea5y c\u00e1c gi\u00e1 tr\u1ecb P(X) l\u00e0 gi\u1ed1ng nhau v\u1edbi m\u1ecdi l\u1edbp n\u00ean kh\u00f4ng c\u1ea7n t\u00ednh. Do \u0111\u00f3 ta ch\u1ec9 c\u1ea7n t\u00ecm gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u1ee7a P(X|Ci) * P(Ci). Ch\u00fa \u00fd r\u1eb1ng P(Ci) \u0111\u01b0\u1ee3c \u01b0\u1edbc l\u01b0\u1ee3ng b\u1eb1ng |Di|\/|D|, trong \u0111\u00f3 Di l\u00e0 t\u1eadp c\u00e1c ph\u1ea7n t\u1eed d\u1eef li\u1ec7u thu\u1ed9c l\u1edbp Ci. N\u1ebfu x\u00e1c su\u1ea5t ti\u1ec1n nghi\u1ec7m P(Ci) c\u0169ng kh\u00f4ng x\u00e1c \u0111\u1ecbnh \u0111\u01b0\u1ee3c th\u00ec ta coi ch\u00fang b\u1eb1ng nhau P(C1) = P(C2) = &#8230; = P(Cm), khi \u0111\u00f3 ta ch\u1ec9 c\u1ea7n t\u00ecm gi\u00e1 tr\u1ecb P(X|Ci) l\u1edbn nh\u1ea5t.\nd. Khi s\u1ed1 l\u01b0\u1ee3ng c\u00e1c thu\u1ed9c t\u00ednh m\u00f4 t\u1ea3 d\u1eef li\u1ec7u l\u00e0 l\u1edbn th\u00ec chi ph\u00ed t\u00ednh to\u00e0n P(X|Ci) l\u00e0 r\u1ea5t l\u1edbn, d\u00f3 \u0111\u00f3 c\u00f3 th\u1ec3 gi\u1ea3m \u0111\u1ed9 ph\u1ee9c t\u1ea1p c\u1ee7a thu\u1eadt to\u00e1n Naive Bayes gi\u1ea3 thi\u1ebft c\u00e1c thu\u1ed9c t\u00ednh \u0111\u1ed9c l\u1eadp nhau. Khi \u0111\u00f3 ta c\u00f3 th\u1ec3 t\u00ednh:\nP(X|Ci) = P(x1|Ci)&#8230;P(xn|Ci)\n<br>\n<h1><span id=\"Lam_min_Laplace\"> L\u00e0m m\u1ecbn Laplace <\/span><\/h1>\nL\u00e0m m\u1ecbn Laplace l\u00e0 m\u1ed9t k\u1ef9 thu\u1eadt l\u00e0m m\u1ecbn gi\u00fap gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 x\u00e1c su\u1ea5t b\u1eb1ng kh\u00f4ng trong thu\u1eadt to\u00e1n h\u1ecdc m\u00e1y Na\u00efve Bayes. S\u1eed d\u1ee5ng c\u00e1c gi\u00e1 tr\u1ecb alpha cao h\u01a1n s\u1ebd \u0111\u1ea9y kh\u1ea3 n\u0103ng h\u01b0\u1edbng t\u1edbi gi\u00e1 tr\u1ecb 0,5, t\u1ee9c l\u00e0 x\u00e1c su\u1ea5t c\u1ee7a m\u1ed9t t\u1eeb b\u1eb1ng 0,5 cho c\u1ea3 \u0111\u00e1nh gi\u00e1 t\u00edch c\u1ef1c v\u00e0 ti\u00eau c\u1ef1c. V\u00ec ch\u00fang ta kh\u00f4ng nh\u1eadn \u0111\u01b0\u1ee3c nhi\u1ec1u th\u00f4ng tin t\u1eeb \u0111\u00f3, n\u00ean kh\u00f4ng th\u00edch h\u1ee3p. Do \u0111\u00f3, ch\u00fang ta \u01b0u ti\u00ean s\u1eed d\u1ee5ng alpha = 1.\n\n\n<h1><span id=\"Xay_dung_thuat_toan_Phan_lop_Naive_Bayes_bang_C\"> X\u00e2y d\u1ef1ng thu\u1eadt to\u00e1n Ph\u00e2n l\u1edbp Naive Bayes b\u1eb1ng C# <\/span><\/h1> \n\n<h2><span id=\"3_Mot_vi_du_cua_viec_su_dung_giai_thuat_phan_lop_Naive_Bayes\"> 3. M\u1ed9t v\u00ed d\u1ee5 c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive Bayes  <\/span><\/h2>\nCh\u00fang ta c\u00f3 t\u1eadp d\u1eef li\u1ec7u h\u1ecdc t\u1eadp sau \u0111\u00e2y:\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>TT<\/td><td>Tu\u1ed5i<\/td><td>Thu nh\u1eadp <\/td><td>Sinh vi\u00ean<\/td><td>T\u00edn d\u1ee5ng<\/td><td>Ci: Mua<\/td><\/tr><tr><td>1<\/td><td>Tr\u1ebb<\/td><td>Cao<\/td><td>Kh\u00f4ng<\/td><td>Cao<\/td><td>kh\u00f4ng<\/td><\/tr><tr><td>2<\/td><td>Tr\u1ebb<\/td><td>Cao<\/td><td>Kh\u00f4ng<\/td><td>r\u1ea5t cao<\/td><td>kh\u00f4ng<\/td><\/tr><tr><td>3<\/td><td>Trung ni\u00ean<\/td><td>Cao<\/td><td>Kh\u00f4ng<\/td><td>cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>4<\/td><td>Cao ni\u00ean<\/td><td>Trung b\u00ecnh<\/td><td>Kh\u00f4ng<\/td><td>cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>5<\/td><td>Cao ni\u00ean<\/td><td>Th\u1ea5p<\/td><td>C\u00f3<\/td><td>cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>6<\/td><td>Trung ni\u00ean<\/td><td>Th\u1ea5p<\/td><td>C\u00f3<\/td><td>R\u1ea5t cao<\/td><td>kh\u00f4ng<\/td><\/tr><tr><td>7<\/td><td>Tr\u1ebb<\/td><td>Th\u1ea5p<\/td><td>C\u00f3<\/td><td>R\u1ea5t cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>8<\/td><td>Tr\u1ebb<\/td><td>Trung b\u00ecnh<\/td><td>Kh\u00f4ng<\/td><td>Cao<\/td><td>kh\u00f4ng<\/td><\/tr><tr><td>9<\/td><td>Tr\u1ebb<\/td><td>Th\u1ea5p<\/td><td>C\u00f3 <\/td><td>Cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>10<\/td><td>Cao ni\u00ean<\/td><td>Trung b\u00ecnh<\/td><td>C\u00f3 <\/td><td>Cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>11<\/td><td>Tr\u1ebb<\/td><td>Trung b\u00ecnh<\/td><td>C\u00f3<\/td><td>R\u1ea5t cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>12<\/td><td>Trung ni\u00ean<\/td><td>Trung b\u00ecnh<\/td><td>Kh\u00f4ng<\/td><td>R\u1ea5t cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>13<\/td><td>Trung ni\u00ean<\/td><td>Cao<\/td><td>C\u00f3<\/td><td>Cao<\/td><td>C\u00f3<\/td><\/tr><tr><td>14<\/td><td>Cao ni\u00ean<\/td><td>Trung b\u00ecnh<\/td><td>Kh\u00f4ng<\/td><td>R\u1ea5t cao<\/td><td>Kh\u00f4ng<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>D\u1eef li\u1ec7u tr\u00ean \u0111\u01b0\u1ee3c m\u00f4 t\u1ea3 b\u1edfi c\u00e1c thu\u1ed9c t\u00ednh: tu\u1ed5i, thu nh\u1eadp, l\u00e0 sinh vi\u00ean hay kh\u00f4ng v\u00e0 t\u00e0i kho\u1ea3n t\u00edn d\u1ee5ng. C\u00f3 2 thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c g\u00e1n nh\u00e3n cho vi\u1ec7c ng\u01b0\u1eddi \u0111\u00f3 c\u00f3 mua m\u00e1y t\u00ednh hay kh\u00f4ng \u0111\u00f3 l\u00e0 c\u00f3 (class C1), kh\u00f4ng (class C2)<\/p>\n\n\n\n<p>Gi\u1ea3 s\u1eed m\u1eabu d\u1eef li\u1ec7u m\u00e0 ch\u00fang ta c\u1ea7n d\u1ef1 \u0111o\u00e1n l\u00e0: <\/p>\n\n\n\n<p>X = (tu\u1ed5i = tr\u1ebb, thu nh\u1eadp=trung b\u00ecnh, sinh vi\u00ean = c\u00f3, t\u00e0i kho\u1ea3n t\u00edn d\u1ee5ng = cao) <\/p>\n\n\n\n<p>Ch\u00fang ta c\u1ea7n t\u1ed1i \u01b0u ph\u00e9p to\u00e1n P(X|Ci) P(Ci) cho i = 1,2. P(Ci), l\u00e0 x\u00e1c xu\u1ea5t c\u1ee7a m\u1ed7i l\u1edbp. c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c d\u1ef1 \u0111o\u00e1n d\u1ef1a tr\u00ean c\u00e1c m\u1eabu \u0111\u00e3 h\u1ecdc: <\/p>\n\n\n\n<p>P(mua=c\u00f3) = 9\/14<\/p>\n\n\n\n<p>P(mua=kh\u00f4ng) = 5\/14<\/p>\n\n\n\n<p>\u0110\u1ec3 t\u00ednh to\u00e1n P(X|Ci), cho i =1,2, chngs ta t\u00ednh to\u00e1n t\u1ec9 l\u1ec7 x\u00e1c xu\u1ea5t c\u00f3 \u0111i\u1ec1u ki\u1ec7n nh\u01b0 sau: <\/p>\n\n\n\n<p>P(tu\u1ed5i=tr\u1ebb|mua=c\u00f3) = 2\/9<\/p>\n\n\n\n<p>P(tu\u1ed5i=tr\u1ebb|mua=kh\u00f4ng) =3\/5<\/p>\n\n\n\n<p>P=(thu nh\u1eadp=trung b\u00ecnh|mua=c\u00f3)=4\/9<\/p>\n\n\n\n<p>P(thu nh\u00e2p=trung b\u00ecnh|mua=kh\u00f4ng) =2\/5<\/p>\n\n\n\n<p>P(sinh vi\u00ean=c\u00f3|mua=c\u00f3) =6\/9<\/p>\n\n\n\n<p>P(sinh vi\u00ean-c\u00f3|mua=kh\u00f4ng)=1\/5<\/p>\n\n\n\n<p>&#8230;<\/p>\n\n\n\n<p>S\u1eed d\u1ee5ng c\u00e1c c\u00f4ng th\u1ee9c t\u00ednh x\u00e1c xu\u1ea5t, ch\u00fang ta c\u00f3:<\/p>\n\n\n\n<p>P(X|mua=c\u00f3) = 2\/9&#215;4\/9&#215;6\/9&#215;6\/9 =0.044<\/p>\n\n\n\n<p>T\u01b0\u01a1ng t\u1ef1:<\/p>\n\n\n\n<p>P(X|mua=0)=0.019<\/p>\n\n\n\n<p>\u0110\u1ec3 t\u00ecm l\u1edbp c\u00e1i m\u00e0 t\u1ed1i \u01b0u P(X|Ci)P(Ci), Ch\u00fang ta c\u00f3 th\u1ec3 t\u00ednh to\u00e1n: <\/p>\n\n\n\n<p>P(X|mua=c\u00f3)xP(mua=c\u00f3) = 0.028<\/p>\n\n\n\n<p>P(X|mua=kh\u00f4ng)xP(mua=kh\u00f4ng) = 0.007<\/p>\n\n\n\n<p>V\u00ec v\u1eady, gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive Bayes \u1edf \u0111\u00e2y c\u00f3 th\u1ec3 d\u1ef1 \u0111o\u00e1n mua = c\u00f3 cho m\u1eabu d\u1eef li\u1ec7u X. <\/p>\n\n\n\n<p> <\/p>\n\n\n\n<p>X\u00e2y d\u1ef1ng thu\u1eadt to\u00e1n naive bayes b\u1eb1ng C#, d\u01b0\u1edbi \u0111\u00e2y l\u00e0 \u0111o\u1ea1n m\u00e3 code th\u1ef1c hi\u1ec7n demo thu\u1eadt to\u00e1n Naive bayes s\u1eed d\u1ee5ng C#<\/p>\n\n\n\n<p>Ch\u00fang ta c\u00f3 b\u00e0i to\u00e1n c\u1ea7n ph\u00e2n l\u1edbp: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n            \/\/ Load d\u1eef li\u1ec7u \u0111\u00e0u v\u00e0o\n            \n            string fn = \"D:\/Learning C#\/data2.txt\";\n            int nx = 4;  \/\/ S\u1ed1 thu\u1ed9c t\u00ednh m\u1ed9t m\u1eabu d\u1eef li\u1ec7u\n            int nc = 2;  \/\/ S\u1ed1 L\u1edbp \n            int N = 14;  \/\/ S\u1ed1 m\u1eabu data trong d\u1eef li\u1ec7u h\u1ecdc t\u1eadp\n\n\n            string&#91;]&#91;] data = LoadData(fn, N, nx + 1, ',');\n\n\n            \/\/ \u0110\u1ecdc data training\n            \n            for (int i = 0; i &lt; 10; ++i)\n            {\n                Console.Write(\"&#91;\" + i + \"] \");\n                for (int j = 0; j &lt; nx + 1; ++j)\n                {\n                    Console.Write(data&#91;i]&#91;j] + \" \");\n                }\n                Console.WriteLine(\"\");\n            }\n            \/\/D\u1eef li\u1ec7u c\u1ea7n d\u1ef1 \u0111o\u00e1n \n            int&#91;]&#91;] jointCts = MatrixInt(nx, nc);\r\n            int&#91;] yCts = new int&#91;nc];\r\n            string&#91;] X = new string&#91;] { \"Tr\u1ebb\", \"Trung B\u00ecnh\", \"C\u00f3\",\"Cao\" };\r\n\r\n\r\n            Console.WriteLine(\"Item to classify: \");\n  \r\n            for (int i = 0; i &lt; nx; ++i)\r\n                Console.Write(X&#91;i] + \" \");\r\n            Console.WriteLine(\"\\n\");\r\n\r\n\r\n            \r\n            for (int i = 0; i &lt; N; ++i)\r\n            {\r\n                int y = int.Parse(data&#91;i]&#91;nx]);\r\n                ++yCts&#91;y];\r\n                for (int j = 0; j &lt; nx; ++j)\r\n                {\r\n                    if (data&#91;i]&#91;j] == X&#91;j])\r\n                        ++jointCts&#91;j]&#91;y];\r\n                }\r\n            }\r\n  \/\/ Laplacian smoothing\n\n            for (int i = 0; i &lt; nx; ++i)\n                for (int j = 0; j &lt; nc; ++j)\n                    ++jointCts&#91;i]&#91;j];\n            Console.WriteLine(\"Joint counts: \");\n            \n            \n            for (int i = 0; i &lt; nx; ++i)\n            {\n                for (int j = 0; j &lt; nc; ++j)\n                {\n                    Console.Write(jointCts&#91;i]&#91;j] + \" \");\n                }\n                Console.WriteLine(\"\");\n            }\n            Console.WriteLine(\"\\n Class counts: \");\n            \n            \n            for (int k = 0; k &lt; nc; ++k)\n                Console.Write(yCts&#91;k] + \" \");\n            Console.WriteLine(\"\\n\");\n            \/\/ Compute evidence terms\n            \n            \n            double&#91;] eTerms = new double&#91;nc];\n            for (int k = 0; k &lt; nc; ++k)\n            {\n                double v = 1.0;\n                for (int j = 0; j &lt; nx; ++j)\n                {\n                    v *= (double)(jointCts&#91;j]&#91;k]) \/ (yCts&#91;k] + nx);\n                }\n                v *= (double)(yCts&#91;k]) \/ N;\n                eTerms&#91;k] = v;\n            }\n            \n            Console.WriteLine(\"Evidence terms:\");\n            for (int k = 0; k &lt; nc; ++k)\n                Console.Write(eTerms&#91;k].ToString(\"F4\") + \" \");\n            Console.WriteLine(\"\\n\");\n            double evidence = 0.0;\n            for (int k = 0; k &lt; nc; ++k)\n                evidence += eTerms&#91;k];\n            double&#91;] probs = new double&#91;nc];\n            for (int k = 0; k &lt; nc; ++k)\n                probs&#91;k] = eTerms&#91;k] \/ evidence;\n            Console.WriteLine(\"Probabilities: \");\n            \n            for (int k = 0; k &lt; nc; ++k)\n                Console.Write(probs&#91;k].ToString(\"F4\") + \" \");\n            Console.WriteLine(\"\\n\");\n            int pc = ArgMax(probs);\n            Console.WriteLine(\"Predicted class: \");\n            Console.WriteLine(pc);\n            Console.WriteLine(\"\\nEnd naive Bayes \");\n            Console.ReadLine();\n\r\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>  static string&#91;]&#91;] MatrixString(int rows, int cols)\r\n        {\r\n            string&#91;]&#91;] result = new string&#91;rows]&#91;];\r\n            for (int i = 0; i &lt; rows; ++i)\r\n                result&#91;i] = new string&#91;cols];\r\n            return result;\r\n        }\r\n        \r\n        static int&#91;]&#91;] MatrixInt(int rows, int cols)\r\n        {\r\n            int&#91;]&#91;] result = new int&#91;rows]&#91;];\r\n            for (int i = 0; i &lt; rows; ++i)\r\n                result&#91;i] = new int&#91;cols];\r\n            return result;\r\n        }\r\n        \r\n        static string&#91;]&#91;] LoadData(string fn, int rows,\r\n        int cols, char delimit)\r\n        {\r\n            string&#91;]&#91;] result = MatrixString(rows, cols);\r\n            FileStream ifs = new FileStream(fn, FileMode.Open);\r\n            StreamReader sr = new StreamReader(ifs);\r\n            string&#91;] tokens = null;\r\n            string line = null;\r\n            int i = 0;\r\n            while ((line = sr.ReadLine()) != null)\r\n            {\r\n                tokens = line.Split(delimit);\r\n                for (int j = 0; j &lt; cols; ++j)\r\n                    result&#91;i]&#91;j] = tokens&#91;j];\r\n                ++i;\r\n            }\r\n            sr.Close(); ifs.Close();\r\n            return result;\r\n        }\r\n        \r\n        static int ArgMax(double&#91;] vector)\r\n        {\r\n            int result = 0;\r\n            double maxV = vector&#91;0];\r\n            for (int i = 0; i &lt; vector.Length; ++i)\r\n            {\r\n                if (vector&#91;i] > maxV)\r\n                {\r\n                    maxV = vector&#91;i];\r\n                    result = i;\r\n                }\r\n            }\r\n            return result;\r\n        }\n\n<\/code><\/pre>\n\n\n\n<p>K\u1ebft qu\u1ea3 khi ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"491\" height=\"670\" src=\"https:\/\/cafe2sach.com\/wp-content\/uploads\/2023\/03\/image.png\" alt=\"\" class=\"wp-image-2579\" srcset=\"https:\/\/cafe2sach.com\/wp-content\/uploads\/2023\/03\/image.png 491w, https:\/\/cafe2sach.com\/wp-content\/uploads\/2023\/03\/image-220x300.png 220w, https:\/\/cafe2sach.com\/wp-content\/uploads\/2023\/03\/image-308x420.png 308w\" sizes=\"(max-width: 491px) 100vw, 491px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>N\u1ed9i dung ch&iacute;nh1 Thu\u1eadt to\u00e1n Naive Bayes 1.1 1.1 \u0110\u1ecbnh l\u00fd Bayes 1.2 1.2 Gi\u1ea3i thu\u1eadt ph\u00e2n l\u1edbp Naive bayes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[714,233],"tags":[],"class_list":["post-2553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hoc-may-co-ban","category-tri-tue-nhan-tao"],"_links":{"self":[{"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/posts\/2553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/comments?post=2553"}],"version-history":[{"count":14,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/posts\/2553\/revisions"}],"predecessor-version":[{"id":2584,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/posts\/2553\/revisions\/2584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/media\/2568"}],"wp:attachment":[{"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/media?parent=2553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/categories?post=2553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cafe2sach.com\/index.php\/wp-json\/wp\/v2\/tags?post=2553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}