Repository navigation
Closed
Labels
Description
Describe the bug
When using sklearn.cluster.HDBSCAN with metric="precomputed", the input distance matrix is modified after calling fit_predict(). The original hdbscan package (v0.8.40) works correctly.
Steps/Code to Reproduce
import numpy as np
from sklearn.cluster import HDBSCAN
rmsd_matrix = np.random.rand(5, 5)
rmsd_matrix = (rmsd_matrix + rmsd_matrix.T) / 2
np.fill_diagonal(rmsd_matrix, 0)
print("Before HDBSCAN:")
print(rmsd_matrix)
hdb = HDBSCAN(metric="precomputed", min_cluster_size=2)
hdb.fit_predict(rmsd_matrix)
print("\nAfter HDBSCAN:")
print(rmsd_matrix) # Matrix is changed!Expected Results
Input matrix should remain unchanged (as in original hdbscan).
Actual Results
Input matrix is changed
Versions
System:
python: 3.12.3 (main, Jun 18 2025, 17:59:45) [GCC 13.3.0]
executable: /home/username/project/bin/python3
machine: Linux-6.14.0-27-generic-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.7.0
pip: 24.0
setuptools: 80.9.0
numpy: 2.2.6
scipy: 1.16.0
Cython: None
pandas: 2.3.0
matplotlib: 3.10.3
joblib: 1.5.1
threadpoolctl: 3.6.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: openblas
num_threads: 18
prefix: libscipy_openblas
filepath: /home/username/project/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so
version: 0.3.29
threading_layer: pthreads
architecture: Haswell
user_api: blas
internal_api: openblas
num_threads: 18
prefix: libscipy_openblas
filepath: /home/username/project/lib/python3.12/site-packages/scipy.libs/libscipy_openblas-68440149.so
version: 0.3.28
threading_layer: pthreads
architecture: Haswell
user_api: openmp
internal_api: openmp
num_threads: 18
prefix: libgomp
filepath: /home/username/project/lib/python3.12/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
version: None
Activity
NandiniV-20 commentedon Aug 11, 2025
Sarthakpurii commentedon Aug 11, 2025
I’m currently working on this issue.
I have confirmed that when passing a dense precomputed distance matrix to HDBSCAN, scikit-learn’s implementation mutates the matrix in place. This should not happen as the input pairwise distance matrix should remain unchanged after fitting.
Earlier, I suspected that it was due to
force_writeable=Truebut maybe that's not the case. I'm currently investigating the issue further.adrinjalali commentedon Aug 12, 2025
You can control this by setting
copy=True, but default, we avoid copies and it means the original data might be changed. Same is true for a lot of other estimators in scikit-learn. This code works as you expect:Gives:
@NandiniV-20 @Sarthakpurii please make sure you only contribute where the issue is confirmed and maintainers have agreed on a solution. Otherwise we might need to block you from the org.
Sarthakpurii commentedon Aug 12, 2025
@adrinjalali Please don't be so harsh. I just wanted to give back to the community.
I have used scikit-learn a lot, and I did some research as well yesterday. According to that, be it scikit-learn or any other ML library, the input data should not be mutated/altered given to their
fit()ortransform()methods by default as it breaks the reproducibility and predictability.Also, I did come through that the required functioning can be delivered by setting
copy=Truebut that's not the default case here (I understand to avoid copies default is False, making the algorithm memory efficient). This is the reason I handledcopyto set to beTrueif the given metric is dense precomputed distance metrics; otherwise false. Another idea was to setcopy=Trueby default but that would not have been the memory efficient solution because ifhdbscan_bruteis used asmst_funcfor metrics other than 'precomputed', it would be creating possible extra copies which is not required.Would love to discuss this a bit further or get your views on this.
NandiniV-20 commentedon Aug 12, 2025
adrinjalali commentedon Aug 12, 2025
@Sarthakpurii now that you're investigating, could you please have a look at other estimators where we have a
copyarg and see how the default behavior there is, and if there's any other case of precomputed input which could be a similar case. I'd be happy to have this fixed if it's not consistent with other places.Sarthakpurii commentedon Aug 13, 2025
@adrinjalali I investigated other estimators with the
copyparameter. Every single one defaults tocopy=True. Here is the list of all the estimators for your convenience to quickly go through -FactorAnalysis,PLSSVD,_PLS,AffinityPropagation,IncrementalPCA,PCA,SimpleImputer,OrthogonalMatchingPursuitCV,KNNImputer& all normalizers/scalers.HDBSCANis the only estimator havingcopy=False.Earlier, I was hesitant to set HDBSCAN's default
copy=Truebecause I was concerned about the performance and memory overhead and scikit-learn's broader design patterns. This is the reason I handled thecopyto beTruejust for dense precomputed distance metrics in my recent PR, but now I feel setting defaultcopy=Truewould be the better choice to maintain consistency. I would love to raise another PR correcting the same if you allow.What are your thoughts on this?
Sarthakpurii commentedon Aug 15, 2025
@adrinjalali I am waiting for your views on this.
adrinjalali commentedon Aug 19, 2025
Then I think we can have a PR changing the default value, w/o deprecation cycle, and have it as a FIX. I wonder what @Micky774 thinks about it though.
Micky774 commentedon Aug 19, 2025
It's worth making it consistent with other estimators if it is currently off. I think it might be worth a deprecation cycle though, since it could meaningfully break some workflows that perhaps already assumed that a copy was being made, and hence wouldn't immediately know how to correct for the change. Thank you for bringing this to our attention @Sarthakpurii and @tolyanchik5 :)
Sarthakpurii commentedon Aug 19, 2025
@Micky774 @adrinjalali I went through the standards on how scikit-learn implements a deprecation cycle in
contributing.rst. If we are going forward with a deprecation cycle, we can make default value ofcopy="warn"and raise a warning that the default value ofcopyis going to change from False to True in version x, if user uses the default value of copy. Additionally, making some minor changes to_parameter_constraints,etc to maintain consistency for this change.Does this works?
Micky774 commentedon Aug 19, 2025
That sounds good to me. Feel free to ping me for review on that PR
Sarthakpurii commentedon Aug 19, 2025
Which version should I mention for when this change will go live?
adrinjalali commentedon Aug 19, 2025
That would be 1.10