Skip to content

Commit b8e9405

Browse files
committed
Merge branch 'release-1.2.0' into develop
2 parents 80e0c58 + 2b54c18 commit b8e9405

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
1.2.0
2+
- Multiprocessing (by loisaidasam)
23
- Python 3 support
34
- Split up one big file into smaller more logical sub-modules
5+
- Fixed https://github.com/exhuma/python-cluster/issues/11
6+
- Documentation update.
47

58
1.1.1b3
69
- Fixed bug #1727558

cluster/method/hierarchical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class HierarchicalClustering(BaseClusterMethod):
3131
Implementation of the hierarchical clustering method as explained in a
3232
tutorial_ by *matteucc*.
3333
34-
.. _tutorial:: http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/hierarchical.html
34+
.. _tutorial: http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/hierarchical.html
3535
3636
Example:
3737
@@ -61,7 +61,7 @@ def __init__(self, data, distance_function, linkage=None, num_processes=1):
6161
linkage = 'single'
6262
logger.info("Initializing HierarchicalClustering object with linkage "
6363
"method %s", linkage)
64-
BaseClusterMethod.__init__(self, data, distance_function)
64+
BaseClusterMethod.__init__(self, sorted(data), distance_function)
6565
self.set_linkage_method(linkage)
6666
self.num_processes = num_processes
6767
self.__cluster_created = False

docs/index.rst

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ Welcome to python-cluster's documentation!
33

44
Implementation of cluster algorithms in pure Python.
55

6-
.. warning::
76

8-
This is currently a bug in HierarchicalClustering which causes incorrect
9-
output. This is currently being worked on. In the meantime
10-
``HierarchicalClustering`` should be regarded as *broken*!
117

12-
Example
13-
-------
8+
Example for K-Means Clustering
9+
------------------------------
1410

1511
::
1612

@@ -30,7 +26,7 @@ Example
3026
(9, 3)
3127
]
3228
cl = KMeansClustering(data)
33-
c.getclusters(2)
29+
cl.getclusters(2)
3430

3531
The above code would give the following result::
3632

@@ -39,6 +35,44 @@ The above code would give the following result::
3935
[(3, 5), (1, 5), (3, 4), (2, 6), (2, 5), (3, 6)]
4036
]
4137

38+
39+
Example for Hierarchical Clustering
40+
-----------------------------------
41+
42+
::
43+
44+
from cluster import HierarchicalClustering
45+
data = [791, 956, 676, 124, 564, 84, 24, 365, 594, 940, 398,
46+
971, 131, 365, 542, 336, 518, 835, 134, 391]
47+
cl = HierarchicalClustering(data)
48+
cl.getlevel(40)
49+
50+
The above code would give the following result::
51+
52+
[
53+
[24],
54+
[84, 124, 131, 134],
55+
[336, 365, 365, 391, 398],
56+
[676],
57+
[594, 518, 542, 564],
58+
[940, 956, 971],
59+
[791],
60+
[835],
61+
]
62+
63+
64+
Using :py:meth:`~cluster.method.hierarchical.HierarchicalClustering.getlevel()`
65+
returns clusters where the distance between each cluster is no less than
66+
*level*.
67+
68+
.. note::
69+
70+
Due to a bug_ in earlier releases, the elements of the input data *must be*
71+
sortable!
72+
73+
.. _bug: https://github.com/exhuma/python-cluster/issues/11
74+
75+
4276
API
4377
---
4478

test.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,22 @@ class HClusterIntegerTestCase(unittest.TestCase):
7171

7272
def setUp(self):
7373
self.__data = [791, 956, 676, 124, 564, 84, 24, 365, 594, 940, 398,
74-
971, 131, 365, 542, 336, 518, 835, 134, 391]
74+
971, 131, 365, 542, 336, 518, 835, 134, 391]
7575

7676
def testCluster(self):
7777
"Basic Hierarchical Clustering test with integers"
7878
cl = HierarchicalClustering(self.__data, lambda x, y: abs(x - y))
7979
cl.cluster()
8080
self.assertEqual([
81-
[24],
82-
[84, 124, 131, 134],
83-
[336, 365, 365, 365, 398, 391],
84-
[940, 956, 971],
85-
[791],
86-
[835],
87-
[676],
88-
[518, 564, 542]],
89-
cl.getlevel(40))
81+
[24],
82+
[84, 124, 131, 134],
83+
[336, 365, 365, 391, 398],
84+
[676],
85+
[594, 518, 542, 564],
86+
[940, 956, 971],
87+
[791],
88+
[835],
89+
], cl.getlevel(40))
9090

9191
def testUnmodifiedData(self):
9292
cl = HierarchicalClustering(self.__data, lambda x, y: abs(x - y))
@@ -102,9 +102,9 @@ def sim(self, x, y):
102102
return 1 - sm.ratio()
103103

104104
def setUp(self):
105-
self.__data = ("Lorem ipsum dolor sit amet, consectetuer adipiscing "
106-
"elit. Ut elit. Phasellus consequat ultricies mi. Sed congue "
107-
"leo at neque. Nullam."). split()
105+
self.__data = ("Lorem ipsum dolor sit amet consectetuer adipiscing "
106+
"elit Ut elit Phasellus consequat ultricies mi Sed "
107+
"congue leo at neque Nullam").split()
108108

109109
def testDataTypes(self):
110110
"Test for bug #?"
@@ -118,19 +118,17 @@ def testCluster(self):
118118
"Basic Hierachical clustering test with strings"
119119
cl = HierarchicalClustering(self.__data, self.sim)
120120
self.assertEqual([
121-
['Nullam.'],
122-
['Sed'],
123-
['mi.'],
124-
['ultricies'],
125-
['Phasellus'],
126-
['amet,', 'at'],
127-
['sit', 'elit.', 'elit.', 'elit.'],
128-
['leo', 'Lorem', 'dolor'],
129-
['neque.', 'congue', 'consectetuer', 'consequat'],
130-
['ipsum'],
131-
['adipiscing']
132-
],
133-
cl.getlevel(0.5))
121+
['ultricies'],
122+
['Sed'],
123+
['Phasellus'],
124+
['mi'],
125+
['Nullam'],
126+
['sit', 'elit', 'elit', 'Ut', 'amet', 'at'],
127+
['leo', 'Lorem', 'dolor'],
128+
['congue', 'neque', 'consectetuer', 'consequat'],
129+
['adipiscing'],
130+
['ipsum'],
131+
], cl.getlevel(0.5))
134132

135133
def testUnmodifiedData(self):
136134
cl = HierarchicalClustering(self.__data, self.sim)

0 commit comments

Comments
 (0)