-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_methods.py
127 lines (103 loc) · 4.27 KB
/
show_methods.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
import time
def show_data_attributes(inputs, labels,attribute_names,class_names):
# Create a figure
figure_handle = plt.figure()
n_attributes = inputs.shape[1]
n_plots = int(n_attributes*(n_attributes-1)/2)
n_plot_rows = int(np.floor(np.sqrt(n_plots)))
n_plot_columns = int(np.ceil(n_plots/n_plot_rows))
# Plot each dimension against another dimension
n_classes = len(class_names)
plot_index = 1
for i in range(n_attributes):
for j in range(i+1,n_attributes):
# Add subplot to the figure
plot_handle = figure_handle.add_subplot(n_plot_rows, n_plot_columns, plot_index)
# Do a scatter plot for each class separately (matplotlib automatically does them in different colours)
for k in range(n_classes):
# Find all the indexes of points of class k
I = np.where(labels==k)[0]
# Show a scatter plot of dimension i vs. dimension j of the points of class k
plot_handle.scatter(inputs[I,i],inputs[I,j])
# Increments the plot_index variable
plot_index += 1
# Label the x and y axis
plot_handle.set_xlabel(attribute_names[i])
plot_handle.set_ylabel(attribute_names[j])
# Show the legend for class colours on the last subplot
plot_handle.legend(class_names)
def show_data_images(images, labels=None, predictions=None, class_names=None,blocking=True):
# Create a figure
figure_handle = plt.figure(figsize=(6,8))
n_images = len(images)
n_plot_rows = int(np.floor(np.sqrt(n_images)))
n_plot_columns = int(np.ceil(n_images/n_plot_rows))
plt.ion()
# Plot each dimension against another dimension
for i in range(n_plot_rows * n_plot_columns):
plot_handle = figure_handle.add_subplot(n_plot_rows, n_plot_columns, i + 1)
if len(np.shape(images)) == 3:
plot_handle.imshow(images[i, :, :], cmap='gray')
else:
plot_handle.imshow(images[i, :, :,:])
plot_handle.tick_params(bottom=False, left=False)
plot_handle.tick_params(labelbottom=False, labelleft=False)
titleStr = ''
if labels is not None:
if class_names is not None:
if len(labels.shape) == 1:
titleStr += 'label: %s' % class_names[labels[i]]
else:
titleStr = ''
for j in labels[i]:
if j<len(class_names):
titleStr += '%s, ' % class_names[j]
titleStr = titleStr[:-2]
elif isinstance(labels[i],list) and len(labels[i])==3:
titleStr = ''
firstLabel = True
for label in labels[i]:
if firstLabel:
firstLabel = False
else:
titleStr += '\n'
titleStr += '%s (%.2f)' % (label[1],label[2])
if predictions is not None:
if labels is not None:
titleStr += '\n'
titleStr += 'pred: %s' % class_names[predictions[i]]
plot_handle.set_title(titleStr)
plt.pause(0.01)
time.sleep(0.01)
plt.show()
if blocking:
plt.ioff()
plt.show()
def show_data_classes(inputs, labels, class_names,mode='pca'):
if len(np.shape(inputs))>2:
n_inputs = len(inputs)
inputs = np.reshape(inputs,(n_inputs,-1))
# Set up PCA compression to 2D
if mode=='pca':
x_2D = PCA(n_components=2).fit_transform(inputs)
elif mode=='tsne':
x_2D = TSNE(n_components=2).fit_transform(inputs)
# Create a new figure
figure_handle = plt.figure()
# Create a subplot
plot_handle = figure_handle.add_subplot(1, 1, 1)
# Do a scatter plot for each class separately (matplotlib automatically does them in different colours)
for k in range(len(class_names)):
# Find all the indexes of points of class k
I = np.where(labels == k)[0]
# Show a scatter plot of dimension i vs. dimension j of the points of class k
plot_handle.scatter(x_2D[I, 0], x_2D[I, 1])
# Show the legend for class colours
if len(class_names) <= 10:
plot_handle.legend(class_names)
# Display the plot title
plot_handle.set_title('Compressed data from %dD to 2D' % inputs.shape[1])