Skip to content

Kmeans fit sideeffect: sample_weight is parameter is modified #17203

@mseehafer

Description

@mseehafer

Describe the bug

The array passed in to fit() or fit_predict for the argument sample weight is modified (rescaled) in the method with side effects outside the call.

Steps/Code to Reproduce

Example:

import numpy as np
from sklearn.cluster import KMeans

X = np.array([[1], [2], [4]])
sample_weights = np.array([0.5, 0.2, 0.3])
kmeans = KMeans(n_clusters=2, random_state=4321)
y_kmeans = kmeans.fit_predict(X, sample_weight=sample_weights)
print(sample_weights)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions