Skip to content

Commit 53c8381

Browse files
committed
docs: update two notebooks
1 parent 09752e0 commit 53c8381

6 files changed

Lines changed: 1692 additions & 15 deletions

File tree

doc/source/autoapi/rehline/index.rst

Lines changed: 198 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Overview
1919
- ReHLine Minimization.
2020
* - :py:obj:`plqERM_Ridge <rehline.plqERM_Ridge>`
2121
- Empirical Risk Minimization (ERM) with a piecewise linear-quadratic (PLQ) objective with a ridge penalty.
22+
* - :py:obj:`plqERM_ElasticNet <rehline.plqERM_ElasticNet>`
23+
- Empirical Risk Minimization (ERM) with a piecewise linear-quadratic (PLQ) objective with a elastic net penalty.
2224
* - :py:obj:`plq_Ridge_Classifier <rehline.plq_Ridge_Classifier>`
2325
- Empirical Risk Minimization (ERM) Classifier with a Piecewise Linear-Quadratic (PLQ) loss
2426
* - :py:obj:`plq_Ridge_Regressor <rehline.plq_Ridge_Regressor>`
@@ -32,7 +34,7 @@ Overview
3234
:widths: auto
3335
:class: summarytable
3436

35-
* - :py:obj:`ReHLine_solver <rehline.ReHLine_solver>`\ (X, U, V, Tau, S, T, A, b, Lambda, Gamma, xi, max_iter, tol, shrink, verbose, trace_freq)
37+
* - :py:obj:`ReHLine_solver <rehline.ReHLine_solver>`\ (X, U, V, Tau, S, T, A, b, rho, Lambda, Gamma, xi, mu, max_iter, tol, shrink, verbose, trace_freq)
3638
- \-
3739
* - :py:obj:`plqERM_Ridge_path_sol <rehline.plqERM_Ridge_path_sol>`\ (X, y, \*None, loss, constraint, eps, n_Cs, Cs, max_iter, tol, verbose, shrink, warm_start, return_time)
3840
- Compute the PLQ Empirical Risk Minimization (ERM) path over a range of regularization parameters.
@@ -497,7 +499,161 @@ Classes
497499

498500

499501

500-
.. py:class:: plq_Ridge_Classifier(loss, constraint=[], C=1.0, U=np.empty((0, 0)), V=np.empty((0, 0)), Tau=np.empty((0, 0)), S=np.empty((0, 0)), T=np.empty((0, 0)), A=np.empty((0, 0)), b=np.empty((0, )), max_iter=1000, tol=0.0001, shrink=1, warm_start=0, verbose=0, trace_freq=100, fit_intercept=True, intercept_scaling=1.0, class_weight=None)
502+
.. py:class:: plqERM_ElasticNet(loss, constraint=[], C=1.0, l1_ratio=0.5, U=np.empty(shape=(0, 0)), V=np.empty(shape=(0, 0)), Tau=np.empty(shape=(0, 0)), S=np.empty(shape=(0, 0)), T=np.empty(shape=(0, 0)), A=np.empty(shape=(0, 0)), b=np.empty(shape=0), max_iter=1000, tol=0.0001, shrink=1, warm_start=0, verbose=0, trace_freq=100)
503+
504+
Bases: :py:obj:`rehline._base._BaseReHLine`, :py:obj:`sklearn.base.BaseEstimator`
505+
506+
Empirical Risk Minimization (ERM) with a piecewise linear-quadratic (PLQ) objective with a elastic net penalty.
507+
508+
.. math::
509+
510+
\min_{\mathbf{\beta} \in \mathbb{R}^d} C \sum_{i=1}^n \text{PLQ}(y_i, \mathbf{x}_i^T \mathbf{\beta}) + \text{l1_ratio} \| \mathbf{\beta} \|_1 + \frac{1}{2} (1 - \text{l1_ratio}) \| \mathbf{\beta} \|_2^2, \ \text{ s.t. } \
511+
\mathbf{A} \mathbf{\beta} + \mathbf{b} \geq \mathbf{0},
512+
513+
The function supports various loss functions, including:
514+
- 'hinge', 'svm' or 'SVM'
515+
- 'check' or 'quantile' or 'quantile regression' or 'QR'
516+
- 'sSVM' or 'smooth SVM' or 'smooth hinge'
517+
- 'TV'
518+
- 'huber' or 'Huber'
519+
- 'SVR' or 'svr'
520+
521+
The following constraint types are supported:
522+
* 'nonnegative' or '>=0': A non-negativity constraint.
523+
* 'fair' or 'fairness': A fairness constraint.
524+
* 'custom': A custom constraint, where the user must provide the constraint matrix 'A' and vector 'b'.
525+
526+
Parameters
527+
----------
528+
loss : dict
529+
A dictionary specifying the loss function parameters.
530+
531+
constraint : list of dict
532+
A list of dictionaries, where each dictionary represents a constraint.
533+
Each dictionary must contain a 'name' key, which specifies the type of constraint.
534+
535+
C : float, default=1.0
536+
Regularization parameter. The strength of the regularization is
537+
inversely proportional to C. Must be strictly positive.
538+
`C` will be absorbed by the ReHLine parameters when `self.make_ReLHLoss` is conducted.
539+
540+
l1_ratio : float, default=0.5
541+
The ElasticNet mixing parameter, with 0 <= l1_ratio < 1. For l1_ratio = 0 the penalty
542+
is an L2 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.
543+
544+
verbose : int, default=0
545+
Enable verbose output. Note that this setting takes advantage of a
546+
per-process runtime setting in liblinear that, if enabled, may not work
547+
properly in a multithreaded context.
548+
549+
max_iter : int, default=1000
550+
The maximum number of iterations to be run.
551+
552+
_U, _V: array of shape (L, n_samples), default=np.empty(shape=(0, 0))
553+
The parameters pertaining to the ReLU part in the loss function.
554+
555+
_Tau, _S, _T: array of shape (H, n_samples), default=np.empty(shape=(0, 0))
556+
The parameters pertaining to the ReHU part in the loss function.
557+
558+
_A: array of shape (K, n_features), default=np.empty(shape=(0, 0))
559+
The coefficient matrix in the linear constraint.
560+
561+
_b: array of shape (K, ), default=np.empty(shape=0)
562+
The intercept vector in the linear constraint.
563+
564+
Attributes
565+
----------
566+
coef\_ : array-like
567+
The optimized model coefficients.
568+
569+
n_iter\_ : int
570+
The number of iterations performed by the ReHLine solver.
571+
572+
opt_result\_ : object
573+
The optimization result object.
574+
575+
dual_obj\_ : array-like
576+
The dual objective function values.
577+
578+
primal_obj\_ : array-like
579+
The primal objective function values.
580+
581+
Methods
582+
-------
583+
fit(X, y, sample_weight=None)
584+
Fit the model based on the given training data.
585+
586+
decision_function(X)
587+
The decision function evaluated on the given dataset.
588+
589+
Notes
590+
-----
591+
The `plqERM_ElasticNet` class is a subclass of `_BaseReHLine` and `BaseEstimator`, which suggests that it is part of a larger framework for implementing ReHLine algorithms.
592+
593+
594+
595+
Overview
596+
========
597+
598+
599+
.. list-table:: Methods
600+
:header-rows: 0
601+
:widths: auto
602+
:class: summarytable
603+
604+
* - :py:obj:`fit <rehline.plqERM_ElasticNet.fit>`\ (X, y, sample_weight)
605+
- Fit the model based on the given training data.
606+
* - :py:obj:`decision_function <rehline.plqERM_ElasticNet.decision_function>`\ (X)
607+
- The decision function evaluated on the given dataset
608+
609+
610+
Members
611+
=======
612+
613+
.. py:method:: fit(X, y, sample_weight=None)
614+
615+
Fit the model based on the given training data.
616+
617+
Parameters
618+
----------
619+
620+
X: {array-like} of shape (n_samples, n_features)
621+
Training vector, where `n_samples` is the number of samples and
622+
`n_features` is the number of features.
623+
624+
y : array-like of shape (n_samples,)
625+
The target variable.
626+
627+
sample_weight : array-like of shape (n_samples,), default=None
628+
Array of weights that are assigned to individual
629+
samples. If not provided, then each sample is given unit weight.
630+
631+
Returns
632+
-------
633+
self : object
634+
An instance of the estimator.
635+
636+
637+
638+
639+
.. py:method:: decision_function(X)
640+
641+
The decision function evaluated on the given dataset
642+
643+
Parameters
644+
----------
645+
X : array-like of shape (n_samples, n_features)
646+
The data matrix.
647+
648+
Returns
649+
-------
650+
ndarray of shape (n_samples, )
651+
Returns the decision function of the samples.
652+
653+
654+
655+
656+
.. py:class:: plq_Ridge_Classifier(loss, constraint=[], C=1.0, U=np.empty((0, 0)), V=np.empty((0, 0)), Tau=np.empty((0, 0)), S=np.empty((0, 0)), T=np.empty((0, 0)), A=np.empty((0, 0)), b=np.empty((0, )), max_iter=1000, tol=0.0001, shrink=1, warm_start=0, verbose=0, trace_freq=100, fit_intercept=True, intercept_scaling=1.0, class_weight=None, multi_class=[], n_jobs=None)
501657
502658
Bases: :py:obj:`rehline._class.plqERM_Ridge`, :py:obj:`sklearn.base.ClassifierMixin`
503659

@@ -511,6 +667,7 @@ Classes
511667
- Supports optional intercept fitting (via an augmented constant feature).
512668
- Provides standard methods ``fit``, ``predict``, and ``decision_function``.
513669
- Integrates with scikit-learn ecosystem (e.g., GridSearchCV, Pipeline).
670+
- Supports multiclass classification via OvR or OvO method.
514671

515672
Parameters
516673
----------
@@ -571,17 +728,33 @@ Classes
571728
- 'balanced' uses n_samples / (n_classes * n_j).
572729
- dict maps label -> weight in the ORIGINAL label space.
573730

731+
multi_class : str or list, default=[]
732+
Method for multiclass classification. Options:
733+
- 'ovo': One-vs-One, trains K*(K-1)/2 binary classifiers.
734+
- 'ovr': One-vs-Rest, trains K binary classifiers.
735+
- [ ] or ignored when only 2 classes are present.
736+
737+
n_jobs : int or None, default=None
738+
Number of parallel jobs for multiclass fitting.
739+
None means 1 (serial). -1 means use all available CPUs.
740+
Passed directly to joblib.Parallel.
741+
742+
574743
Attributes
575744
----------
576-
coef_ : ndarray of shape (n_features,)
577-
Coefficients excluding the intercept.
745+
``coef_ ``: ndarray of shape (n_features,) for binary, (n_estimators, n_features) for multiclass
746+
Coefficients of all fitted classifiers, excluding the intercept.
578747
579-
intercept_ : float
580-
Intercept term. 0.0 if ``fit_intercept=False``.
748+
``intercept_ ``: float for binary, ndarray of shape (n_estimators,) for multiclass
749+
Intercept term(s). 0.0 if ``fit_intercept=False``.
581750

582-
classes_ : ndarray of shape (2,)
751+
classes_ : ndarray of shape (n_classes,)
583752
Unique class labels in the original label space.
584753

754+
estimators_ : list, only present for multiclass
755+
For OvR: list of (coef, intercept) tuples, length K.
756+
For OvO: list of (coef, intercept, cls_i, cls_j) tuples, length K*(K-1)/2.
757+
585758
_label_encoder : LabelEncoder
586759
Encodes original labels into {0,1} for internal training.
587760

@@ -631,20 +804,30 @@ Classes
631804
632805
Compute the decision function for samples in X.
633806

807+
For binary classification, returns a 1D array of scores.
808+
For OvR multiclass, returns a 2D array of shape (n_samples, K).
809+
For OvO multiclass, returns a 2D array of shape (n_samples, K*(K-1)/2).
810+
811+
Using coef_.T works uniformly for both binary (n_features,) and
812+
multiclass (n_estimators, n_features) shapes.
813+
634814
Parameters
635815
----------
636-
X : array-like of shape (n_samples, n_features)
816+
X : array-like of shape (n_samples, n_features)
637817
Input samples.
638818

639819
Returns
640820
-------
641-
ndarray of shape (n_samples,)
821+
ndarray of shape (n_samples,) or (n_samples, n_estimators)
642822
Continuous scores for each sample.
643823

644824

645825
.. py:method:: predict(X)
646826
647827
Predict class labels for samples in X.
828+
For binary classification, thresholds the decision score at 0.
829+
For OvR, takes the argmax across K classifiers.
830+
For OvO, uses majority voting across K*(K-1)/2 classifiers.
648831

649832
Parameters
650833
----------
@@ -722,11 +905,11 @@ Classes
722905

723906
Attributes
724907
----------
725-
coef_ : ndarray of shape (n_features,)
908+
``coef_`` : ndarray of shape (n_features,)
726909
Learned linear coefficients (excluding the intercept term).
727-
intercept_ : float
910+
``intercept_`` : float
728911
Intercept term extracted from the last coefficient when ``fit_intercept=True``, otherwise 0.0.
729-
n_features_in_ : int
912+
``n_features_in_`` : int
730913
Number of input features seen during :meth:`fit` (before intercept augmentation).
731914

732915
Notes
@@ -748,7 +931,7 @@ Classes
748931
* - :py:obj:`fit <rehline.plq_Ridge_Regressor.fit>`\ (X, y, sample_weight)
749932
- If ``fit_intercept=True``, a constant column (value = ``intercept_scaling``) is appended
750933
* - :py:obj:`decision_function <rehline.plq_Ridge_Regressor.decision_function>`\ (X)
751-
- Compute f(X) = X @ coef_ + intercept_.
934+
- Compute f(X) = X @ ``coef_`` + ``intercept_``.
752935
* - :py:obj:`predict <rehline.plq_Ridge_Regressor.predict>`\ (X)
753936
- Predict targets as the linear decision function.
754937

@@ -782,7 +965,7 @@ Classes
782965

783966
.. py:method:: decision_function(X)
784967
785-
Compute f(X) = X @ coef_ + intercept_.
968+
Compute f(X) = X @ ``coef_`` + ``intercept_``.
786969

787970
Parameters
788971
----------
@@ -1059,7 +1242,7 @@ Classes
10591242

10601243
Functions
10611244
---------
1062-
.. py:function:: ReHLine_solver(X, U, V, Tau=np.empty(shape=(0, 0)), S=np.empty(shape=(0, 0)), T=np.empty(shape=(0, 0)), A=np.empty(shape=(0, 0)), b=np.empty(shape=0), Lambda=np.empty(shape=(0, 0)), Gamma=np.empty(shape=(0, 0)), xi=np.empty(shape=(0, 0)), max_iter=1000, tol=0.0001, shrink=1, verbose=1, trace_freq=100)
1245+
.. py:function:: ReHLine_solver(X, U, V, Tau=np.empty(shape=(0, 0)), S=np.empty(shape=(0, 0)), T=np.empty(shape=(0, 0)), A=np.empty(shape=(0, 0)), b=np.empty(shape=0), rho=0.0, Lambda=np.empty(shape=(0, 0)), Gamma=np.empty(shape=(0, 0)), xi=np.empty(shape=(0, 0)), mu=np.empty(shape=(0, 0)), max_iter=1000, tol=0.0001, shrink=1, verbose=1, trace_freq=100)
10631246
10641247
.. py:function:: plqERM_Ridge_path_sol(X, y, *, loss, constraint=[], eps=0.001, n_Cs=100, Cs=None, max_iter=5000, tol=0.0001, verbose=0, shrink=1, warm_start=False, return_time=True)
10651248

doc/source/example.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Example Gallery
2020
examples/Sklearn_Mixin.ipynb
2121
examples/Multiclass_Classification.ipynb
2222
examples/NMF.ipynb
23+
examples/Smooth_SVM_new.ipynb
24+
examples/MonotonicSVM.ipynb
2325

2426
List of Examples
2527
----------------
@@ -39,3 +41,5 @@ List of Examples
3941
examples/Sklearn_Mixin.ipynb
4042
examples/Multiclass_Classification.ipynb
4143
examples/NMF.ipynb
44+
examples/Smooth_SVM_new.ipynb
45+
examples/MonotonicSVM.ipynb

0 commit comments

Comments
 (0)