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
add cookbook page for svr #3227
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
48 changes: 48 additions & 0 deletions
48
doc/cookbook/source/examples/regression/support_vector_regression.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,48 @@ | ||
========================= | ||
Support Vector Regression | ||
========================= | ||
|
||
Support vector regression is a regression model inspired from support vector machines. The solution can be written as: | ||
|
||
.. math:: | ||
f({\bf x})=\sum_{i=1}^{N} \alpha_i k({\bf x}, {\bf x}_i)+b | ||
|
||
where :math:`{\bf x}` is the new data point, :math:`{\bf x}_i` is a training sample, :math:`N` denotes number of training samples, :math:`k` is a kernel function, :math:`\alpha` and :math:`b` are determined in training. | ||
|
||
See :cite:`scholkopf2002learning` for a more detailed introduction. :sgclass:`LibSVR` performs support vector regression using LibSVM :cite:`chang2011libsvm`. | ||
|
||
------- | ||
Example | ||
------- | ||
|
||
Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as | ||
|
||
.. sgexample:: support_vector_regression.sg:create_features | ||
|
||
Choose an appropriate :sgclass:`CKernel` and instantiate it. Here we use a :sgclass:`CGaussianKernel`. | ||
|
||
.. sgexample:: support_vector_regression.sg:create_appropriate_kernel | ||
|
||
We create an instance of :sgclass:`CLibSVR` classifier by passing it the kernel, labels, solver type and some more parameters. More solver types are available in :sgclass:`CLibSVR`. See :cite:`chang2002training` for more details. | ||
|
||
.. sgexample:: support_vector_regression.sg:create_instance | ||
|
||
Then we train the regression model and apply it to test data to get the predicted :sgclass:`CRegressionLabels`. | ||
|
||
.. sgexample:: support_vector_regression.sg:train_and_apply | ||
|
||
After training, we can extract :math:`\alpha`. | ||
|
||
.. sgexample:: support_vector_regression.sg:extract_alpha | ||
|
||
Finally, we can evaluate the :sgclass:`CMeanSquaredError`. | ||
|
||
.. sgexample:: support_vector_regression.sg:evaluate_error | ||
|
||
---------- | ||
References | ||
---------- | ||
:wiki:`Support_vector_machine` | ||
|
||
.. bibliography:: ../../references.bib | ||
:filter: docname in docnames |
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
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,39 @@ | ||
CSVFile f_feats_train("../../data/regression_1d_sinc_features_train.dat") | ||
CSVFile f_feats_test("../../data/regression_1d_sinc_features_test.dat") | ||
CSVFile f_labels_train("../../data/regression_1d_sinc_labels_train.dat") | ||
CSVFile f_labels_test("../../data/regression_1d_sinc_labels_test.dat") | ||
|
||
#![create_features] | ||
RealFeatures features_train(f_feats_train) | ||
RealFeatures features_test(f_feats_test) | ||
RegressionLabels labels_train(f_labels_train) | ||
RegressionLabels labels_test(f_labels_test) | ||
#![create_features] | ||
|
||
#![create_appropriate_kernel] | ||
real width = 1 | ||
GaussianKernel kernel(width) | ||
#![create_appropriate_kernel] | ||
|
||
#![create_instance] | ||
real svm_c = 1 | ||
real svr_param = 0.1 | ||
LibSVR svr(svm_c, svr_param, kernel, labels_train, enum LIBSVR_SOLVER_TYPE.LIBSVR_EPSILON_SVR) | ||
#![create_instance] | ||
|
||
#![train_and_apply] | ||
svr.train(features_train) | ||
RegressionLabels labels_predict = svr.apply_regression(features_test) | ||
#![train_and_apply] | ||
|
||
#![extract_alpha] | ||
RealVector alpha = svr.get_alphas() | ||
#![extract_alpha] | ||
|
||
#![evaluate_error] | ||
MeanSquaredError eval() | ||
real mse = eval.evaluate(labels_predict, labels_test) | ||
#![evaluate_error] | ||
|
||
# integration testing variables | ||
RealVector output = labels_test.get_labels() |
35 changes: 0 additions & 35 deletions
35
examples/undocumented/csharp_modular/regression_libsvr_modular.cs
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
examples/undocumented/java_modular/regression_libsvr_modular.java
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
examples/undocumented/python_modular/regression_libsvr_modular.py
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
examples/undocumented/ruby_modular/regression_libsvr_modular.rb
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.
Another good reference here is the book "Learning with kernels" By Schölkopf and Smola