Comments (1)
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)
- MLkNN breaks when sklearn >= 1.0 HOT 3
- Random state parameter doomed to fail in IterativeStratification HOT 3
- how meka classifier can use predict_proba?
- IterativeStratification use in medical and some datasets ValueError: Only one class present in y_true. ROC AUC score is not defined in that case HOT 1
- Proposal for small bug resolval in MEKA wrapper HOT 1
- smaller support in function "_get_most_desired_combination"
- Upgrade to tensorflow 2
- ditch python 2
- Some tests are failing
- model_selection/iterative_stratification.py throws AttributeError: 'numpy.ndarray' object has no attribute 'iloc' HOT 2
- Next release to Pypi HOT 9
- Shuffle data in `iterative_train_test_split` beforehand (with solution) HOT 8
- how to implement kernal trick in MLTSVM
- fit() method for ChainClassifier() doesn't save classifiers in self.classifiers_ dictionary
- Incorrect link on authors page in the docs
- refer to igraph instead of python-igraph in docs
- `skmultilearn.dataset` missing `arff` dependency
- Performance on the MEKA wrapper
- iterative_train_test_split assumes dataframe when numpy array is allowable HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scikit-multilearn.