Giter Club home page Giter Club logo

Comments (1)

northern-64bit avatar northern-64bit commented on June 2, 2024

Hello @graceleetr !

You can wrap LinearSVC in a class that implements predict_proba() by calibrating the decision function outputs to probabilities. This involves using CalibratedClassifierCV from scikit-learn, which can calibrate the decision scores to probabilities. Here's how you can do it:

from sklearn.svm import LinearSVC
from sklearn.calibration import CalibratedClassifierCV
from skmultilearn.problem_transform import BinaryRelevance
from sklearn.datasets import make_multilabel_classification
from sklearn.model_selection import train_test_split


X, y = make_multilabel_classification(n_samples=1000, n_features=20, n_classes=5, n_labels=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize the LinearSVC classifier
base_clf = LinearSVC()

# Calibrate the classifier
calibrated_clf = CalibratedClassifierCV(base_clf)

# Use the calibrated classifier with for instance BinaryRelevance from scikit-multilearn
classifier = BinaryRelevance(classifier=calibrated_clf)

# Train the classifier
classifier.fit(X_train, y_train)

# Now you can use predict_proba
probabilities = classifier.predict_proba(X_test)

print(probabilities)

Note that the calibration may add some training time and complexity.

Also note that this may not always be normalized probabilities so this you may have to fix with:

import numpy as np

# Convert the sparse matrix to a dense format for manipulation
prob_dense = probabilities.toarray()

# Normalize the probabilities row-wise
prob_normalized = prob_dense / np.sum(prob_dense, axis=1, keepdims=True)

# Check the sum again to ensure they are normalized
prob_sum_normalized = prob_normalized.sum(axis=1)

prob_normalized, prob_sum_normalized[:5]

I hope that this helps!

from scikit-multilearn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.