python樸素貝葉斯怎么實現(xiàn)

這篇文章主要介紹了python樸素貝葉斯怎么實現(xiàn)的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python樸素貝葉斯怎么實現(xiàn)文章都會有所收獲,下面我們一起來看看吧。

成都創(chuàng)新互聯(lián)專注于微山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供微山營銷型網(wǎng)站建設(shè),微山網(wǎng)站制作、微山網(wǎng)頁設(shè)計、微山網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造微山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供微山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

背景介紹

這是一種基于貝葉斯定理的分類技術(shù),假設(shè)預測變量之間具有獨立性。簡而言之,樸素貝葉斯分類器假定類中某個特定特征的存在與任何其他特征的存在無關(guān)。例如,如果水果是紅色,圓形且直徑約3英寸,則可以將其視為蘋果。即使這些特征相互依賴或依賴于其他特征的存在,樸素的貝葉斯分類器也會考慮所有這些特征,以獨立地促成該果實是蘋果的可能性。

python樸素貝葉斯怎么實現(xiàn)

樸素貝葉斯模型易于構(gòu)建,對于非常大的數(shù)據(jù)集特別有用。 除了簡單之外,樸素貝葉斯(Naive Bayes)還勝過非常復雜的分類方法。

貝葉斯定理提供了一種從P(c),P(x)和P(x | c)計算后驗概率P(c | x)的方法。 

這里,

  • P(c | x)是給定預測變量(屬性)的類(目標)的后驗概率。

  • P(c)是分類的先驗概率。

  • P(x | c)是似然度,它是預測變量給定類別的概率。

  • P(x)是預測變量的先驗概率。

例子:讓我們通過一個例子來理解它。下面是天氣的訓練數(shù)據(jù)集和相應的目標變量“玩?!薄,F(xiàn)在,我們需要根據(jù)天氣情況對玩家是否參加比賽進行分類。讓我們按照以下步驟進行操作。

步驟1:將資料集轉(zhuǎn)換為頻率表

步驟2:通過找到概率(如陰天概率= 0.29和游戲概率為0.64)來創(chuàng)建似然度表。

python樸素貝葉斯怎么實現(xiàn)

步驟3:現(xiàn)在,使用樸素貝葉斯方程來計算每個類別的后驗概率。后驗概率最高的類別是預測的結(jié)果。

問題:如果天氣晴朗,玩家將出去玩,這個說法正確嗎?

我們可以使用上面討論的方法來解決它,所以 P(Yes | Sunny) = P( Sunny | Yes) * P(Yes) / P (Sunny)

這里我們有 P (Sunny |Yes) = 3/9 = 0.33, P(Sunny) = 5/14 = 0.36, P( Yes)= 9/14 = 0.64

現(xiàn)在,P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60,這更有可能。

樸素貝葉斯(Naive Bayes)使用類似的方法根據(jù)各種屬性來預測不同類別的概率。該算法主要用于文本分類,并且存在多個類的問題。

用Python編寫一個樸素貝葉斯分類模型:

'''The following code is for Naive BayesCreated by - ANALYTICS VIDHYA'''
# importing required librariesimport pandas as pdfrom sklearn.naive_bayes import GaussianNBfrom sklearn.metrics import accuracy_score
# read the train and test datasettrain_data = pd.read_csv('train-data.csv')test_data = pd.read_csv('test-data.csv')
# shape of the datasetprint('Shape of training data :',train_data.shape)print('Shape of testing data :',test_data.shape)
# Now, we need to predict the missing target # variable in the test data# target variable - Survived
# seperate the independent and target variable on training datatrain_x = train_data.drop(columns=['Survived'],axis=1)train_y = train_data['Survived']
# seperate the independent and target variable on testing datatest_x = test_data.drop(columns=['Survived'],axis=1)test_y = test_data['Survived']
'''Create the object of the Naive Bayes modelYou can also add other parameters and test your code hereSome parameters are : var_smoothingDocumentation of sklearn GaussianNB:
https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html
'''model = GaussianNB()
# fit the model with the training datamodel.fit(train_x,train_y)
# predict the target on the train datasetpredict_train = model.predict(train_x)print('Target on train data',predict_train)
# Accuray Score on train datasetaccuracy_train = accuracy_score(train_y,predict_train)print('accuracy_score on train dataset : ', accuracy_train)
# predict the target on the test datasetpredict_test = model.predict(test_x)print('Target on test data',predict_test)
# Accuracy Score on test datasetaccuracy_test = accuracy_score(test_y,predict_test)print('accuracy_score on test dataset : ', accuracy_test)

運行結(jié)果:

Shape of training data : (712, 25)Shape of testing data : (179, 25)Target on train data [1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]accuracy_score on train dataset :  0.44803370786516855Target on test data [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]accuracy_score on test dataset :  0.35195530726256985

關(guān)于“python樸素貝葉斯怎么實現(xiàn)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“python樸素貝葉斯怎么實現(xiàn)”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:python樸素貝葉斯怎么實現(xiàn)
轉(zhuǎn)載注明:http://muchs.cn/article48/jchjep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、軟件開發(fā)、定制網(wǎng)站、虛擬主機、、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營