Talip7/scikit-learn-issues-multilabel huggingface dataset:
GrideSearchCV() has Issue with LSSVM () classification

### Describe the workflow you want to enable

Hi, 
I tried to use `GrideSearchCV()` with `LSSVM()` but could not do that , please could you help ?
 The code : 
The code which i used is from Github romolo code.
[https://github.com/RomuloDrumond/LSSVM](https://www.researchgate.net/deref/https%3A%2F%2Fgithub.com%2FRomuloDrumond%2FLSSVM?_tp=eyJjb250ZXh0Ijp7ImZpcnN0UGFnZSI6Il9kaXJlY3QiLCJwYWdlIjoiX2RpcmVjdCJ9fQ)

```python
lssvc = LSSVC(gamma=1, kernel='rbf', sigma=.5) # Class instantiation
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

Then tried to do `GridesearchCV()` :

```python
parameters = {'kernel':('rbf'),
'gamma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0],
'sigma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}
lssvm = LSSVC()
LSSVC= GridSearchCV(lssvm, parameters)
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

There are other implementaion for LSSVM but non of them worked with GrideSearchCV() since not in the original sklearn ?

### Describe your proposed solution

Adding LSSVM to GridesearchCV() 

### Describe alternatives you've considered, if relevant

No alternatives 

### Additional context

_No response_

COMMENT:
Seems like `LSSVC` estimator does not have `score` method. `GrideSearchCV` works only with estimators that have it.

Labels: New Feature, Needs Triage
GrideSearchCV() has Issue with LSSVM () classification

### Describe the workflow you want to enable

Hi, 
I tried to use `GrideSearchCV()` with `LSSVM()` but could not do that , please could you help ?
 The code : 
The code which i used is from Github romolo code.
[https://github.com/RomuloDrumond/LSSVM](https://www.researchgate.net/deref/https%3A%2F%2Fgithub.com%2FRomuloDrumond%2FLSSVM?_tp=eyJjb250ZXh0Ijp7ImZpcnN0UGFnZSI6Il9kaXJlY3QiLCJwYWdlIjoiX2RpcmVjdCJ9fQ)

```python
lssvc = LSSVC(gamma=1, kernel='rbf', sigma=.5) # Class instantiation
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

Then tried to do `GridesearchCV()` :

```python
parameters = {'kernel':('rbf'),
'gamma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0],
'sigma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}
lssvm = LSSVC()
LSSVC= GridSearchCV(lssvm, parameters)
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

There are other implementaion for LSSVM but non of them worked with GrideSearchCV() since not in the original sklearn ?

### Describe your proposed solution

Adding LSSVM to GridesearchCV() 

### Describe alternatives you've considered, if relevant

No alternatives 

### Additional context

_No response_

COMMENT:
yes.

but , where is the scoring method in  SVM, sklearn !!!
https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/svm/_classes.py

This  code 
 def score_samples(self, X):
        """Raw scoring function of the samples.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data matrix.

        Returns
        -------
        score_samples : ndarray of shape (n_samples,)
            Returns the (unshifted) scoring function of the samples.
        """
        return self.decision_function(X) + self.offset_

Labels: New Feature, Needs Triage
GrideSearchCV() has Issue with LSSVM () classification

### Describe the workflow you want to enable

Hi, 
I tried to use `GrideSearchCV()` with `LSSVM()` but could not do that , please could you help ?
 The code : 
The code which i used is from Github romolo code.
[https://github.com/RomuloDrumond/LSSVM](https://www.researchgate.net/deref/https%3A%2F%2Fgithub.com%2FRomuloDrumond%2FLSSVM?_tp=eyJjb250ZXh0Ijp7ImZpcnN0UGFnZSI6Il9kaXJlY3QiLCJwYWdlIjoiX2RpcmVjdCJ9fQ)

```python
lssvc = LSSVC(gamma=1, kernel='rbf', sigma=.5) # Class instantiation
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

Then tried to do `GridesearchCV()` :

```python
parameters = {'kernel':('rbf'),
'gamma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0],
'sigma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}
lssvm = LSSVC()
LSSVC= GridSearchCV(lssvm, parameters)
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

There are other implementaion for LSSVM but non of them worked with GrideSearchCV() since not in the original sklearn ?

### Describe your proposed solution

Adding LSSVM to GridesearchCV() 

### Describe alternatives you've considered, if relevant

No alternatives 

### Additional context

_No response_

COMMENT:
If you want this class to work with sklearn environment, it has to implement mentioned method, and there is nothing much can be done on the side of sklearn. To make it work you can create a class that will inherit from LSSVC and implement score method, using methods of LSSVC

Labels: New Feature, Needs Triage
GrideSearchCV() has Issue with LSSVM () classification

### Describe the workflow you want to enable

Hi, 
I tried to use `GrideSearchCV()` with `LSSVM()` but could not do that , please could you help ?
 The code : 
The code which i used is from Github romolo code.
[https://github.com/RomuloDrumond/LSSVM](https://www.researchgate.net/deref/https%3A%2F%2Fgithub.com%2FRomuloDrumond%2FLSSVM?_tp=eyJjb250ZXh0Ijp7ImZpcnN0UGFnZSI6Il9kaXJlY3QiLCJwYWdlIjoiX2RpcmVjdCJ9fQ)

```python
lssvc = LSSVC(gamma=1, kernel='rbf', sigma=.5) # Class instantiation
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

Then tried to do `GridesearchCV()` :

```python
parameters = {'kernel':('rbf'),
'gamma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0],
'sigma':[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]}
lssvm = LSSVC()
LSSVC= GridSearchCV(lssvm, parameters)
lssvc.fit(X_tr_norm, y_train) # Fitting the model
y_pred = lssvc.predict(X_ts_norm) # Making predictions with the trained model
acc = accuracy_score(dummie2multilabel(y_test), dummie2multilabel(y_pred)) # Calculate Accuracy
print('acc_test = ', acc, '\n')
```

There are other implementaion for LSSVM but non of them worked with GrideSearchCV() since not in the original sklearn ?

### Describe your proposed solution

Adding LSSVM to GridesearchCV() 

### Describe alternatives you've considered, if relevant

No alternatives 

### Additional context

_No response_

COMMENT:
@Navien2 `LSSVC` implementation is just not compatible with scikit-learn.
They have to fix it on their side.

Labels: New Feature, Needs Triage
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
I don't know if we should consider modifying the documentation or consider this as a bug. We usually never modify `X` in-place because it would be to much surprising for our user.

I am not sure of what is best if we want to fix it. Making a check that the diagonal as explicit zeros will be to costly and making a copy will be less memory efficient (we do a copy of `X`). For sure the simplest fix would be to make the copy.

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
Thank you @glemaitre. I should also point out that 
* Optics [modifies](https://github.com/scikit-learn/scikit-learn/blob/d99b728b3a7952b2111cf5e0cb5d14f92c6f3a80/sklearn/cluster/_optics.py#L335-L340C23) the sparse matrix in-place.

* HDBSCAN  [modifies](https://github.com/scikit-learn/scikit-learn/blob/d99b728b3a7952b2111cf5e0cb5d14f92c6f3a80/sklearn/cluster/_hdbscan/hdbscan.py#L248-L252) the distance matrix when computing the mutual reachability graph. 


For DBSCAN, would the fix involve just adding `X = X.copy()` before `X.setdiag(X.diagonal())`?

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
Adding that this behavior was causing issues as subsequent steps were transforming the matrix, and were very surprised to see that running HDBSCAN was modifying the input object

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
Yep, usually this is not the contract of scikit-learn to modify the passed matrices.

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
Thank you @glemaitre. Would you prefer a single PR that addresses this issue in DBSCAN, HDBSCAN and Optics?

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
I would prefer 3 PRs such that the changes are minimal to review.

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
- [x] DBSCAN #27651
- [ ] HDBSCAN
- [x] Optics

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
I'm working on HDBSCAN, but I'm not sure if it would be a good idea to copy `distance_matrix` one more time since this variable is already a copy of `X`. What is your opinion? @lorentzenchr @glemaitre

Labels: Bug, Documentation, help wanted
Mention that DBSCAN might modify precomputed sparse distance matrix

### Describe the issue linked to the documentation

[DBSCAN](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html) provides a parameter called `metric` which can be assigned the value `precomputed` so that a precomputed distance matrix can be passed to the `fit` method. 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN.

When a sparse matrix is passed to DBSCAN, it is modified [in-place.](https://github.com/scikit-learn/scikit-learn/blob/2d8e03f4d5b3c466fb4542360bcef742a3a4e0a1/sklearn/cluster/_dbscan.py#L384-L389). If the sparse matrix already had diagonal elements present, this leads to no change in the matrix. However, if the diagonal elements are not present, then extra elements are added to the sparse matrix. In the documentation, this should be mentioned explicitly. 

### Suggest a potential alternative/fix

For the `metric` param - 

> If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a [sparse graph](https://scikit-learn.org/stable/glossary.html#term-sparse-graph), in which case only “nonzero” elements may be considered neighbors for DBSCAN. **Note that DBSCAN modifies the sparse matrix in-place by setting the diagonal of the sparse matrix.**

COMMENT:
In HDBSCAN we don't need to force a copy here indeed because this is already controlled by the ``copy`` parameter:
```
If copy=True then any time an in-place modifications would be made that
would overwrite data passed to [fit](https://scikit-learn.org/dev/glossary.html#term-fit),
a copy will first be made, guaranteeing that the original data will be unchanged.
Currently, it only applies when metric="precomputed", when passing a dense array
or a CSR sparse matrix and when algorithm="brute".
```

With that in mind I'm closing this issue as resolved

Labels: Bug, Documentation, help wanted