clusters_based
- Name: cognitivefactory.interactive_clustering.sampling.clusters_based
- Description: Implementation of constraints sampling based on clusters information.
- Author: Erwan SCHILD
- Created: 04/10/2021
- Licence: CeCILL (https://cecill.info/licences.fr.html)
ClustersBasedConstraintsSampling
¶
Bases: AbstractConstraintsSampling
This class implements the sampling of data IDs based on clusters information in order to annotate constraints.
It inherits from AbstractConstraintsSampling
.
Example
# Import.
from cognitivefactory.interactive_clustering.constraints.binary import BinaryConstraintsManager
from cognitivefactory.interactive_clustering.sampling.clusters_based import ClustersBasedConstraintsSampling
# Create an instance of random sampling.
sampler = ClustersBasedConstraintsSampling(random_seed=1)
# Define list of data IDs.
list_of_data_IDs = ["bonjour", "salut", "coucou", "au revoir", "a bientôt",]
# Define constraints manager.
constraints_manager = BinaryConstraintsManager(
list_of_data_IDs=list_of_data_IDs,
)
constraints_manager.add_constraint(data_ID1="bonjour", data_ID2="salut", constraint_type="MUST_LINK")
constraints_manager.add_constraint(data_ID1="au revoir", data_ID2="a bientôt", constraint_type="MUST_LINK")
# Run sampling.
selection = sampler.sample(
constraints_manager=constraints_manager,
nb_to_select=3,
)
# Print results.
print("Expected results", ";", [("au revoir", "bonjour"), ("bonjour", "coucou"), ("a bientôt", "coucou"),])
print("Computed results", ":", selection)
Source code in src\cognitivefactory\interactive_clustering\sampling\clusters_based.py
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 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 242 243 244 245 246 247 |
|
__init__(random_seed=None, clusters_restriction=None, distance_restriction=None, without_added_constraints=True, without_inferred_constraints=True, **kargs)
¶
The constructor for Clusters Based Constraints Sampling class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
random_seed |
Optional[int]
|
The random seed to use to redo the same sampling. Defaults to |
None
|
clusters_restriction |
Optional[str]
|
Restrict the sampling with a cluster constraints. Can impose data IDs to be in |
None
|
distance_restriction |
Optional[str]
|
Restrict the sampling with a distance constraints. Can impose data IDs to be |
None
|
without_added_constraints |
bool
|
Option to not sample the already added constraints. Defaults to |
True
|
without_inferred_constraints |
bool
|
Option to not sample the deduced constraints from already added one. Defaults to |
True
|
**kargs |
dict
|
Other parameters that can be used in the instantiation. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if some parameters are incorrectly set. |
Source code in src\cognitivefactory\interactive_clustering\sampling\clusters_based.py
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 |
|
sample(constraints_manager, nb_to_select, clustering_result=None, vectors=None, **kargs)
¶
The main method used to sample pairs of data IDs for constraints annotation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints_manager |
AbstractConstraintsManager
|
A constraints manager over data IDs. |
required |
nb_to_select |
int
|
The number of pairs of data IDs to sample. |
required |
clustering_result |
Optional[Dict[str, int]]
|
A dictionary that represents the predicted cluster for each data ID. The keys of the dictionary represents the data IDs. If |
None
|
vectors |
Optional[Dict[str, csr_matrix]]
|
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 |
None
|
**kargs |
dict
|
Other parameters that can be used in the sampling. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if some parameters are incorrectly set or incompatible. |
Returns:
Type | Description |
---|---|
List[Tuple[str, str]]
|
List[Tuple[str,str]]: A list of couple of data IDs. |
Source code in src\cognitivefactory\interactive_clustering\sampling\clusters_based.py
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 242 243 244 245 246 247 |
|