Talip7/scikit-learn-issues-multilabel huggingface dataset:
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
Sounds like a good resolution to me.

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
Hi if this still needs to be implemented, can I work on this? @adrinjalali @jjerphan @lorentzenchr @ogrisel

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
Hi @RudreshVeerkhare. Yes, this needs to be implemented and you can work on it !

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
/take

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
> Hi @RudreshVeerkhare. Yes, this needs to be implemented and you can work on it !

Thanks @jjerphan 😁, I've start with setup.
Will communicate regarding further doubts are suggestions.😄

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
@jjerphan I'm done with the setup. 
So before I start writing code, just want to make sure that I'm on a correct track.

Basically, I need to add functionality to allow value of p < 1 and p > 0 only when algorithm is explicitly set to "brute", and also  to raise a warning that with p < 1, Minkowski is not a valid metric. 

Is that correct? 🤔

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
Yes, based on what was concluded in discussions starting from https://github.com/scikit-learn/scikit-learn/issues/22811#issuecomment-1239351666 you are correct up to a clarification for what is to raise.

I think @lorentzenchr meant to raise an error when users set `algo="ball_tree"` or `algo="kd_tree"` in this case (and I do think this is a better approach over raising a warning).

The logic which sets `_fit_method` also needs to be adapted if `algo="auto"` is set by users in this case; it starts here:
https://github.com/scikit-learn/scikit-learn/blob/60cc5b596f38d0d236dab34e02c05d98b5a72bad/sklearn/neighbors/_base.py#L585

Labels: New Feature, module:neighbors, Needs Info
KNeighborsClassifier: allow p value for Minkowski calculation to be less than 1.0 (and greater than 0)

### Describe the workflow you want to enable

I would like to be able to use the KNeighborsClassifier with something like:

`neigh = KNeighborsClassifier(n_neighbors=2, p=0.1)`

The error you get is:

```
File ~/opt/anaconda3/envs/tensorflow-3/lib/python3.10/site-packages/sklearn/neighbors/_base.py:395, in NeighborsBase._check_algorithm_metric(self)
    392     effective_p = self.p
    394 if self.metric in ["wminkowski", "minkowski"] and effective_p < 1:
--> 395     raise ValueError("p must be greater or equal to one for minkowski metric")

ValueError: p must be greater or equal to one for minkowski metric
```

Change the above line to limit minkowski p value to < 0:

`if self.metric in ["wminkowski", "minkowski"] and effective_p < 0:`

There is nothing wrong with using a p value > 0 and < 1.

### Describe your proposed solution

Don't throw an exception if p is < 1.0.

Calculating Minkowski distance is most definitely valid for p > 0 and p < 1.  I.e.: 0.1, 0.2, 0.3, 0.4, etc

The only requirement is compute for each dimension raised to the p power. In this case **p and after the sum of all dimensions. Take the p-th root of the sum of dimensions:  `distance = distance_sum**p`

There are many cases where it is desirable to compute minkowski > 0 and < 1

### Describe alternatives you've considered, if relevant

Write my own kNN classifier with my own minkowski distance calculator:

```
def minkowski_distance(row1, row2, p=3):
    distance = 0.0
    for i in range(len(row1)-1):
        distance += abs(row1[i] - row2[i])**p
    return distance**(1/p)
```

### Additional context

_No response_

COMMENT:
Thanks @jjerphan for the clarification, I'll start working on it, and will create a WIP PR. 
Will discuss further on it...

Labels: New Feature, module:neighbors, Needs Info
Allow other initialization strategies for IterativeImputer

### Describe the workflow you want to enable

I want to initialize an `IterativeImputer` using a `KNNImputer` rather than a `SimpleImputer`.

### Describe your proposed solution

Modifying `IterativeImputer` to accept instances of `_BaseImputer` as `initial_strategy`s should be fairly simple, though I'm aware it is under active development and I understand why this wouldn't be a priority. I'm happy to put together a pull request if it is likely to be accepted.

### Describe alternatives you've considered, if relevant

I am not aware of any compelling alternatives. 

### Additional context

_No response_

COMMENT:
I don't think that this is a good idea in practice.

I will refer to https://github.com/scikit-learn/scikit-learn/issues/21967 and the work of @aperezlebel

Combining `IterativeImputer` with `KNNImputer` will not scale since already both imputers do not scale well in terms of computation time. `KNNImputer` is also intractable with a large number of samples.

So before considering this feature, I would request a good benchmark to show that using a `KNNImputer` as initialization is actually useful in practice.

Labels: New Feature, module:impute, Needs Decision - Include Feature
Conda install code not showing in the docs

### Describe the bug

On the [installation page](https://scikit-learn.org/stable/install.html), when selecting the `conda` package and any operating system, the install code does not render.

**Tested Browsers**
* Chrome Version 99.0.4844.51 (Official Build) (x86_64)
* Safari Version 15.0 (16612.1.29.41.4, 16612)
<img width="1040" alt="image" src="https://user-images.githubusercontent.com/4793561/157894121-be2ecb54-591e-490e-9035-39e72626dd09.png">


### Steps/Code to Reproduce

1. Go to https://scikit-learn.org/stable/install.html 
2. In Packager click `conda`
3. Look at the `Then run:` code snippet

### Expected Results

To see code equivalent to `pip3 install -U scikit-learn` but for conda 

### Actual Results

An area for holding code but with no text in it

### Versions

```shell
Latest docs
```

COMMENT:
Thanks for the report. Doesn't work with firefox as well.

Labels: Bug, Documentation
Conda install code not showing in the docs

### Describe the bug

On the [installation page](https://scikit-learn.org/stable/install.html), when selecting the `conda` package and any operating system, the install code does not render.

**Tested Browsers**
* Chrome Version 99.0.4844.51 (Official Build) (x86_64)
* Safari Version 15.0 (16612.1.29.41.4, 16612)
<img width="1040" alt="image" src="https://user-images.githubusercontent.com/4793561/157894121-be2ecb54-591e-490e-9035-39e72626dd09.png">


### Steps/Code to Reproduce

1. Go to https://scikit-learn.org/stable/install.html 
2. In Packager click `conda`
3. Look at the `Then run:` code snippet

### Expected Results

To see code equivalent to `pip3 install -U scikit-learn` but for conda 

### Actual Results

An area for holding code but with no text in it

### Versions

```shell
Latest docs
```

COMMENT:
/take

Labels: Bug, Documentation
RFC introduce methods to get and set estimators' state

Right now `clone` uses `{get, set}_params` to replicate an unfit estimator. These methods are designed to return esimators' hyperparameters. At the moment, we have no way of getting the state of a fitted estimator in a non-pickle format.

Pickle files are by design able to run arbitrary code, and therefore one should ideally only load a pickle file from a trusted source. This makes sharing and moving scikit-learn based estimators hard, which also introduces security issues when deploying ML models in production.

Another issue with pickle files is that we kinda force people to use the same versions of the libraries they used to train the model and dump the pickle. This prevents people from being able to update their base docker images when they're deploying a model which was trained a while ago, and I'm not sure if we have good ways of letting them update their pickle files for a new version.

My proposal is to introduce `{get, set}_state` methods on the `BaseEstimator` level be able to persist and set the state of models in a more portable, secure, and backward compatible way. We can probably even just do JSON.

cc @scikit-learn/core-devs @koaning

COMMENT:
There's a similar need for the callbacks API. Essentially, once fitted, all you need to save are the attributes (init params, private and fitted). Sometimes not all of them are necessary for predict, transform, ...
Then creating an instance of the estimator and setting all these attributes will be as if the estimator was fitted.

Labels: API, RFC
RFC introduce methods to get and set estimators' state

Right now `clone` uses `{get, set}_params` to replicate an unfit estimator. These methods are designed to return esimators' hyperparameters. At the moment, we have no way of getting the state of a fitted estimator in a non-pickle format.

Pickle files are by design able to run arbitrary code, and therefore one should ideally only load a pickle file from a trusted source. This makes sharing and moving scikit-learn based estimators hard, which also introduces security issues when deploying ML models in production.

Another issue with pickle files is that we kinda force people to use the same versions of the libraries they used to train the model and dump the pickle. This prevents people from being able to update their base docker images when they're deploying a model which was trained a while ago, and I'm not sure if we have good ways of letting them update their pickle files for a new version.

My proposal is to introduce `{get, set}_state` methods on the `BaseEstimator` level be able to persist and set the state of models in a more portable, secure, and backward compatible way. We can probably even just do JSON.

cc @scikit-learn/core-devs @koaning

COMMENT:
I applaud the effort! 

Might be good to discuss a downside though. There's an upper limit with what might be achieved via this route. I fear that things like the [FunctionTransformer](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.FunctionTransformer.html#:~:text=A%20FunctionTransformer%20forwards%20its%20X,%2C%20doing%20custom%20scaling%2C%20etc.) will never serialize nicely because we won't ever be able to jsonify a Python function.

Labels: API, RFC
RFC introduce methods to get and set estimators' state

Right now `clone` uses `{get, set}_params` to replicate an unfit estimator. These methods are designed to return esimators' hyperparameters. At the moment, we have no way of getting the state of a fitted estimator in a non-pickle format.

Pickle files are by design able to run arbitrary code, and therefore one should ideally only load a pickle file from a trusted source. This makes sharing and moving scikit-learn based estimators hard, which also introduces security issues when deploying ML models in production.

Another issue with pickle files is that we kinda force people to use the same versions of the libraries they used to train the model and dump the pickle. This prevents people from being able to update their base docker images when they're deploying a model which was trained a while ago, and I'm not sure if we have good ways of letting them update their pickle files for a new version.

My proposal is to introduce `{get, set}_state` methods on the `BaseEstimator` level be able to persist and set the state of models in a more portable, secure, and backward compatible way. We can probably even just do JSON.

cc @scikit-learn/core-devs @koaning

COMMENT:
Yes, I'd be happy if those methods raise (even an `AttributeError`) on `FunctionTransformer`.

Labels: API, RFC