Skip to main content

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

pip install sklearn_cv_pandas

Development

Requires Python 3.11+ and uv.

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)

Metadata

Release files for sklearn_cv_pandas 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sklearn_cv_pandas 0.1.0
File Size Uploaded
sklearn_cv_pandas-0.1.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sklearn_cv_pandas 0.1.0
File Interpreter ABI Platform
sklearn_cv_pandas-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / sklearn_cv_pandas-0.1.0.tar.gz

Download URL sklearn_cv_pandas-0.1.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
9579c9a408a885f247cc39dc0270b9b80d5cb32e245c66676bffa160a348f699
BLAKE2b-256 checksum
How to use checksums
ef7def7a4ccf53fab1b58327f4bc394759f616d4a8bc1308cca2db24167f45b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sklearn_cv_pandas-0.1.0-py3-none-any.whl

Download URL sklearn_cv_pandas-0.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6db3a474036b7ac1eae8285f40b29bfe8d0772e2085826919e283893cacfb153
BLAKE2b-256 checksum
How to use checksums
3ad2b35d2f6132fc3b2064c4a6f92b4835e88425f27e42791ef61c1c82ef871d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page