Skip to content

calling type_of_target crashes before returning unknown #9777

@massich

Description

@massich

Description

calling type_of_target(y) with y being a sparse representation of a multiclass-multioutput variable crashes.

Steps/Code to Reproduce

import numpy as np
from sklearn.utils.multiclass import type_of_target
from scipy import sparse

xx = sparse.rand(10,3, density=.3)
xx = (xx*10).astype(int)
yy = (xx>0).astype(int)

data = {'xx':xx.toarray(), 'xx_sparse':xx, 'yy':yy.toarray(), 'yy_sparse':yy}

for k, v in data.items():
    print('{} -> {}'.format(k, type_of_target(v)))

Expected Results

$python /tmp/test_typeoftarget.py 
xx -> multiclass-multioutput
xx_sparse -> unknown
yy -> multilabel-indicator
yy_sparse -> multilabel-indicator

Actual Results

$ python /tmp/test_typeoftarget.py 
xx -> multiclass-multioutput
Traceback (most recent call last):
  File "/tmp/test_typeoftarget.py", line 12, in <module>
    print('{} -> {}'.format(k, type_of_target(v)))
  File "/home/sik/miniconda3/lib/python3.6/site-packages/sklearn/utils/multiclass.py", line 267, in type_of_target
    if y.ndim > 2 or (y.dtype == object and len(y) and
TypeError: len() of unsized object

####Notes
#9059 solves it, but I need to double check that its actually properly solved / tested. Because this should had been captured by some test already.

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