-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_sklearn_NaiveBayes.py
More file actions
53 lines (38 loc) · 1.95 KB
/
Python_sklearn_NaiveBayes.py
File metadata and controls
53 lines (38 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Python tutorial using scikit-learn for Naive Bayes Classifier on the wine dataset.
# The Naive Bayes Algorithm is based off of probabilistic classifiers by applying the Bayes' theorem with strong independence assumptions between the features.
# Python is an interpreted, high-level, general-purpose programming language.
# sci-kit learn or sklearn is an high-level machine learning library for python.
''' Naive Bayes Model
P(h|D) = P(D|h)P(h) / P(D)
P(h|D) = the probability of hypothesis h given the data D
P(D|h) = the probability of data D given the hypothesis h was true
P(h) = the probability of hypothesis h being true (regardless of the data)
p(D) = the probability of the data (regardless of the hypothesis)
'''
# Import python libraries
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
# Import the wine dataset
dataSet = datasets.load_wine()
# Analyze the feature set of the data
#print("Features: ", dataSet.feature_names)
# Analyze the target set of the data
#print("Labels: ", dataSet.target_names)
# Analyze the dataset's shape
#print("Data's Shape: ", dataSet.data.shape)
# Analyze the first five entires of the dataset's values
#print("Data's First Five: ", dataSet.data[0:5])
# Analyze the target set of the data
#print("Data's Target: ", dataSet.target)
# Split the whole dataset into a seperate training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(dataSet.data, dataSet.target, test_size=0.3, random_state=109)
# Create the Naive Bayes Model
theModel = GaussianNB()
# Train the newly created Naive Bayes Model using both the X and y training datasets
theModel.fit(X_train, y_train)
# Predict the testing dataset using the recently trained Naive Bayes model
theModel_Predict = theModel.predict(X_test)
# Analyze the Naive Bayes model's Accuracy results
print("Accuracy: ", metrics.accuracy_score(y_test, theModel_Predict))