Metadata-Version: 2.4
Name: sklearn_cv_pandas
Version: 0.1.0
Summary: RandomizedSearchCV/GridSearchCV with pandas.DataFrame interface
Author: @not-so-fat
License: Copyright (c) 2020 @not-so-fat
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/not-so-fat/sklearn_cv_pandas
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas>=3
Requires-Dist: scikit-learn>=1.9
Dynamic: license-file

# sklearn-cv-pandas
RandomizedSearchCV/GridSearchCV with pandas.DataFrame interface

## Why do I want this?

- I usually prepare features as pandas.DataFrame
- Scikit learn input should be `array-like`. https://scikit-learn.org/stable/glossary.html#term-array-like.
- Although it includes pandas.DataFrame, there are some issues;
    - It does not support `Int64` data type
    - Output model does not remember which columns should be used

## Solution

- Provide GridSearchCV / RandomizedSearchCV with pandas.DataFrame interface
    - Internally preprocess DataFrame to be applicable for sklearn
- Output of `fit` command is now original `Model` object, which
    - stores column name information
    - provides pandas.DataFrame interface for prediction


## Installation

```bash
pip install sklearn_cv_pandas
```

### Development

Requires Python 3.11+ and [uv](https://docs.astral.sh/uv/).

```bash
uv sync --group dev
uv run pytest
```

## Usage

### Configure CV object

Instantiate CV in the same manner as original ones.
```
from scipy import stats
from sklearn import linear_model
from sklearn_cv_pandas import RandomizedSearchCV

estimator = linear_model.Lasso()
param_dist = dict(alpha=stats.loguniform(1e-5, 10))
cv = RandomizedSearchCV(estimator, param_dist, scoring="neg_mean_absolute_error")
```

### `fit` with pandas.DataFrame

Our CV object has new methods `fit_holdout_pandas` and `fit_cv_pandas`.
Original ones requires `x` and `y` as `numpy.array`.
Instead of numpy array, you can specify one `pandas.DataFrame` 
and column names for `x` (`feature_columns`), and column name of `y` (`target_column`).
```
model = cv.fit_cv_pandas(
    df, target_column="y", feature_columns=["x{}".format(i) for i in range(100)], n_fold=5
)
```

### `predict` with pandas.DataFrame

You can run prediction with pandas.DataFrame interface as well.
Output of `fit_holdout_pandas` and `fit_cv_pandas` stores `feature_columns` and `target_column`.
You can just input `pandas.DataFrame` for prediction into the method `predict`.

```
model.predict(df)
```
