-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider clusters of segments with similar naive averages as segment candidates in their own right #23
Merged
+497
−295
Merged
Consider clusters of segments with similar naive averages as segment candidates in their own right #23
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d379775
Pre=pull commit
EgorKraevTransferwise b0debad
Pre=pull commit
EgorKraevTransferwise 4092fc1
Merge branch 'main' of https://github.com/transferwise/wise-pizza
EgorKraevTransferwise 7300020
Switch to segment generation logic better suited to clustered segment…
EgorKraevTransferwise d9496e5
Use groups of segments with similar naive averages as additional cand…
EgorKraevTransferwise 51ddd47
Introduce aliases for composite segments, and a property relevant_clu…
EgorKraevTransferwise 445509b
Address AlxdrPolyakov's comments
EgorKraevTransferwise 8400ec3
added relevant_cluster_names plot, added info to the readme
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 | ||
# If we allow 2 clusters, it almost always just splits positive vs negative - boring! | ||
for n_clusters in range(3, 10): | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlikely that it will cause problems, but we define best_n only in the if statement, I suggest to add best_n = None before "for" loop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do |
||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I forgot something, but why don't we standardize here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we don't want to mix up the positive and the negative values, we want to transform both separately.