Thetan Sequential#

thetan.core

Thetan Sequential (TS), the one-pass primitive, and the cleanup steps that TB builds from it. Compiled with Cython. TS and TP are aliases of sequential and sequential_prelim.

thetan.core.candidate_labels_c(centroids, labels, initial_labels)#

Find duplicate centroids to delete after a TS pass.

Running TS on the centroids of a TS pass puts centroids that lie within theta of each other in the same group. In each group the centroid with the smallest index is kept and every other one becomes a candidate for deletion. Point labels are then shifted so they index the centroid array after the candidates are removed with np.delete.

Parameters:
centroidsndarray, shape (n_groups, n_features)

Centroids from TS on the centroids. Not used.

labelsndarray, shape (n_centroids,)

Group index of each first-pass centroid, as returned by TS on the centroids.

initial_labelsndarray, shape (n_samples,)

First-pass label of each point.

Returns:
final_nicendarray, shape (n_samples,)

1 if the point’s centroid is kept, 0 if it is deleted.

final_candidatesndarray, shape (n_candidates,)

Indices of the first-pass centroids to delete. Not sorted.

clusters2None

Always None. A placeholder so callers can unpack four values.

final_updated_labelsndarray, shape (n_samples,)

Label of each point after deletion. Points whose centroid is deleted get a negative value (not always -1) and need to be reassigned with reassign_uncertain_update_labels.

thetan.core.centroids_from_labels(X, indices)#

Compute the mean of the points in each cluster.

Parameters:
Xndarray, shape (n_samples, n_features)

Data, as a C-contiguous float64 array.

indicesndarray, shape (n_samples,)

Cluster label of each point. Every value must be non-negative; this is not checked.

Returns:
np.ascontiguousarray(final_centroids)ndarray, shape (max(indices) + 1, n_features)

Mean of the points with each label. A label in that range with no points gets a row of NaN.

thetan.core.reassign_uncertain_update_labels(X, centroids, labels, nice, metric='euclidean')#

Give every point with a negative label its closest centroid.

Points with a non-negative label keep it. Points with a negative label (their centroid was deleted) get the index of the nearest centroid, with no theta check.

Parameters:
Xndarray, shape (n_samples, n_features)

Data, as a C-contiguous float64 array.

centroidsndarray, shape (n_clusters, n_features)

Centroids left after deletion.

labelsndarray, shape (n_samples,)

Labels from candidate_labels_c. Negative values mark the points to reassign.

nicendarray, shape (n_samples,)

Not used.

metricstr, optional

Distance metric, any name accepted by distance_matrix. Default is ‘euclidean’. ‘dot_prod’ is the negative dot product, so the closest centroid is the one with the largest dot product.

Returns:
reassigned_indicesndarray, shape (n_samples,)

New labels with no negative values.

thetan.core.sequential(X, theta, metric='euclidean', all_out=False, verbose=False)#

Cluster data in one pass with Thetan Sequential (TS).

Parameters:
Xndarray, shape (n_samples, n_features)

Data to cluster, as a C-contiguous float64 array with at least one row.

thetafloat

Distance threshold. A point joins its nearest centroid only when the distance is strictly less than theta.

metricstr, optional

Distance metric, any name accepted by distance_matrix. Default is ‘euclidean’. ‘dot_prod’ is the negative dot product, so theta is normally negative: theta=-0.9 means a point joins a centroid when their dot product is above 0.9. The dot product also grows with the length of the vectors, so rows are usually normalised to unit length first.

all_outbool, optional

If True, also return the sum and size of each cluster. Default is False.

verbosebool, optional

If True, print the number of distance evaluations. Default is False.

Returns:
final_centroidsndarray, shape (n_clusters, n_features)

Mean of the points in each cluster.

final_indicesndarray, shape (n_samples,)

Cluster index of each point.

final_summsndarray, shape (n_clusters, n_features)

Sum of the points in each cluster. Only returned if all_out is True.

final_clusters_sizesndarray, shape (n_clusters,)

Number of points in each cluster. Only returned if all_out is True.

thetan.core.sequential_prelim(X, means_prev, theta, metric='euclidean', all_out=False)#

Run TS on data with prior centroids placed in front of it.

The prior centroids in means_prev are processed first with TS, so any that lie within theta of each other merge, and each one counts as a single sample in the running means. The points in X are then processed in order against those clusters, exactly as in sequential. This is the second pass of Thetan Berserker.

Parameters:
Xndarray, shape (n_samples, n_features)

Data to cluster, as a C-contiguous float64 array.

means_prevndarray, shape (n_prev, n_features)

Prior centroids, with at least one row and the same number of columns as X.

thetafloat

Distance threshold. A point joins its nearest centroid only when the distance is strictly less than theta.

metricstr, optional

Distance metric, any name accepted by distance_matrix. Default is ‘euclidean’. ‘dot_prod’ is the negative dot product, so theta is normally negative (see sequential).

all_outbool, optional

If True, also return the sum and size of each cluster. Default is False.

Returns:
final_centroidsndarray, shape (n_clusters, n_features)

Mean of each cluster, including the prior centroids it absorbed.

final_indicesndarray, shape (n_samples,)

Cluster index of each point in X. The prior centroids get no labels.

final_summsndarray, shape (n_clusters, n_features)

Sum of each cluster’s members, prior centroids included. Only returned if all_out is True.

final_clusters_sizesndarray, shape (n_clusters,)

Number of members in each cluster, with each prior centroid counted once. Only returned if all_out is True.