-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.py
109 lines (93 loc) · 3.35 KB
/
visual.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 3 10:51:19 2020
@author: dykua
functions for visualization
"""
import numpy as np
import matplotlib.pyplot as plt
def score_bar(datalist, colorlist, labellist, namelist,
width=0.15, ylim = [0., 1.1], alpha = 0.5, figsize=(8,30),):
'''
datalist: a list of data to plot, each member is a numpy array
colorlist: a list of color for each group member
labellist: a list, name for each group
namelist: a list, name for the legend
'''
# Setting the positions and width for the bars
pos = list(range(len(labellist)))
# width = 0.1
# Plotting the bars
fig, ax = plt.subplots(figsize=figsize)
num_data = len(datalist)
# Create a bar with pre_score data,
# in position pos,
for i in range(num_data):
plt.bar([p+width*i for p in pos], datalist[i],
width=width, alpha=alpha, color=colorlist[i], label=namelist[i])
# Set the y axis label
ax.set_ylabel('Accuracy')
ax.set_xlabel('Models')
# Set the chart's title
# ax.set_title('Summaries')
# Set the position of the x ticks
ax.set_xticks([p + 1.0 * width for p in pos])
# Set the labels for the x ticks
ax.set_xticklabels(labellist)
# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+2*width)
plt.ylim(ylim)
plt.xticks(rotation = 0)
# Adding the legend and showing the plot
plt.legend(loc='upper left')
plt.grid()
plt.show()
def plot_confusion_matrix(cm, classes,
normalize=False,
title=None,
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
cm: the confusion matrix
classes: a list, name for each class
"""
if not title:
if normalize:
title = 'Normalized confusion matrix'
else:
title = 'Confusion matrix'
# Compute confusion matrix
# cm = confusion_matrix(y_true, y_pred)
# Only use the labels that appear in the data
# classes = classes[unique_labels(y_true, y_pred)]
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix')
print(cm)
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
# ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
# ... and label them with the respective list entries
xticklabels=classes, yticklabels=classes,
title=title,
ylabel='True',
xlabel='Predicted')
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center", fontsize='large',
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
return ax