New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cookbook: multiclass logistic regression #3244
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
doc/cookbook/source/examples/classifier/multiclass_logisticregression.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
=============================== | ||
Multi-class Logistic Regression | ||
=============================== | ||
|
||
Multinomial logistic regression assigns the sample :math:`\mathbf{x}_i` to class :math:`c` | ||
based on the probability for sample :math:`\mathbf{x}_i` to be in class :math:`c`: | ||
|
||
.. math:: | ||
|
||
P(Y_i = c | \mathbf{x}_i) = \frac{\exp(\mathbf{\theta}^\top_c\mathbf{x}_i)}{1+ \sum_{k=1}^{K}\exp(\mathbf{\theta}^\top_k\mathbf{x}_i)} | ||
|
||
in which :math:`K` is the number of classes. | ||
|
||
The loss function that needs to be minimized is: | ||
|
||
.. math:: | ||
|
||
{\min_{\mathbf{\theta}}}\sum_{k=1}^{K}\sum_{i=1}^{m}w_{ik}\log(1+\exp(-y_{ik}(\mathbf{x}_k^\top\mathbf{a}_{ik} + c_k))) + \lambda\left \| \mathbf{x} \right \|_{l_1/l_q} | ||
|
||
where :math:`\mathbf{a}_{ik}` denotes the :math:`i`-th sample for the :math:`k`-th class, :math:`w_{ik}` is the weight for :math:`\mathbf{a}_{ik}^\top`, | ||
:math:`y_{ik}` is the response of :math:`\mathbf{a}_{ik}`, and :math:`c_k` is the intercept (scalar) for the :math:`k`-th class. | ||
:math:`\lambda` is the :math:`l_1/l_q`-norm regularization parameter. | ||
|
||
------- | ||
Example | ||
------- | ||
|
||
Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as | ||
|
||
.. sgexample:: multiclass_logisticregression.sg:create_features | ||
|
||
We create an instance of the :sgclass:`CMulticlassLogisticRegression` classifier by passing it the dataset, lables, and specifying the regularization constant :math:`\lambda` for each machine | ||
|
||
.. sgexample:: multiclass_logisticregression.sg:create_instance | ||
|
||
Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. | ||
|
||
.. sgexample:: multiclass_logisticregression.sg:train_and_apply | ||
|
||
We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. | ||
|
||
.. sgexample:: multiclass_logisticregression.sg:evaluate_accuracy | ||
|
||
---------- | ||
References | ||
---------- | ||
|
||
:wiki:`Multinomial_logistic_regression` | ||
|
||
:wiki:`Multiclass_classification` |
29 changes: 29 additions & 0 deletions
29
examples/meta/src/classifier/multiclass_logisticregression.sg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
CSVFile f_feats_train("../../data/classifier_4class_2d_linear_features_train.dat") | ||
CSVFile f_feats_test("../../data/classifier_4class_2d_linear_features_test.dat") | ||
CSVFile f_labels_train("../../data/classifier_4class_2d_linear_labels_train.dat") | ||
CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat") | ||
|
||
#![create_features] | ||
RealFeatures features_train(f_feats_train) | ||
RealFeatures features_test(f_feats_test) | ||
MulticlassLabels labels_train(f_labels_train) | ||
MulticlassLabels labels_test(f_labels_test) | ||
#![create_features] | ||
|
||
|
||
#![create_instance] | ||
MulticlassLogisticRegression classifier(1, features_train, labels_train) | ||
#![create_instance] | ||
|
||
#![train_and_apply] | ||
classifier.train() | ||
MulticlassLabels labels_predict = classifier.apply_multiclass(features_test) | ||
#![train_and_apply] | ||
|
||
#![evaluate_accuracy] | ||
MulticlassAccuracy eval() | ||
real accuracy = eval.evaluate(labels_predict, labels_test) | ||
#![evaluate_accuracy] | ||
|
||
# integration testing variables | ||
RealVector output = labels_predict.get_labels() |
40 changes: 0 additions & 40 deletions
40
examples/undocumented/python_modular/classifier_multiclasslogisticregression_modular.py
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, this is much better now. It can stay like that once you cleared up these minor things