-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naive Bayes - S V M.txt
82 lines (60 loc) · 2.34 KB
/
Naive Bayes - S V M.txt
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import csv
import numpy as np
import pandas as pd
mydata = pd.read_csv('IRIS.csv')
mydata.head(2)
mydata['species'].unique()
mydata.describe(include = 'all')
import seaborn as sns
import matplotlib.pyplot as plt
sns.pairplot(mydata)
g = sns.relplot( x = 'petal_length', y = 'petal_width', data = mydata, hue = 'species', style = 'species')
g.fig.set_size_inches(10, 5)
plt.show()
x = mydata.iloc[:, 0:4].values
y = mydata.iloc[:, 4].values
x
y
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)
y
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 42)
Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
y_pred = gaussian.predict(x_test)
accuracy_nb = round(accuracy_score(y_test, y_pred)*100, 2)
acc_gaussian = round(gaussian.score(x_train, y_train)*100, 2)
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average = 'micro')
recall = recall_score(y_test, y_pred, average = 'micro')
f1 = f1_score(y_test, y_pred, average = 'micro')
print('Confusion Matrix from Naive Bayes \n', cm)
print('Accuracy : %.3f' %accuracy)
print('Precision : %.3f' %precision)
print('Recall : %.3f' %recall)
print('F1score : %.3f' %f1)
Support Vector Machine
from sklearn.svm import SVC, LinearSVC
linear_svc = LinearSVC(max_iter = 4000)
linear_svc.fit(x_train, y_train)
y_pred = linear_svc.predict(x_test)
accuracy_svc = round(accuracy_score(y_test, y_pred)*100, 2)
acc_linear_svc = round(linear_svc.score(x_train, y_train)*100, 2)
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average = 'micro')
recall = recall_score(y_test, y_pred, average = 'micro')
f1 = f1_score(y_test, y_pred, average = 'micro')
print('Confusion Matrix from Naive Bayes \n', cm)
print('Accuracy : %.3f' %accuracy)
print('Precision : %.3f' %precision)
print('Recall : %.3f' %recall)
print('F1score : %.3f' %f1)