Skip to content

Commit 503fff2

Browse files
authored
Merge pull request #1391 from kernc/fixups
Fixups
2 parents f51e402 + 074ceca commit 503fff2

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ exclude_lines =
1111
pragma: no cover
1212
raise NotImplementedError
1313
if __name__ == .__main__.:
14+
except MemoryError

Orange/clustering/kmeans.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ class KMeans(SklProjector):
1414
__wraps__ = skl_cluster.KMeans
1515

1616
def __init__(self, n_clusters=8, init='k-means++', n_init=10, max_iter=300,
17-
tol=0.0001, random_state=None, preprocessors=None):
17+
tol=0.0001, random_state=None, preprocessors=None,
18+
compute_silhouette_score=False):
1819
super().__init__(preprocessors=preprocessors)
1920
self.params = vars()
21+
self._compute_silhouette = compute_silhouette_score
2022

2123
def fit(self, X, Y=None):
2224
proj = skl_cluster.KMeans(**self.params)
2325
proj = proj.fit(X, Y)
24-
if 2 <= proj.n_clusters < X.shape[0]:
25-
proj.silhouette = silhouette_score(X, proj.labels_)
26-
else:
27-
proj.silhouette = 0
26+
proj.silhouette = np.nan
27+
try:
28+
if self._compute_silhouette and 2 <= proj.n_clusters < X.shape[0]:
29+
proj.silhouette = silhouette_score(X, proj.labels_)
30+
except MemoryError: # Pairwise dist in silhouette fails for large data
31+
pass
2832
proj.inertia = proj.inertia_ / X.shape[0]
2933
cluster_dist = Euclidean(proj.cluster_centers_)
3034
proj.inter_cluster = np.mean(cluster_dist[np.triu_indices_from(cluster_dist, 1)])

Orange/tests/test_clustering_kmeans.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def test_kmeans_parameters(self):
2525
max_iter=10,
2626
random_state=42,
2727
tol=0.001,
28-
init='random')
28+
init='random',
29+
compute_silhouette_score=True)
2930
c = kmeans(self.iris)
3031

3132
def test_predict_single_instance(self):

Orange/widgets/unsupervised/owkmeans.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def run_optimization(self):
218218
k_to = min(self.k_to, len(self.data))
219219
kmeans = KMeans(
220220
init=['random', 'k-means++'][self.smart_init],
221-
n_init=self.n_init, max_iter=self.max_iterations)
221+
n_init=self.n_init, max_iter=self.max_iterations,
222+
compute_silhouette_score=self.scoring == self.SILHOUETTE)
222223
with self.progressBar(k_to - self.k_from + 1) as progress:
223224
for k in range(self.k_from, k_to + 1):
224225
progress.advance()
@@ -264,7 +265,7 @@ def show_results(self):
264265
best_run = scores.index(best_score)
265266
score_span = (best_score - worst_score) or 1
266267
max_score = max(scores)
267-
nplaces = min(5, int(abs(math.log(max(max_score, 1e-10)))) + 2)
268+
nplaces = min(5, np.floor(abs(math.log(max(max_score, 1e-10)))) + 2)
268269
fmt = "{{:.{}f}}".format(nplaces)
269270
model = self.table_model
270271
model.setRowCount(len(k_scores))
@@ -278,7 +279,8 @@ def show_results(self):
278279
item = model.item(i, 1)
279280
if item is None:
280281
item = QStandardItem()
281-
item.setData(fmt.format(score), Qt.DisplayRole)
282+
item.setData(fmt.format(score) if not np.isnan(score) else 'out-of-memory error',
283+
Qt.DisplayRole)
282284
bar_ratio = 0.95 * (score - worst_score) / score_span
283285
item.setData(bar_ratio, gui.TableBarItem.BarRole)
284286
model.setItem(i, 1, item)

Orange/widgets/widget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def progressBarSet(self, value, processEvents=QEventLoop.AllEvents):
614614
text = "{}:{:02}:{:02}".format(hrs, mins, secs)
615615
else:
616616
text = "{}:{}:{:02}".format(hrs, mins, secs)
617-
self.setWindowTitle("{} ({:.2f}% complete, remaining time: {})"
617+
self.setWindowTitle("{} ({:d}%, ETA: {})"
618618
.format(self.captionTitle, value, text))
619619
else:
620620
self.setWindowTitle(self.captionTitle + " (0% complete)")

0 commit comments

Comments
 (0)