-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.py
281 lines (229 loc) · 7.25 KB
/
visualization.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di
import pandas as pd
import numpy as np
import plotly.express as px
import warnings
import os
import plotly.graph_objects as go
import seaborn as sns
from sklearn.preprocessing import MinMaxScaler
from matplotlib import pyplot as plt
import plotly.figure_factory as ff
from skimage import io
#from google.colab import files
import json
import random
def plot_bar(summary, attribute, title):
fig = px.bar(
summary,
x=summary.index,
y=attribute,
text=summary.values,
color_continuous_scale=px.colors.qualitative.Set2,
log_y=True,
color_discrete_sequence = px.colors.qualitative.Set2,
title= title,
color=summary.index,
)
fig.update_traces(
texttemplate='%{text:.2s}',
textposition='outside',
)
fig.update_traces(hovertemplate= attribute + ' category: %{x} <br>Occurrences: %{y}')
fig.update_layout(
xaxis_title= attribute +" categories",
yaxis_title="Number of rows",
coloraxis_showscale=False,
xaxis_type='category',
width=1000,
height=500,
hovermode='closest',
hoverlabel=dict(
bgcolor="white",
)
)
fig.show()
def plot_score_outliers(score_sample, offset, title,x_position_outlier_ann, y_position_outlier_ann ):
fig = go.Figure()
fig.update_layout(
title = title,
xaxis_title= "Training dataset point",
yaxis_title= "Score",
showlegend=False,
shapes=[
# 1st highlight during Feb 4 - Feb 6
dict(
type="rect",
# x-reference is assigned to the x-values
# y-reference is assigned to the plot paper [0,1]
x0=0,
y0=min(score_sample),
x1=len(score_sample),
y1=offset,
fillcolor="Crimson",
opacity=0.3,
layer="below",
line_width=0,
),
]
)
fig.add_trace(
go.Scatter(
x=[i for i in range(0, len(score_sample))],
y=np.sort(score_sample),
mode="markers",
#text=[round(v*100, 1) for v in np.cumsum(pca.explained_variance_ratio_)],
textposition="bottom center",
hovertemplate = "Point index: %{x}<br>Score: %{y:.2f}",
textfont=dict(
size=12,
),
marker=dict(
color='rgb(229,196,148)',
size=3,
),
line = dict(
width=4
),
),
)
fig.add_shape(
# Line Vertical
dict(
type="line",
x0=0,
y0=offset,
x1=len(score_sample),
y1=offset,
line=dict(
color="Crimson",
width=2,
),
))
fig.update_xaxes(showspikes=True)
fig.update_yaxes(showspikes=True)
fig.add_annotation(
x=x_position_outlier_ann,
y=y_position_outlier_ann,
text="Candidate outliers",
#showarrow=False,
font=dict(
size=18,
)
)
fig.show()
def plot_scattermatrix(dimensions, title, df):
fig = px.scatter_matrix(df,
dimensions=dimensions,
color='default.payment.next.month',
color_discrete_sequence = px.colors.qualitative.Set2,
color_continuous_scale = px.colors.qualitative.Set2,
symbol ='default.payment.next.month',
)
fig.update_layout(
title = title,
coloraxis_showscale=False,
width=1700,
height=1700,
legend=dict(
yanchor="top",
font = dict(
size=16
),
xanchor="right",
)
)
fig.update_traces(diagonal_visible=False,
showupperhalf=False,
marker=dict(
colorscale=px.colors.qualitative.Set2,
#showscale=False, # colors encode categorical variables
line_color='white',
line_width=0,
size=2
),)
fig.show()
import plotly.figure_factory as ff
def plot_confusion_matrix(y_true, y_pred):
tp = ((y_pred == 1)&(y_true == 1)).sum()
fp = ((y_pred == 1)&(y_true == 0)).sum()
tn = ((y_pred == 0)&(y_true == 0)).sum()
fn = ((y_pred == 0)&(y_true == 1)).sum()
tpr = tp/(tp + fn)
tnr = tn/(tn + fp)
fpr = 1 - tnr
fnr = 1- tpr
# | Positive Prediction | Negative Prediction
# Positive Class | True Positive (TP) | False Negative (FN)
# Negative Class | False Positive (FP) | True Negative (TN)
z = [[tpr, fpr],
[fnr, tnr]]
z_text = [['True Positive rate: ' + str(round(tpr, 2)), 'False Positive rate: ' + str(round(fpr, 2))],
['False Negative rate: ' + str(round(fnr, 2)), 'True Negative rate: ' + str(round(tnr, 2))]]
x = ['1', '0']
y = ['1', '0']
# set up figure
fig = ff.create_annotated_heatmap(
z,
x=x,
y=y,
annotation_text=z_text,
colorscale= [[0,'rgb(256,256,256)'], [1, 'rgb(27,158,119)']],
hoverinfo = "text"
)
# add custom xaxis title
fig.add_annotation(dict(font=dict(size=20),
x=0.5,
y=-0.1,
showarrow=False,
text="True value",
xref="paper",
yref="paper"))
# add custom yaxis title
fig.add_annotation(dict(font=dict(size=20),
x=-0.1,
y=0.5,
showarrow=False,
text="Predicted value",
textangle=-90,
xref="paper",
yref="paper"))
# adjust margins to make room for yaxis title
fig.update_layout(
margin=dict(t=50, l=200),
title_text='Confusion matrix',
font=dict(size=16)
)
fig.show()
return z
def print_result(s, report):
s_table= ""
for name, metrics in report.items():
if not name == 'accuracy':
s_table += "<tr><td>" + name +"</td>"
for metric_name, metric in metrics.items():
if not metric_name == 'support':
s_table += '<td>' + str(round(metric, 2)) + '</td>'
s_table += "</tr>"
s_table += ""
s_table += '<tr><td>accuracy</td><td colspan=3>' + str(round(report["accuracy"], 2)) + '</td></tr>'
di.display_html("""
<p style='margin-bottom: 1em;font-size:15px'>
""" + s + """
</p>
<table id="customers">
<thead>
<tr class="second">
<th></th>
<th>Precision</th>
<th>Recall</th>
<th>F1-score</th>
</tr>
</thead>
<tbody>
""" + s_table+ """
</tbody>
</table>
""", raw=True)