libmultilabel/linear/linear.py
def train_1vsrest(y: sparse.csr_matrix, x: sparse.csr_matrix, options: str):
    """
    Trains a linear model for multiabel data using a one-vs-all strategy.

    Returns the model.

    y is a 0/1 matrix with dimensions number of instances * number of classes.
    x is a matrix with dimensions number of instances * number of features.
    options is the option string passed to liblinear.
    """
    if options.find('-R') != -1:
        raise ValueError('-R is not supported')

    bias = -1.
    if options.find('-B') != -1:
        options_split = options.split()
        i = options_split.index('-B')
        bias = float(options_split[i+1])
        options = ' '.join(options_split[:i] + options_split[i+2:])
        x = sparse.hstack([
            x,
            np.full((x.shape[0], 1), bias),
        ], 'csr')

    y = y.tocsc()
    nr_class = y.shape[1]
    nr_feature = x.shape[1]
    weights = np.zeros((nr_feature, nr_class), order='F')
    for i in range(nr_class):
        yi = y[:, i].toarray().reshape(-1)
        modeli = train(yi, x, options)
        w = np.ctypeslib.as_array(modeli.w, (nr_feature,))
        # liblinear label mapping depends on data, we ensure
        # it is the same for all labels
        if modeli.get_labels()[0] == 0:
            w = -w
        weights[:, i] = w

    return {'weights': np.asmatrix(weights), '-B': bias}

    Trains a linear model for multiabel data using a one-vs-all strategy.

    Returns the model.

    y is a 0/1 matrix with dimensions number of instances * number of classes.
    x is a matrix with dimensions number of instances * number of features.
    options is the option string passed to liblinear.
    
Trains a linear model for multiabel data using a one-vs-all strategy.
Returns the model.

y is a 0/1 matrix with dimensions number of instances * number of classes.
x is a matrix with dimensions number of instances * number of features.
options is the option string passed to liblinear.
Trains a linear model for multiabel data using a one - vs - all strategy . Returns the model . y is a 0 / 1 matrix with dimensions number of instances * number of classes . x is a matrix with dimensions number of instances * number of features . options is the option string passed to liblinear .
def train_1vsrest(y: sparse.csr_matrix, x: sparse.csr_matrix, options: str):
    if options.find('-R') != -1:
        raise ValueError('-R is not supported')
    bias = -1.
    if options.find('-B') != -1:
        options_split = options.split()
        i = options_split.index('-B')
        bias = float(options_split[i+1])
        options = ' '.join(options_split[:i] + options_split[i+2:])
        x = sparse.hstack([
            x,
            np.full((x.shape[0], 1), bias),
        ], 'csr')
    y = y.tocsc()
    nr_class = y.shape[1]
    nr_feature = x.shape[1]
    weights = np.zeros((nr_feature, nr_class), order='F')
    for i in range(nr_class):
        yi = y[:, i].toarray().reshape(-1)
        modeli = train(yi, x, options)
        w = np.ctypeslib.as_array(modeli.w, (nr_feature,))
        if modeli.get_labels()[0] == 0:
            w = -w
        weights[:, i] = w
    return {'weights': np.asmatrix(weights), '-B': bias}
def train_1vsrest ( y : sparse . csr_matrix , x : sparse . csr_matrix , options : str ) : if options . find ( '-R' ) != - 1 : raise ValueError ( '-R is not supported' ) bias = - 1. if options . find ( '-B' ) != - 1 : options_split = options . split ( ) i = options_split . index ( '-B' ) bias = float ( options_split [ i + 1 ] ) options = ' ' . join ( options_split [ : i ] + options_split [ i + 2 : ] ) x = sparse . hstack ( [ x , np . full ( ( x . shape [ 0 ] , 1 ) , bias ) , ] , 'csr' ) y = y . tocsc ( ) nr_class = y . shape [ 1 ] nr_feature = x . shape [ 1 ] weights = np . zeros ( ( nr_feature , nr_class ) , order = 'F' ) for i in range ( nr_class ) : yi = y [ : , i ] . toarray ( ) . reshape ( - 1 ) modeli = train ( yi , x , options ) w = np . ctypeslib . as_array ( modeli . w , ( nr_feature , ) ) if modeli . get_labels ( ) [ 0 ] == 0 : w = - w weights [ : , i ] = w return { 'weights' : np . asmatrix ( weights ) , '-B' : bias }
Trains a linear model for multiabel data using a one-vs-all strategy.
Trains a linear model for multiabel data using a one - vs - all strategy .
"""
    Trains a linear model for multiabel data using a one-vs-all strategy.

    Returns the model.

    y is a 0/1 matrix with dimensions number of instances * number of classes.
    x is a matrix with dimensions number of instances * number of features.
    options is the option string passed to liblinear.
    """ # liblinear label mapping depends on data, we ensure # it is the same for all labels
y
sparse.csr_matrix
x
sparse.csr_matrix
options
str