forked from petermattia/predicting-battery-lifetime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoeff_plotter.py
74 lines (61 loc) · 1.84 KB
/
coeff_plotter.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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import csv
# Read in data: DataIN
filename = 'results/norm_coeffs.csv'
FMatrix = np.genfromtxt(filename, delimiter=',')
FMatrix = FMatrix[1:,1:]
nF = FMatrix.shape[0]
nC = FMatrix.shape[1]
# Read in headers: Features & Cycles
f = open(filename, 'rt')
reader = csv.reader(f)
Cycles = next(reader, None)
column = {}
for h in Cycles:
column[h] = []
Features = []
for row in reader:
for h, v in zip(Cycles, row):
Features.append(v)
nDel = 0
for i in range(nF):
for j in range(nC+1):
if j == 0:
nDel += 1
else:
del Features[nDel]
del Cycles[0]
FMatrix = abs(FMatrix*100)
for i in range(nF):
for j in range(nC):
if FMatrix[i,j] == 0:
FMatrix[i,j] = float('nan')
# Plot
fig, ax = plt.subplots(figsize=(10,10))
im = ax.imshow(FMatrix)
matplotlib.rcParams.update({'font.size': 18})
# Create colorbar
cbarlabel="Feature weight * 100"
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom", size=18)
# We want to show all ticks...
ax.set_xticks(np.arange(len(Cycles)))
ax.set_yticks(np.arange(len(Features)))
# ... and label them with the respective list entries
ax.set_xticklabels(Cycles)
ax.set_yticklabels(Features)
# 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.
for i in range(nF):
for j in range(nC):
if FMatrix[i,j] != 0:
text = ax.text(j, i, np.round(FMatrix[i,j],1), ha="center", va="center", color="w")
ax.set_title("Feature selection for elastic net", weight='bold', size=20)
plt.xlabel('Cycle number')
fig.tight_layout()
plt.savefig('./plots/enet_features.png')
plt.savefig('./plots/enet_features.svg')