Skip to content

An error is thrown when using Random forest classifier multi-target non-numeric classes #11451

Closed

Description

@mitar

Description

Random forest classifier can be fit on multiple target columns where columns have string labels and are not numeric, but when trying to predict, an error is thrown.

Steps/Code to Reproduce

import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble.forest import RandomForestClassifier

c = RandomForestClassifier()

X, y = make_classification()
y = np.array(['foo' if v else 'bar' for v in y]).reshape((y.shape[0], 1))
ys = np.hstack([y, y])

c.fit(X, ys)
c.predict(X)

Expected Results

No error is thrown.

Actual Results

  File ".../local/lib/python2.7/site-packages/sklearn/ensemble/forest.py", line 550, in predict
    axis=0)
ValueError: could not convert string to float: foo

Versions

Linux-4.13.0-45-generic-x86_64-with-Ubuntu-17.10-artful
('Python', '2.7.14 (default, Sep 23 2017, 22:06:14) \n[GCC 7.2.0]')
('NumPy', '1.14.5')
('SciPy', '1.1.0')
('Scikit-Learn', '0.19.1')

Activity

  1. mitar commented on Jul 6, 2018

    @mitar
    ContributorAuthor

    It seems the following code fixes the problem:

            if self.n_outputs_ == 1:
                return self.classes_.take(np.argmax(proba, axis=1), axis=0)
    
            else:
                n_samples = proba[0].shape[0]
                predictions = []
    
                for k in range(self.n_outputs_):
                    predictions.append(self.classes_[k].take(np.argmax(proba[k], axis=1), axis=0))
    
                return np.array(predictions).T
  2. jnothman commented on Jul 7, 2018

    @jnothman
    Member
  3. mitar commented on Jul 7, 2018

    @mitar
    ContributorAuthor

    I mean, the code above looks like you are trying to support. Otherwise there would be no self.classes_[k].take there, no?

    I can make a pull request. Is the example solution OK? Going through a list and not pre-allocate?

  4. jnothman commented on Jul 7, 2018

    @jnothman
    Member
  5. added a commit that references this issue on Jul 8, 2018
    e215890
  6. mitar commented on Jul 8, 2018

    @mitar
    ContributorAuthor

    Pull request with tests made: #11458

  7. mitar commented on Jul 8, 2018

    @mitar
    ContributorAuthor

    While fixing this, I searched a code a bit for similar (bad) pattern and found out that trees also do not work. So I fixed them as well.

  8. added a commit that references this issue on Feb 16, 2019
    2ee5ddc
  9. added a commit that references this issue on Feb 19, 2019
    95993a4
  10. added a commit that references this issue on Apr 28, 2019
    f95ffe6
  11. added a commit that references this issue on Jul 12, 2019
    2643b48
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