Skip to content
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

add **metrickw to KMedoids for the convinence of the metric like 'min… #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions sklearn_extra/cluster/_k_medoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ def __init__(
init="heuristic",
max_iter=300,
random_state=None,
**metrickw
):
self.n_clusters = n_clusters
self.metric = metric
self.method = method
self.init = init
self.max_iter = max_iter
self.random_state = random_state
self.metrickw=metrickw

def _check_nonnegative_int(self, value, desc, strict=True):
"""Validates if value is a valid integer > 0"""
Expand Down Expand Up @@ -235,7 +237,7 @@ def fit(self, X, y=None):
% (self.n_clusters, X.shape[0])
)

D = pairwise_distances(X, metric=self.metric)
D = pairwise_distances(X, metric=self.metric,**self.metrickwm)

medoid_idxs = self._initialize_medoids(
D, self.n_clusters, random_state_, X
Expand Down Expand Up @@ -379,10 +381,10 @@ def transform(self, X):
check_is_fitted(self, "cluster_centers_")

Y = self.cluster_centers_
kwargs = {}

if self.metric == "seuclidean":
kwargs["V"] = np.var(np.vstack([X, Y]), axis=0, ddof=1)
DXY = pairwise_distances(X, Y=Y, metric=self.metric, **kwargs)
DXY = pairwise_distances(X, Y=Y, metric=self.metric, **self.metrickw)

return DXY

Expand Down Expand Up @@ -421,7 +423,7 @@ def predict(self, X):
X,
Y=self.cluster_centers_,
metric=self.metric,
metric_kwargs=kwargs,
metric_kwargs=self.metrickw,
)

return pd_argmin
Expand Down