-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport_functions.py
63 lines (51 loc) · 2.12 KB
/
support_functions.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
import numpy as np
import itertools
from matplotlib import pyplot as plt
def calculate_accuracy(predicted_classes, actual_classes, ):
return sum(actual_classes[:] == predicted_classes[:]) / len(actual_classes)
def generate_features_targets(data):
output_targets = np.empty(shape=(len(data)), dtype='<U20')
output_targets[:] = data['class']
input_features = np.empty(shape=(len(data), 13))
input_features[:, 0] = data['u-g']
input_features[:, 1] = data['g-r']
input_features[:, 2] = data['r-i']
input_features[:, 3] = data['i-z']
input_features[:, 4] = data['ecc']
input_features[:, 5] = data['m4_u']
input_features[:, 6] = data['m4_g']
input_features[:, 7] = data['m4_r']
input_features[:, 8] = data['m4_i']
input_features[:, 9] = data['m4_z']
input_features[:, 10] = data['petroR50_u'] / data['petroR90_u']
input_features[:, 11] = data['petroR50_r'] / data['petroR90_r']
input_features[:, 12] = data['petroR50_z'] / data['petroR90_z']
return input_features, output_targets
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.get_cmap('nipy_spectral',10)):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, "{}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True Class')
plt.xlabel('Predicted Class')