-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from transferwise/cluster
Consider clusters of segments with similar naive averages as segment candidates in their own right
- Loading branch information
Showing
10 changed files
with
497 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
191 changes: 68 additions & 123 deletions
191
notebooks/Finding interesting segments (continuous segments).ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.preprocessing import PowerTransformer | ||
from sklearn.cluster import KMeans, kmeans_plusplus | ||
from sklearn.metrics import silhouette_score | ||
|
||
|
||
def guided_kmeans(X: np.ndarray, power_transform: bool = True) -> np.ndarray: | ||
""" | ||
Cluster segment averages to calculate aggregated segments | ||
@param X: Segment mean minus global mean, for each dimension value | ||
@param power_transform: Do we power transform before clustering | ||
@return: cluster labels and the transformed values | ||
""" | ||
if isinstance(X, pd.Series): | ||
X = X.values.reshape(-1, 1) | ||
elif isinstance(X, pd.DataFrame): | ||
X = X.values | ||
|
||
if power_transform: | ||
if len(X[X > 0] > 1): | ||
X[X > 0] = PowerTransformer(standardize=False).fit_transform(X[X > 0].reshape(-1, 1)).reshape(-1) | ||
if len(X[X < 0] > 1): | ||
X[X < 0] = -PowerTransformer(standardize=False).fit_transform(-X[X < 0].reshape(-1, 1)).reshape(-1) | ||
|
||
best_score = -1 | ||
best_labels = None | ||
best_n = -1 | ||
# If we allow 2 clusters, it almost always just splits positive vs negative - boring! | ||
for n_clusters in range(3, int(len(X) / 2) + 1): | ||
cluster_labels = KMeans(n_clusters=n_clusters, init="k-means++", n_init=10).fit_predict(X) | ||
score = silhouette_score(X, cluster_labels) | ||
# print(n_clusters, score) | ||
if score > best_score: | ||
best_score = score | ||
best_labels = cluster_labels | ||
best_n = n_clusters | ||
|
||
# print(best_n) | ||
return best_labels, X | ||
|
||
|
||
def to_matrix(labels: np.ndarray) -> np.ndarray: | ||
out = np.zeros((len(labels), len(labels.unique()))) | ||
for i in labels.unique(): | ||
out[labels == i, i] = 1.0 | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.