樸素貝葉斯實現分類_關于樸素貝葉斯分類及其實現的簡短教程

樸素貝葉斯實現分類

Naive Bayes classification is one of the most simple and popular algorithms in data mining or machine learning (Listed in the top 10 popular algorithms by CRC Press Reference [1]). The basic idea of the Naive Bayes classification is very simple.

樸素貝葉斯分類是數據挖掘或機器學習中最簡單,最流行的算法之一(在CRC Press Reference [1]列出的十大流行算法中)。 樸素貝葉斯分類的基本思想很簡單。

(In case you think video format is more suitable for you, you can jump here you can also go to the notebook.)

(如果您認為視頻格式更適合您,則可以跳到此處 ,也可以轉到筆記本 。)

基本直覺: (The basic Intuition:)

Let’s say, we have books of two categories. One category is Sports and the other is Machine Learning. I count the frequency of the words of “Match” (Attribute 1) and Count of the word “Algorithm” (Attribute 2). Let’s assume, I have a total of 6 books from each of these two categories and the count of words across the six books looks like the below figure.

假設我們有兩類書籍。 一類是運動,另一類是機器學習。 我計算“匹配”(屬性1)單詞的出現頻率和“算法”(屬性2)單詞的計數。 假設,我總共擁有這六類書中的六本書,這六本書中的單詞數如下圖所示。

Image for post
Figure 1: Count of words across the books
圖1:書中的單詞數

We see that clearly that the word ‘algorithm’ appears more in Machine Learning books and the word ‘match’ appears more in Sports. Powered with this knowledge, Let’s say if I have a book whose category is unknown. I know Attribute 1 has a value 2 and Attribute 2 has a value 10, we can say the book belongs to Sports Category.

我們清楚地看到,“算法”一詞在機器學習書籍中出現的次數更多,而“匹配”一詞在體育游戲中出現的次數更多。 借助這種知識,假設我有一本書的類別未知。 我知道屬性1的值為2,屬性2的值為10,可以說這本書屬于“體育類別”。

Basically we want to find out which category is more likely, given attribute 1 and attribute 2 values.

基本上,我們希望找出給定屬性1和屬性2值的可能性更大的類別。

Image for post
Figure 2: Finding the category of the book based on counts
圖2:根據計數查找書的類別

從計數到概率: (Moving from count to Probability:)

This count-based approach works fine for a small number of categories and a small number of words. The same intuition is followed more elegantly using conditional probability.

這種基于計數的方法適用于少量類別和少量單詞。 使用條件概率可以更優雅地遵循相同的直覺

Image for post
Figure 3: Conditional Probability (Image Source: Author)
圖3:條件概率(圖片來源:作者)

Conditional Probability is again best understood with an example

再舉一個例子可以更好地理解條件概率

Let’s assume

假設

Event A: The face value is odd | Event B: The face value is less than 4

事件A:面值是奇數| 事件B:面值小于4

P(A) = 3/6 (Favourable cases 1,3,5 Total Cases 1,2,3,4,5,6) similarly P(B) is also 3/6 (Favourable cases 1,2,3 Total Cases 1,2,3,4,5,6). An example of conditional probability is what is the probability of getting an odd number (A)given the number is less than 4(B). For finding this first we find the intersection of events A and B and then we divide by the number of cases in case B. More formally this is given by the equation

P(A)= 3/6(有利案件1,3,5總案件1,2,3,4,5,6)類似,P(B)也是3/6(有利案件1,2,3總案件) 1,2,3,4,5,6)。 條件概率的一個示例是給定奇數(A)小于4(B)的概率是多少。 為了找到這一點,我們首先找到事件A和B的交集,然后除以案例B中的案例數。更正式地說,由等式給出

Image for post
Figure 4: Conditional Probability (Image Source: Author)
圖4:條件概率(圖片來源:作者)

P(A|B) is the conditional probability and is read as the probability of A Given B. This equation forms the central tenet. Let’s now go back again to our book category problem, we want to find the category of the book more formally.

P(A | B)是條件概率,并被解讀為A給定B的概率。該等式形成了中心原則。 現在讓我們再回到書籍類別問題,我們希望更正式地找到書籍的類別。

樸素貝葉斯分類器的條件概率 (Conditional Probability to Naive Bayes Classifier)

Let’s use the following notation Book=ML is Event A, book=Sports is Event B, and “Attribute 1 = 2 and Attribute 2 = 10” is Event C. The event C is a joint event and we will come to this in a short while.

讓我們使用以下表示法Book = ML是事件A,book = Sports是事件B,“屬性1 = 2和屬性2 = 10”是事件C。事件C是聯合事件,我們將在一會兒。

Hence the problem becomes like this we calculate P(A|C) and P(B|C). Let’s say the first one has a value 0.01 and the second one 0.05. Then our conclusion will be the book belongs to the second class. This is a Bayesian Classifier, naive Bayes assumes the attributes are independent. Hence:

因此,問題變得像這樣,我們計算P(A | C)和P(B | C)。 假設第一個值為0.01,第二個值為0.05。 那么我們的結論將是該書屬于第二類。 這是貝葉斯分類器, 樸素貝葉斯假定屬性是獨立的。 因此:

P(Attribute 1 = 2 and Attribute 2 = 10) = P(Attribute 1 = 2) * P(Attribute = 10). Let’s call these conditions as x1 and x2 respectively.

P(屬性1 = 2和屬性2 = 10)= P(屬性1 = 2)* P(屬性= 10)。 我們將這些條件分別稱為x1和x2。

Image for post
Figure 5: Finding the Class with Conditional Probability (Image Source: Author)
圖5:使用條件概率查找類(圖片來源:作者)

Hence, using the likelihood and Prior we calculate the Posterior Probability. And then we assume that the attributes are independent hence likelihood is expanded as

因此,使用似然和先驗,我們計算后驗概率。 然后我們假設屬性是獨立的,因此可能性隨著

Image for post
Figure 6: Expanding Conditional Probability
圖6:擴展條件概率

The above equation is shown for two attributes, however, can be extended for more. So for our specific scenario, the equation get’s changed to the following. It is shown only for Book=’ML’, it will be done similarly for Book =’Sports’.

上面的公式顯示了兩個屬性,但是可以擴展更多。 因此,對于我們的特定情況,方程式get更改為以下形式。 僅在Book ='ML'中顯示,對于Book ='Sports'也將類似地顯示。

Image for post
Fig 7: Naive Bayes Equation for Books Example (Image Source:
圖7:圖書示例的樸素貝葉斯方程(圖片來源:

實現方式: (Implementation:)

Let’s use the famous Flu dataset for naive Bayes and import it, you can change the path. You can download the data from here.

讓我們將著名的Flu數據集用于樸素貝葉斯并將其導入,即可更改路徑。 您可以從此處下載數據。

Importing Data:

匯入資料:

Image for post
Figure 8: Flu Dataset
圖8:流感數據集
nbflu=pd.read_csv('/kaggle/input/naivebayes.csv')

Encoding the Data:

編碼數據:

We store the columns in different variables and encode the same

我們將列存儲在不同的變量中并進行相同的編碼

# Collecting the Variables
x1= nbflu.iloc[:,0]
x2= nbflu.iloc[:,1]
x3= nbflu.iloc[:,2]
x4= nbflu.iloc[:,3]
y=nbflu.iloc[:,4]# Encoding the categorical variables
le = preprocessing.LabelEncoder()
x1= le.fit_transform(x1)
x2= le.fit_transform(x2)
x3= le.fit_transform(x3)
x4= le.fit_transform(x4)
y=le.fit_transform(y)# Getting the Encoded in Data Frame
X = pd.DataFrame(list(zip(x1,x2,x3,x4)))

Fitting the Model:

擬合模型:

In this step, we are going to first train the model, then predict for a patient

在這一步中,我們將首先訓練模型,然后為患者預測

model = CategoricalNB()# Train the model using the training sets
model.fit(X,y)#Predict Output#['Y','N','Mild','Y']
predicted = model.predict([[1,0,0,1]])
print("Predicted Value:",model.predict([[1,0,0,1]]))
print(model.predict_proba([[1,0,0,1]]))

Output:

輸出:

Predicted Value: [1]
[[0.30509228 0.69490772]]

The output tells the probability of not Flu is 0.31 and Flu is 0.69, hence the conclusion will be Flu.

輸出表明非Flu的概率為0.31,Flu為0.69,因此結論為Flu。

Conclusion:

結論

Naive Bayes works very well as a baseline classifier, it’s fast, can work on less number of training examples, can work on noisy data. One of the challenges is it assumes the attributes to be independent.

樸素貝葉斯(Naive Bayes)作為基線分類器的效果非常好,速度很快,可以處理較少數量的訓練示例,可以處理嘈雜的數據。 挑戰之一是它假定屬性是獨立的。

Reference:

參考:

[1] Wu X, Kumar V, editors. The top ten algorithms in data mining. CRC Press; 2009 Apr 9.

[1] Wu X,Kumar V,編輯。 數據挖掘中的十大算法。 CRC出版社; 2009年4月9日。

[2] https://towardsdatascience.com/all-about-naive-bayes-8e13cef044cf

[2] https://towardsdatascience.com/all-about-naive-bayes-8e13cef044cf

翻譯自: https://towardsdatascience.com/a-short-tutorial-on-naive-bayes-classification-with-implementation-2f69183d8ce1

樸素貝葉斯實現分類

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391022.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391022.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391022.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python:改良廖雪峰的使用元類自定義ORM

概要本文僅僅是對廖雪峰老師的使用元類自定義ORM進行改進,并不是要創建一個ORM框架 編寫fieldclass Field(object):def __init__(self, column_type,max_length,**kwargs):1,刪除了參數name,field參數全部為定義字段類型相關參數,…

2019年度年中回顧總結_我的2019年回顧和我的2020年目標(包括數量和收入)

2019年度年中回顧總結In this post were going to take a look at how 2019 was for me (mostly professionally) and were also going to set some goals for 2020! 🤩 在這篇文章中,我們將了解2019年對我來說(主要是職業)如何,我們還將為20…

在Java里重寫equals和hashCode要注意什么問題

問題:在Java里重寫equals和hashCode要注意什么問題 重寫equals和hashCode有哪些問題或者陷阱需要注意? 回答一 理論(對于語言律師或比較傾向于數學的人): equals() (javadoc) 必須定義為一個相等關系(它…

vray陰天室內_陰天有話:第1部分

vray陰天室內When working with text data and NLP projects, word-frequency is often a useful feature to identify and look into. However, creating good visuals is often difficult because you don’t have a lot of options outside of bar charts. Lets face it; ba…

【codevs2497】 Acting Cute

這個題個人認為是我目前所做的最難的區間dp了,以前把環變成鏈的方法在這個題上并不能使用,因為那樣可能存在重復計算 我第一遍想的時候就是直接把環變成鏈了,wa了5個點,然后仔細思考一下就發現了問題 比如這個樣例 5 4 1 2 4 1 1 …

漸進式web應用程序_漸進式Web應用程序與加速的移動頁面:有什么區別,哪種最適合您?

漸進式web應用程序Do you understand what PWAs and AMPs are, and which might be better for you? Lets have a look and find out.您了解什么是PWA和AMP,哪一種可能更適合您? 讓我們看看并找出答案。 So many people own smartphones these days. T…

高光譜圖像分類_高光譜圖像分析-分類

高光譜圖像分類初學者指南 (Beginner’s Guide) This article provides detailed implementation of different classification algorithms on Hyperspectral Images(HSI).本文提供了在高光譜圖像(HSI)上不同分類算法的詳細實現。 目錄 (Table of Contents) Introduction to H…

在Java里如何給一個日期增加一天

在Java里如何給一個日期增加一天 我正在使用如下格式的日期: yyyy-mm-dd. 我怎么樣可以給一個日期增加一天? 回答一 這樣應該可以解決問題 String dt "2008-01-01"; // Start date SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd&q…

CentOS 7安裝和部署Docker

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u010046908/article/details/79553227 Docker 要求 CentOS 系統的內核版本高于 3.10 ,查看本頁面的前提條件來驗證你的CentOS 版本是否支持 Docker 。通過 uname …

JavaScript字符串方法終極指南-拆分

The split() method separates an original string into an array of substrings, based on a separator string that you pass as input. The original string is not altered by split().split()方法根據您作為輸入傳遞的separator字符串,將原始字符串分成子字符串…

機器人的動力學和動力學聯系_通過機器學習了解幸福動力學(第2部分)

機器人的動力學和動力學聯系Happiness is something we all aspire to, yet its key factors are still unclear.幸福是我們所有人都渴望的東西,但其關鍵因素仍不清楚。 Some would argue that wealth is the most important condition as it determines one’s li…

在Java里怎將字節數轉換為我們可以讀懂的格式?

問題:在Java里怎將字節數轉換為我們可以讀懂的格式? 在Java里怎將字節數轉換為我們可以讀懂的格式 像1024應該變成"1 Kb",而1024*1024應該變成"1 Mb". 我很討厭為每個項目都寫一個工具方法。在Apache Commons有沒有這…

ubuntu 16.04 安裝mysql

2019獨角獸企業重金招聘Python工程師標準>>> 1) 安裝 sudo apt-get install mysql-server apt-get isntall mysql-client apt-get install libmysqlclient-dev 2) 驗證 sudo netstat -tap | grep mysql 如果有 就代表已經安裝成功。 3)開啟遠程訪問 1、 …

shell:多個文件按行合并

paste file1 file2 file3 > file4 file1內容為: 1 2 3 file2內容為: a b c file3內容為: read write add file4內容為: 1 a read 2 b write 3 c add 轉載于:https://www.cnblogs.com/seaBiscuit0922/p/7728444.html

form子句語法錯誤_用示例語法解釋SQL的子句

form子句語法錯誤HAVING gives the DBA or SQL-using programmer a way to filter the data aggregated by the GROUP BY clause so that the user gets a limited set of records to view.HAVING為DBA或使用SQL的程序員提供了一種過濾由GROUP BY子句聚合的數據的方法&#xff…

leetcode 1310. 子數組異或查詢(位運算)

有一個正整數數組 arr,現給你一個對應的查詢數組 queries,其中 queries[i] [Li, Ri]。 對于每個查詢 i,請你計算從 Li 到 Ri 的 XOR 值(即 arr[Li] xor arr[Li1] xor … xor arr[Ri])作為本次查詢的結果。 并返回一…

大樣品隨機雙盲測試_訓練和測試樣品生成

大樣品隨機雙盲測試This post aims to explore a step-by-step approach to create a K-Nearest Neighbors Algorithm without the help of any third-party library. In practice, this Algorithm should be useful enough for us to classify our data whenever we have alre…

vue組件命名指南,不為取名而糾結

前言 自古中國取名文化博大進深,往往取一個好的名字而絞盡腦汁.那么一個好名字能夠帶來什么呢? 名字的內涵必需和使用者固有的本性相配套不和名人重名、不易重名、創意新穎,真正體現通過名字以區分人的作用響亮上口讀起來流暢好聽,協音美好,…

JavaScript 基礎,登錄驗證

<script></script>的三種用法&#xff1a;放在<body>中放在<head>中放在外部JS文件中三種輸出數據的方式&#xff1a;使用 document.write() 方法將內容寫到 HTML 文檔中。使用 window.alert() 彈出警告框。使用 innerHTML 寫入到 HTML 元素。使用 &qu…

使用final類的作用是什么?

問題&#xff1a;使用final類的作用是什么&#xff1f; 我在看一本關于Java的書&#xff0c;它里面說你可以定義一個類為final。我搞不明白有什么地方會被用到這樣。 我是一個編程萌新。我想知道程序員在他們的程序里面都是怎么用fianl類的。如果知道他們是什么時候使用的話&…