-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaiveBayes.py
62 lines (50 loc) · 1.86 KB
/
naiveBayes.py
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
53
54
55
56
57
58
59
60
61
62
#-------------------- Required imports --------------------#
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn import tree
def naiveBayes(X,y):
#Create a training / test split of the data.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
#random_state = 1 - This makes the code consistent
#Create naive bayes classifer object.
clf = GaussianNB()
#Train naive bayes Classifer
clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
#Determine the acuracy of the classifier.
total = 0
equal = 0
for i,j in zip(y_pred,y_test):
total+=1
if i==j:
equal +=1
print(i , j , i==j, (equal/total))
#Accuracy metric using sklearn
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
return round(equal/total,2)
if __name__ == "__main__":
#Read the name file for aquiring the correct column names.
#with open("iris.names") as f:
#print(f.read())
#Read in the file
filename = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
#Set the column names
names = ["Sepal Length","Sepal Width","Petal length","Petal Width","Class"]
#Dictionary to map strings to values
mapper = {'Iris-setosa':0, 'Iris-versicolor':1,'Iris-virginica':2}
#Create dataframe
df = pd.read_csv(filename , names = names)
#Map the strings to the correct values.
df["Class"] = df["Class"].map(mapper)
#Testing print
#print(df.head())
#Define features and class variable.
X = df[names[0:-1]] #Features
y = df.Class #Define class variable
accuracy = naiveBayes(X,y)
print(accuracy)