Skip to content

'cross_val_predict' throws error when estimator is 'OneVsRest Classifier' and method is 'decision_function' #11058

Closed

Description

@vivek531

Description

'cross_val_predict' function throws error when estimator is 'OneVsRest Classifier with LinearSVC' and method is 'decision_function'.

Steps/Code to Reproduce

from sklearn.model_selection import cross_val_predict
from sklearn import svm
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
import numpy as np

x_train = np.random.rand(1000,10)
class_labels_train = np.random.randint(5, size=(1000,1))

mlb = MultiLabelBinarizer()
class_labels_train = mlb.fit_transform(class_labels_train)

svm_est = svm.LinearSVC(C=1.0, penalty='l1', random_state=456, tol=0.000001, dual=False, class_weight='balanced')
svm_est = OneVsRestClassifier(svm_est)

cv_results = cross_val_predict(svm_est, x_train, class_labels_train, method='decision_function')

Expected Results

Actual Results

Versions

Darwin-17.4.0-x86_64-i386-64bit
('Python', '2.7.10 (default, Jul 15 2017, 17:16:57) \n[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]')
('NumPy', '1.14.1')
('SciPy', '1.0.0')
('Scikit-Learn', '0.19.1')

Activity

  1. agramfort commented on May 3, 2018

    @agramfort
    Member

    please fill up the section: Steps/Code to Reproduce

    thanks

  2. princejha95 commented on May 3, 2018

    @princejha95

    Can I work on this issue if no one is assigned to it ?

  3. vivek531 commented on May 3, 2018

    @vivek531
    Author

    Added Steps/Code to Reproduce

  4. jaebird123 commented on Oct 9, 2018

    @jaebird123

    I have a similar issue with the predict_proba method. Running the following

    import numpy as np
    from sklearn.ensemble import GradientBoostingClassifier
    from sklearn.model_selection import cross_val_predict
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.preprocessing import MultiLabelBinarizer
    
    X = np.random.rand(1000, 10)
    y = np.random.randint(5, size = (1000, 1))
    
    y = MultiLabelBinarizer().fit_transform(y) # cross_val_predict succeeds with this removed
    
    clf = OneVsRestClassifier(GradientBoostingClassifier())
    
    yprob = cross_val_predict(clf, X, y, method = 'predict_proba')
    print(yprob[:5, :])

    gives this error

    Traceback (most recent call last):
      File "example.py", line 17, in <module>
        yprob = cross_val_predict(clf, X, y, method = 'predict_proba')
      File "/usr/local/lib/python3.7/site-packages/sklearn/model_selection/_validation.py", line 723, in cross_val_predict
        y = le.fit_transform(y)
      File "/usr/local/lib/python3.7/site-packages/sklearn/preprocessing/label.py", line 235, in fit_transform
        y = column_or_1d(y, warn=True)
      File "/usr/local/lib/python3.7/site-packages/sklearn/utils/validation.py", line 783, in column_or_1d
        raise ValueError("bad input shape {0}".format(shape))
    ValueError: bad input shape (1000, 5)
    

    I think cross_val_predict should probably not throw the ValueError: bad input shape if y is 2D and the estimator is a OneVsRestClassifer.

  5. amueller commented on Oct 9, 2018

    @amueller
    Member

    this is fixed by #8773 (which is not merged yet) right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions