Skip to content

Commit

Permalink
Fix zero division error (classifying 'empty' docs)
Browse files Browse the repository at this point in the history
When a document that produced as a classification result all-zero
vectors, a ZeroDivision error was thrown. On the Live Test this error
produced an infinity loop freezing the browser's Live Test tab, this was
due to zero division producing NaN instead of throwing an exception
which made the while loop condition never false (e.g. writing "=" as an
input document with a model with more than 2 categories will freeze the
Live Test tab).
  • Loading branch information
sergioburdisso committed Feb 29, 2020
1 parent 7afadce commit bc5c4ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions pyss3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,8 +2330,10 @@ def kmean_multilabel_size(res):
clust["neg"].append(cat_cv)
else:
clust["pos"].append(cat_cv)
new_cent_neg = sum(clust["neg"]) / len(clust["neg"])
new_cent_pos = sum(clust["pos"]) / len(clust["pos"])
if len(clust["neg"]) > 0:
new_cent_neg = sum(clust["neg"]) / len(clust["neg"])
if len(clust["pos"]) > 0:
new_cent_pos = sum(clust["pos"]) / len(clust["pos"])
return len(clust["pos"])


Expand Down
4 changes: 2 additions & 2 deletions pyss3/resources/visual_classifier/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ <h5 style="margin-top: 0;">
<span ng-show="ss3.cvns[0][2] < .75" class="grey-text">
[ more evidence is required <span ng-show="ss3.cvns[0][2] > .25">but if I had to guess, I would say <span class="pink-text">{{ss3.ci[ss3.cvns[0][0]]}}</span></span> ]
</span>
<span style="display:inline-block" ng-if="ss3 && is_cat_active(ss3.cvns[0])">
<span style="display:inline-block" ng-if="ss3 && is_cat_active(ss3.cvns[0]) && ss3.cvns[0][2] >= .75">
<span class="pointer pulse" ng-style="{'opacity': ss3.cvns[0][1]}" ng-click="select_cat(ss3.cvns[0][0])">{{ss3.ci[ss3.cvns[0][0]]}}</span>
</span>
</h5>
<h5 ng-if="ss3 && ncats > 2 && is_cat_active(ss3.cvns[1])">
<h5 ng-if="ss3 && ncats > 2 && is_cat_active(ss3.cvns[1]) && ss3.cvns[0][1]">
<span style="color: black">
<span ng-show="ss3.cvns[1][1] < .25">Hmmm...Just Guessing...</span>
<span ng-show="ss3.cvns[1][1] < .5">Maybe </span>
Expand Down
6 changes: 4 additions & 2 deletions pyss3/resources/visual_classifier/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ app.controller("mainCtrl", function($scope) {
active_cats.push(cats[i][0]);
}
}
new_cent_neg = clust.neg.reduce((a,b) => a + b, 0) / clust.neg.length;
new_cent_pos = clust.pos.reduce((a,b) => a + b, 0) / clust.pos.length;
if (clust.neg.length > 0)
new_cent_neg = clust.neg.reduce((a,b) => a + b, 0) / clust.neg.length;
if (clust.pos.length > 0)
new_cent_pos = clust.pos.reduce((a,b) => a + b, 0) / clust.pos.length;
}
return active_cats;
}
Expand Down

0 comments on commit bc5c4ed

Please sign in to comment.