(X, labels, probabilities=None, parameters=None, ground_truth=False, ax=None)
| 25 | |
| 26 | |
| 27 | def plot(X, labels, probabilities=None, parameters=None, ground_truth=False, ax=None): |
| 28 | if ax is None: |
| 29 | _, ax = plt.subplots(figsize=(10, 4)) |
| 30 | labels = labels if labels is not None else np.ones(X.shape[0]) |
| 31 | probabilities = probabilities if probabilities is not None else np.ones(X.shape[0]) |
| 32 | # Black removed and is used for noise instead. |
| 33 | unique_labels = set(labels) |
| 34 | colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))] |
| 35 | # The probability of a point belonging to its labeled cluster determines |
| 36 | # the size of its marker |
| 37 | proba_map = {idx: probabilities[idx] for idx in range(len(labels))} |
| 38 | for k, col in zip(unique_labels, colors): |
| 39 | if k == -1: |
| 40 | # Black used for noise. |
| 41 | col = [0, 0, 0, 1] |
| 42 | |
| 43 | class_index = (labels == k).nonzero()[0] |
| 44 | for ci in class_index: |
| 45 | ax.plot( |
| 46 | X[ci, 0], |
| 47 | X[ci, 1], |
| 48 | "x" if k == -1 else "o", |
| 49 | markerfacecolor=tuple(col), |
| 50 | markeredgecolor="k", |
| 51 | markersize=4 if k == -1 else 1 + 5 * proba_map[ci], |
| 52 | ) |
| 53 | n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) |
| 54 | preamble = "True" if ground_truth else "Estimated" |
| 55 | title = f"{preamble} number of clusters: {n_clusters_}" |
| 56 | if parameters is not None: |
| 57 | parameters_str = ", ".join(f"{k}={v}" for k, v in parameters.items()) |
| 58 | title += f" | {parameters_str}" |
| 59 | ax.set_title(title) |
| 60 | plt.tight_layout() |
| 61 | |
| 62 | |
| 63 | # %% |
no test coverage detected
searching dependent graphs…