Documentation › Python › scikit-learn classifier
import numpy as np
from libsdca.sklearn import SDCAClassifier
rng = np.random.default_rng(0)
X = rng.normal(size=(300, 20))
y = np.array(["cat", "dog", "emu"])[(X @ rng.normal(size=(20, 3))).argmax(axis=1)]
clf = SDCAClassifier("topk_cross_entropy", k=2, C=1.0).fit(X, y)
print(clf.predict(X[:5]), clf.score(X, y))SDCAClassifier(loss="cross_entropy", *, C=1.0, k=None, variant=None, gamma=None,
tol=1e-3, max_iter=1000, eval_epoch=10, shrinking="auto",
kernel="linear", precision=None, n_jobs=None, random_state=None,
warm_start=False)
SDCAClassifier is a scikit-learn estimator for the single-label losses. It trains with
libsdca.fit and accepts any sortable labels, such as strings.
| Parameter | Default | Meaning |
|---|---|---|
loss |
"cross_entropy" |
The loss name: "cross_entropy", "topk_cross_entropy", "multiclass_hinge" or "topk_hinge". |
C |
1.0 |
The loss weight, positive. |
k |
None |
The number of top classes. The top-k losses require it. |
variant |
None |
The variant of "topk_hinge", "beta" or "alpha". None means "beta". |
gamma |
None |
The smoothing of the hinges. None means 0, the nonsmooth hinge. |
tol |
1e-3 |
The relative duality gap at which the fit stops as solved. |
max_iter |
1000 |
The largest number of epochs. |
eval_epoch |
10 |
The number of epochs between evaluations of the duality gap. 0 evaluates only at the end. |
shrinking |
"auto" |
Speeds up hinge fits (shrinking). |
kernel |
"linear" |
"linear" for features. With "precomputed", fit takes the Gram matrix of the training examples, and the prediction methods take the kernel values between the test and the training examples. |
precision |
None |
The working precision: None, "float32" or "float64" (Precision). |
n_jobs |
None |
The number of threads of the evaluations. None uses the default of libsdca.fit, and -1 uses all CPUs. |
random_state |
None |
The order of the examples in the epochs: an integer, a RandomState or None, as in scikit-learn. |
warm_start |
False |
Start a refit from the solution of the previous fit. |
Pass k, variant and gamma only to a loss that takes them. A parameter of another loss
raises TypeError in fit, as for the loss objects.
| Attribute | Meaning |
|---|---|
classes_ |
The class labels, in the order of the score columns. |
coef_ |
The weights, of shape (n_classes, n_features). A fit with two classes has the shape (1, n_features), the weights of the margin. A precomputed kernel has no coef_. |
dual_coef_ |
The dual variables, of shape (n_classes, n_samples). |
n_features_in_ |
The number of features. |
n_iter_ |
The number of epochs that the fit ran. |
status_ |
The status of the solver. Only "solved" guarantees the duality gap. |
history_ |
The evaluations of the duality gap, as Model.history. |
model_ |
The Model that libsdca.fit returned. |
| Method | Returns |
|---|---|
fit(X, y) |
The fitted estimator. |
predict(X) |
The label of the class with the largest score. |
decision_function(X) |
The scores, of shape (n_samples, n_classes). With two classes it returns the margins, of shape (n_samples,). |
predict_proba(X), predict_log_proba(X) |
The softmax of the scores and its logarithm. Available with loss="cross_entropy" only. |
score(X, y) |
The mean accuracy, as every scikit-learn classifier. |
- No intercept. Append a constant feature for a regularized intercept.
- Convergence. If the solver stops before it reaches
tol,fitstill returns the fitted estimator and issues scikit-learn'sConvergenceWarning. Inspectstatus_andhistory_for the reason and the final gap. - Warm start. A refit reuses the dual variables when the loss, the shapes, the kernel mode and the training labels agree with the previous fit. Otherwise it starts a fresh solve.
- No sample or class weights. The estimator takes no
sample_weightand noclass_weight. - Multilabel losses are not available in the estimator. Train them with
libsdca.fit. - scikit-learn tools. The estimator works with
Pipeline,clone, cross-validation and grid search. It passes scikit-learn'scheck_estimator, except for the checks that supply invalid Gram matrices.