2016-06-09 1 views
3

MultiLabelBinarizer을 인스턴스화 한 후에는 다른 곳에서 구현 한 행렬에 대해 inverse_transform 메서드가 필요합니다. 불행하게도,sklearn - 즉시 MultiLabelBinarizer의 inverse_transform을 호출 할 수 없습니다.

import numpy as np 
from sklearn.preprocessing import MultiLabelBinarizer 
mlb = MultiLabelBinarizer(classes=['a', 'b', 'c']) 

A = np.array([[1, 0, 0], [1, 0, 1], [0, 1, 0], [1, 1, 1]]) 
y = mlb.inverse_transform(A) 

AttributeError: 'MultiLabelBinarizer' object has no attribute 'classes_'

은 내가 mlb의 instanciation 후이 라인을 추가하는 경우

mlb.fit_transform([(c,) for c in ['a', 'b', 'c']]) 

오류가 사라진 것으로 나타났습니다 산출한다. 나는 fit_transformclasses_ 속성의 값을 설정했기 때문에 이것을 추측하고 있습니다.하지만 classes 매개 변수를 제공하고 있기 때문에 인스턴스에서 완료 될 것으로 기대합니다.

저는 sklearn 버전 0.17.1과 python 2.7.6을 사용하고 있습니다. 내가 잘못하고 있니?

답변

3

, 당신은 또한이 같은 빠른 해킹을 할 수 classes_ 속성 또한 scikit-learn.org http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html의 설명서는 메서드 fitMultiLabelBinarizer의 인스턴스를 반환 할 수 있음을 명시 적으로 지정합니다.

def fit(self, y): 
    """Fit the label sets binarizer, storing `classes_` 
    Parameters 
    ---------- 
    y : iterable of iterables 
     A set of labels (any orderable and hashable object) for each 
     sample. If the `classes` parameter is set, `y` will not be 
     iterated. 
    Returns 
    ------- 
    self : returns this MultiLabelBinarizer instance 
    """ 
1

https://github.com/scikit-learn/scikit-learn/blob/51a765a/sklearn/preprocessing/label.py#L636으로 구현 된 것으로 보이며, .fit은 classes_ 속성을 정의하는 유일한 방법입니다. classes_는 생성자의 클래스 사본으로 정의되지 않았으며 주석에 제공된 정의를 고려할 때 클래스와 같은 의미는 아닙니다. 당신은 저자에게 경고 할 수 있습니다. 같은 marmouset 말했다 때문에 단지 fitfit_transorm 맞는 것 같다,

mlb = MultiLabelBinarizer().fit(['a', 'b', 'c']) 

: 당신이 MultiLabelBinarizer의 인스턴스 내에서 속성 classes_을 설정하려면

class MultiLabelBinarizer(BaseEstimator, TransformerMixin): 
    """Transform between iterable of iterables and a multilabel format 
    Although a list of sets or tuples is a very intuitive format for multilabel 
    data, it is unwieldy to process. This transformer converts between this 
    intuitive format and the supported multilabel format: a (samples x classes) 
    binary matrix indicating the presence of a class label. 
    Parameters 
    ---------- 
    classes : array-like of shape [n_classes] (optional) 
     Indicates an ordering for the class labels 
    sparse_output : boolean (default: False), 
     Set to true if output binary array is desired in CSR sparse format 
    Attributes 
    ---------- 
    classes_ : array of labels 
     A copy of the `classes` parameter where provided, 
     or otherwise, the sorted set of classes found when fitting. 
관련 문제