How can I check if a regressor/classifier in sklearn supports Multi-Target output ?
Sklearn documentation says the following about MultiOutputRegressor :
This strategy consists of fitting one regressor per target. This is a simple strategy for extending regressors that do not natively support multi-target regression.
Related
With scikit-learn pipeline we can visualize our pipeline construct. See below screenshot.
I couldn't find similar plotting feature for a sklearn stacking classifier. How can I represent the ensemble model construct with sklearn stacking classifier?
Just like voting classifier, StackingClassifier too could be added as a component of the model pipeline as shown below:
I have recently developed a scikit-learn estimator (a classifier) and I am now wanting to add sample_weight to the estimator. The reason is so I could apply boosting (ie. Adaboost) to the estimator (as Adaboost requires sample_weight to be present in the estimator).
I had a look at a few different scikit-learn estimators such as linear regression, logistic regression and SVM, but they all seem to have a different way of adding sample_weight into their estimators and it's not very clear to me:
Linear regression:
https://github.com/scikit-learn/scikit-learn/blob/95d4f0841/sklearn/linear_model/_base.py#L375
Logistic regression:
https://github.com/scikit-learn/scikit-learn/blob/95d4f0841/sklearn/linear_model/_logistic.py#L1459
SVM:
https://github.com/scikit-learn/scikit-learn/blob/95d4f0841d57e8b5f6b2a570312e9d832e69debc/sklearn/svm/_base.py#L796
So I am confused now and wanting to know how do I add sample_weight into my estimator? Is there a standard way of doing this in scikit-learn or it just depends on the estimator? Any templates or any examples would really be appreciated. Many thanks in advance.
This might be a stupid question but I was just wondering what the difference between ML-KNN implemented in scikit.ml and scikit-learn's KNeighborsClassifier is. According to sklearn's docs KNeighborsClassifier has support for multilabel classification. ML-KNN however is KNN adapted for multilabel classification built on top of sklearn's architecture based on it's docs.
When searching for sample multilabel problems, MLkNN mostly appears but I do not understand if there's any advantage of using it over the base implementation of sklearn if it already supports it. Is it only a late adaptation in sklearn's side or are there more differences in the implementation?
Any input is appreciated. Thanks!
scikit-multilearn's ML-KNN implementations is an improved version of scikit-learn's KNeighborsClassifier. It is actually built on top of it. After the k nearest neighbors in the training data are found, it uses maximum a posteriori principle to label a new instance to achieve a better performance. Also, since it operates on sparse matrices internally using SciPy sparse matrix library, it is highly memory-efficient. More info here and here.
There is a bit of a dilemma with sklearn.
Using SVC I can use the method predict_proba to calculate how likely a prediction is.
Using SGDClassifier I can perform "online/incremental" learning using the method partial_fit method but predict_proba doesn't work with the 'hinge' loss.
Is there anyway to have both?
As someone asked about that before #github, the authors then added mentioning of this to the docs.
So your doc-entry to go for is sklearn's Probability calibration.
A basic example was given in the github link as:
from sklearn.calibration import CalibratedClassifierCV
from sklearn.linear_model import SGDClassifier
clf = SGDClassifier(loss='hinge')
calibrated_clf = CalibratedClassifierCV(clf, cv=5, method='sigmoid')
calibrated_clf.fit(X, y)
But this probably won't work for partial_fit as mentioned here.
So for your task it's important to know if the following works for you:
no, you can't partial_fit the calibration, but you can partial_fit the
underlying classifier
I thought it should be the same, but for method decision_function() I get different results. And SVC with only decision_function_shape='ovr' is really faster.
Related: Scikit learn multi-class classification for support vector machines
I got some clarification on the documentation of LinearSVC in the See also heading, where SVC is mentioned.
SVC
Implementation of Support Vector Machine classifier using libsvm:
....
....
Furthermore SVC multi-class mode is implemented using one vs one scheme while LinearSVC uses one
vs the rest. It is possible to implement one vs the rest with SVC by
using the sklearn.multiclass.OneVsRestClassifier wrapper.
....
Also, SVC delegates all the training to the underlying libsvm library, which handles the multi-class case as 'OvO' (even if the decision_function_shape = 'ovr').
Its mentioned in the issue #delusionX mentioned that decision_function_shape is just for compatibility with scikit API. Its most probably, that all other estimators handle the multi-class as OvR and so when SVC is used in combination with other things, (Like for example in a Pipeline, GridSearchCV, Or wrappers like OneVsRestClassifier) returning a OvO decision function breaks the working of others. But I could not find that written explicitly anywhere.
Fun fact: OneVsOneClassifier also returns a decision function which confirms with the shape of OvR.