affinity_propagation
- Name: interactive-clustering/src/clustering/affinity_propagation.py
- Description: Implementation of constrained Affinity Propagation clustering algorithm.
- Author: David NICOLAZO, Esther LENOTRE, Marc TRUTT
- Created: 02/03/2022
- Licence: CeCILL (https://cecill.info/licences.fr.html)
AffinityPropagationConstrainedClustering
¶
Bases: AbstractConstrainedClustering
This class will implements the Affinity Propagation constrained clustering.
It inherits from AbstractConstrainedClustering
.
References
- Affinity Propagation Clustering:
Frey, B. J., & Dueck, D. (2007). Clustering by Passing Messages Between Data Points. In Science (Vol. 315, Issue 5814, pp. 972–976). American Association for the Advancement of Science (AAAS). https://doi.org/10.1126/science.1136800
- Constrained Affinity Propagation Clustering:
Givoni, I., & Frey, B. J. (2009). Semi-Supervised Affinity Propagation with Instance-Level Constraints. Proceedings of the Twelth International Conference on Artificial Intelligence and Statistics, PMLR 5:161-168
Example
# Import.
from scipy.sparse import csr_matrix
from cognitivefactory.interactive_clustering.constraints.binary import BinaryConstraintsManager
from cognitivefactory.interactive_clustering.clustering.affinity_propagation import AffinityPropagationConstrainedClustering
# Create an instance of affinity propagation clustering.
clustering_model = AffinityPropagationConstrainedClustering(
random_seed=1,
)
# Define vectors.
# NB : use cognitivefactory.interactive_clustering.utils to preprocess and vectorize texts.
vectors = {
"0": csr_matrix([1.00, 0.00, 0.00, 0.00]),
"1": csr_matrix([0.95, 0.02, 0.02, 0.01]),
"2": csr_matrix([0.98, 0.00, 0.02, 0.00]),
"3": csr_matrix([0.99, 0.00, 0.01, 0.00]),
"4": csr_matrix([0.60, 0.17, 0.16, 0.07]),
"5": csr_matrix([0.60, 0.16, 0.17, 0.07]),
"6": csr_matrix([0.01, 0.01, 0.01, 0.97]),
"7": csr_matrix([0.00, 0.01, 0.00, 0.99]),
"8": csr_matrix([0.00, 0.00, 0.00, 1.00]),
}
# Define constraints manager.
constraints_manager = BinaryConstraintsManager(list_of_data_IDs=list(vectors.keys()))
constraints_manager.add_constraint(data_ID1="0", data_ID2="1", constraint_type="MUST_LINK")
constraints_manager.add_constraint(data_ID1="2", data_ID2="3", constraint_type="MUST_LINK")
constraints_manager.add_constraint(data_ID1="4", data_ID2="5", constraint_type="MUST_LINK")
constraints_manager.add_constraint(data_ID1="7", data_ID2="8", constraint_type="MUST_LINK")
constraints_manager.add_constraint(data_ID1="0", data_ID2="4", constraint_type="CANNOT_LINK")
constraints_manager.add_constraint(data_ID1="2", data_ID2="4", constraint_type="CANNOT_LINK")
constraints_manager.add_constraint(data_ID1="4", data_ID2="7", constraint_type="CANNOT_LINK")
# Run clustering.
dict_of_predicted_clusters = clustering_model.cluster(
constraints_manager=constraints_manager,
vectors=vectors,
####nb_clusters=None,
)
# Print results.
print("Expected results", ";", {"0": 0, "1": 0, "2": 0, "3": 0, "4": 1, "5": 1, "6": 2, "7": 2, "8": 2,}) # TODO:
print("Computed results", ":", dict_of_predicted_clusters)
Warns:
Type | Description |
---|---|
FutureWarning
|
|
Source code in src\cognitivefactory\interactive_clustering\clustering\affinity_propagation.py
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 |
|
__init__(max_iteration=150, convergence_iteration=10, random_seed=None, absolute_must_links=True, **kargs)
¶
The constructor for the Affinity Propagation constrained clustering.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_iteration |
int
|
The maximum number of iteration for convergence. Defaults to |
150
|
convergence_iteration |
int
|
The number of iterations with no change to consider a convergence. Default to |
10
|
absolute_must_links |
bool
|
the option to strictly respect |
True
|
random_seed |
Optional[int]
|
The random seed to use to redo the same clustering. Defaults to |
None
|
**kargs |
dict
|
Other parameters that can be used in the instantiation. |
{}
|
Warns:
Type | Description |
---|---|
FutureWarning
|
|
Raises:
Type | Description |
---|---|
ValueError
|
if some parameters are incorrectly set. |
Source code in src\cognitivefactory\interactive_clustering\clustering\affinity_propagation.py
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 |
|
cluster(constraints_manager, vectors, nb_clusters=None, verbose=False, **kargs)
¶
The main method used to cluster data with the KMeans model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints_manager |
AbstractConstraintsManager
|
A constraints manager over data IDs that will force clustering to respect some conditions during computation. |
required |
vectors |
Dict[str, csr_matrix]
|
The representation of data vectors. The keys of the dictionary represents the data IDs. This keys have to refer to the list of data IDs managed by the |
required |
nb_clusters |
Optional[int]
|
The number of clusters to compute. Here |
None
|
verbose |
bool
|
Enable verbose output. Defaults to |
False
|
**kargs |
dict
|
Other parameters that can be used in the clustering. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if |
Returns:
Type | Description |
---|---|
Dict[str, int]
|
Dict[str,int]: A dictionary that contains the predicted cluster for each data ID. |
Source code in src\cognitivefactory\interactive_clustering\clustering\affinity_propagation.py
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 |
|