8000 Type error thrown when handing over an options dictionary to gridsearchs fit method · Issue #15852 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

Type error thrown when handing over an options dictionary to gridsearchs fit method #15852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stepodev opened this issue Dec 10, 2019 · 1 comment

Comments

@stepodev
Copy link

Description

Type error thrown when handing over an options dictionary to gridsearchs fit method like so:

grid_search.fit(train[["A", "B"]], train["C"], **fit_options)

This worked for 0.21.3, but doesn't for 0.22.0, so this might be a regression. Behavior change was introduced here: https://github.com/scikit-learn/scikit-learn/blame/master/sklearn/model_selection/_search.py#L651

Steps/Code to Reproduce

from sklearn.linear_model import HuberRegressor
from sklearn.model_selection import GridSearchCV
import pandas as pd

train = pd.DataFrame({"A": [1, 2, 3, 5, 2], "B": [4, 5, 6, 4, 1], "C": [5, 7, 9, 9, 3]})

hyper_parameter_tune = {
    "epsilon": [1, 1.5]
}

fit_options = {
    "sample_weight": 1
}

gridsearch_params = {
    "cv": 2
}

gsearch = GridSearchCV(param_grid=hyper_parameter_tune,
                       estimator=HuberRegressor(),
                       **gridsearch_params
                       )

gsearch.fit(train[["A", "B"]], train["C"], **fit_options)

observation = pd.DataFrame({"A": [5], "B": [2]})

print(gsearch.predict(observation))

Expected Results

No Error thrown: TypeError: Singleton array array(1) cannot be considered a valid collection

Actual Results

Traceback (most recent call last):
File "/home/stepo/PycharmProjects/untitled/test.py", line 24, in
gsearch.fit(train[["A", "B"]], train["C"], groups=None, **fit_options)
File "/home/stepo/PycharmProjects/untitled/venv/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 652, in fit
fit_params_values = indexable(*fit_params.values())
File "/home/stepo/PycharmProjects/untitled/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 237, in indexable
check_consistent_length(*result)
File "/home/stepo/PycharmProjects/untitled/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 208, in check_consistent_length
lengths = [_num_samples(X) for X in arrays if X is not None]
File "/home/stepo/PycharmProjects/untitled/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 208, in
lengths = [_num_samples(X) for X in arrays if X is not None]
File "/home/stepo/PycharmProjects/untitled/venv/lib/python3.7/site-packages/sklearn/utils/validation.py", line 152, in _num_samples
" a valid collection." % x)
TypeError: Singleton array array(1) cannot be considered a valid collection.

Versions

System:
python: 3.7.5 (default, Nov 20 2019, 09:21:52) [GCC 9.2.1 20191008]
executable: /home/stepo/PycharmProjects/untitled/venv/bin/python
machine: Linux-5.3.0-24-generic-x86_64-with-Ubuntu-19.10-eoan

Python dependencies:
pip: 19.0.3
setuptools: 40.8.0
sklearn: 0.22
numpy: 1.17.4
scipy: 1.3.3
Cython: None
pandas: 0.25.3
matplotlib: None
joblib: 0.14.1

Built with OpenMP: True

@jnothman
Copy link
Member

Duplicate of #15805

@jnothman jnothman marked this as a duplicate of #15805 Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0