Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the naive Bayes of python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the python naive Bayes how to achieve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this python naive Bayes article, let's take a look.

Background introduction

This is a classification technique based on Bayesian theorem, which assumes that the predictive variables are independent. In short, naive Bayesian classifiers assume that the existence of a particular feature in a class has nothing to do with the existence of any other features. For example, if the fruit is red, round and about 3 inches in diameter, you can think of it as an apple. Even if these features depend on each other or on the existence of other features, the naive Bayesian classifier will consider all these features to independently promote the possibility that the fruit is an apple.

Naive Bayesian models are easy to build and are especially useful for very large datasets. In addition to simplicity, naive Naive Bayes trumps very complex classifications.

Bayesian theorem provides a method to calculate a posteriori probability P (c | x) from P (c), P (x) and P (x | c).

Here,

P (c | x) is the posterior probability of the class (target) of a given prediction variable (attribute).

P (c) is the prior probability of classification.

P (x | c) is the degree of likelihood, which is the probability of a given category of prediction variables.

P (x) is the prior probability of the predictive variable.

Example: let's understand it through an example. The following is the weather training data set and the corresponding target variable "play". Now, we need to classify whether players participate in the game according to the weather. Let's follow these steps.

Step 1: convert the dataset to a frequency table

Step 2: create a likelihood table by finding probability (such as cloudy day probability = 0.29 and game probability = 0.64).

Step 3: now, use the naive Bayesian equation to calculate a posteriori probability for each category. The category with the highest a posteriori probability is the predicted result.

Question: is it true that players will go out to play if the weather is clear?

We can use the method discussed above to solve it, so P (Yes | Sunny) = P (Sunny | Yes) * P (Yes) / P (Sunny)

Here we have P (Sunny | Yes) = 3 Sunny 9 = 0.33, P (Yes) = 5max 14 = 0.36, P (Yes) = 9 max 14 = 0.64

Now, P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60, which is more likely.

Naive Bayes (Naive Bayes) uses a similar method to predict the probabilities of different categories based on various attributes. The algorithm is mainly used for text classification and has the problem of multiple classes.

Write a naive Bayesian classification model in 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)

Running result:

Shape of training data: (712,25) Shape of testing data: (179) 25) Target on train data [1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1] Accuracy_score on train dataset: 0.44803370786516855Target on test data [1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1] accuracy_score on test dataset: 0.35195530726256985 on "how to achieve python naive Bayes" is introduced here. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve python naive Bayes". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report