-
Notifications
You must be signed in to change notification settings - Fork 3
/
labeller.py
97 lines (84 loc) · 3.5 KB
/
labeller.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
import ipywidgets as widgets
from . import Classifier, Transaction
class Labeller(object):
def __init__(self, transactions, categories):
self.transactions = transactions
self.categories = categories
self.classifier = Classifier()
self._train()
self.container = widgets.VBox([])
self.transaction_container = widgets.VBox(
[],
layout={'height': '15em', 'overflow_y': 'scroll'}
)
self.retrain_button = None
def _train(self):
training_set = [
transaction for transaction in self.transactions
if transaction.category
]
self.classifier.train(training_set, self.categories)
def _render_row(self, transaction, prediction):
[predicted_category, probability] = prediction
return widgets.HBox([
widgets.Label(transaction.memo, layout={'width': '250px'}),
widgets.Dropdown(
options=[''] + self.categories,
value=transaction.category,
layout={'width': '100px'}
),
widgets.Label(predicted_category, layout={'width': '100px'}),
widgets.FloatProgress(
value=probability,
min=1./3.,
max=1.0,
layout={'width': '150px'}
),
], layout={'flex': '1 0 auto'})
def _get_updated_transactions(self):
transactions = []
for row_widget in self.transaction_container.children:
transaction = Transaction(
row_widget.children[0].value,
row_widget.children[1].value
)
transactions.append(transaction)
return transactions
def _render_headers(self):
return widgets.HBox([
widgets.HTML('<b>Memo</b>', layout={'width': '250px'}),
widgets.HTML('<b>Correct</b>', layout={'width': '100px'}),
widgets.HTML('<b>Predicted</b>', layout={'width': '100px'}),
widgets.HTML('<b>Probability</b>', layout={'width': '150px'})
])
def _render_controls(self):
self.retrain_button = widgets.Button(description='retrain')
self.retrain_button.on_click(lambda evt: self.retrain())
return widgets.HBox([self.retrain_button])
def retrain(self):
# disable retrain button while retraining
if self.retrain_button is not None:
self.retrain_button.description = 'retraining...'
self.retrain_button.disabled = True
self.transactions = self._get_updated_transactions()
self._train()
self.render()
if self.retrain_button is not None:
self.retrain_button.description = 'retrain'
self.retrain_button.disabled = False
def render(self):
predictions = self.classifier.predict_proba(self.transactions)
row_widgets = []
zipped_transaction_predictions = zip(self.transactions, predictions)
sorted_transaction_predictions = sorted(
zipped_transaction_predictions, key=lambda entry: entry[1][1])
for (transaction, prediction) in sorted_transaction_predictions:
row_widget = self._render_row(transaction, prediction)
row_widgets.append(row_widget)
headers = self._render_headers()
controls = self._render_controls()
self.transaction_container.children = row_widgets
self.container.children = [
headers, self.transaction_container, controls
]
return self.container