from
sklearn.datasets
import
load_iris
from
sklearn.model_selection
import
train_test_split
from
sklearn.naive_bayes
import
GaussianNB
from
sklearn.metrics
import
classification_report
def
Confusion_matrix(y_test, y_pred, target_names
=
None
):
if
target_names
=
=
None
:
m
=
len
(
set
(y_test))
else
:
m
=
len
(target_names)
size
=
len
(y_test)
matrix
=
dict
()
for
class_name
in
range
(m):
matrix[class_name]
=
[
0
for
k
in
range
(m)]
for
i
in
range
(size):
actual_class
=
y_test[i]
pred_class
=
y_pred[i]
matrix[actual_class][pred_class]
+
=
1
if
target_names
=
=
None
:
print
(
"Confusion Matrix of given model is :"
)
if
m
=
=
3
:
print
(
"Count=%-14d %-15s %-15s %-15s"
%
(size,
'0'
,
'1'
,
'2'
))
for
key, value
in
matrix.items():
print
(
"Actual %-13s %-15d %-15d %-15d"
%
(key, value[
0
], value[
1
], value[
2
]))
elif
m
=
=
2
:
print
(
"Count=%-14d %-15s %-15s"
%
(size,
'0'
,
'1'
))
for
key, value
in
matrix.items():
print
(
"Actual %-13s %-15d %-15d"
%
(key, value[
0
],
value[
1
]))
else
:
matrix
=
dict
(
zip
(target_names,
list
(matrix.values())))
print
(
"Confusion Matrix of given model is :"
)
print
(
"Count=%-14d %-15s %-15s %-15s"
%
(size, target_names[
0
], target_names[
1
], target_names[
2
]))
for
key, value
in
matrix.items():
print
(
"Actual %-13s %-15d %-15d %-15d"
%
(key, value[
0
], value[
1
], value[
2
]))
return
matrix
def
performance_report(cm):
col
=
len
(cm)
arr
=
[]
for
key, value
in
cm.items():
arr.append(value)
cr
=
dict
()
support_sum
=
0
macro
=
[
0
]
*
3
weighted
=
[
0
]
*
3
for
i
in
range
(col):
vertical_sum
=
sum
([arr[j][i]
for
j
in
range
(col)])
horizontal_sum
=
sum
(arr[i])
p
=
arr[i][i]
/
vertical_sum
r
=
arr[i][i]
/
horizontal_sum
f
=
(
2
*
p
*
r)
/
(p
+
r)
s
=
horizontal_sum
row
=
[p,r,f,s]
support_sum
+
=
s
for
j
in
range
(
3
):
macro[j]
+
=
row[j]
weighted[j]
+
=
row[j]
*
s
cr[i]
=
row
truepos
=
0
total
=
0
for
i
in
range
(col):
truepos
+
=
arr[i][i]
total
+
=
sum
(arr[i])
cr[
'Accuracy'
]
=
["
", "
", truepos
/
total, support_sum]
macro_avg
=
[
Sum
/
col
for
Sum
in
macro]
macro_avg.append(support_sum)
cr[
'Macro_avg'
]
=
macro_avg
weighted_avg
=
[
Sum
/
support_sum
for
Sum
in
weighted]
weighted_avg.append(support_sum)
cr[
'Weighted_avg'
]
=
weighted_avg
print
(
"Performance report of the model is :"
)
space,p,r,f,s
=
" "
,
"Precision"
,
"Recall"
,
"F1-Score"
,
"Support"
print
(
"%13s %9s %9s %9s %9s\n"
%
(space,p,r,f,s))
stop
=
0
for
key,value
in
cr.items():
if
stop<col:
stop
+
=
1
print
(
"%13s %9.2f %9.2f %9.2f %9d"
%
(key,value[
0
],
value[
1
],
value[
2
],
value[
3
]))
elif
stop
=
=
col:
stop
+
=
1
print
(
"\n%13s %9s %9s %9.2f %9d"
%
(key,value[
0
],
value[
1
],
value[
2
],
value[
3
]))
else
:
print
(
"%13s %9.2f %9.2f %9.2f %9d"
%
(key,
value[
0
],
value[
1
],
value[
2
],
value[
3
]))
def
main():
dataset
=
load_iris()
X,y,classes
=
dataset[
'data'
],dataset[
'target'
],
dataset[
'target_names'
]
X_train,X_test,y_train,y_test
=
train_test_split(
X,y,shuffle
=
True
,random_state
=
5
,test_size
=
0.3
)
model
=
GaussianNB().fit(X_train,y_train)
y_pred
=
model.predict(X_test)
classes
=
list
(classes)
cm
=
Confusion_matrix(y_test, y_pred, classes)
cr
=
performance_report(cm)
print
(
"\nCR by library method=\n"
,
classification_report(y_test, y_pred))
if
__name__
=
=
'__main__'
:
main()