... | ... | @@ -2,128 +2,77 @@ |
|
|
|
|
|
An implementation of Least Angle Regression (Stagewise/laSso), also known as LARS.
|
|
|
|
|
|
```prolog
|
|
|
:- use_module('path/to/.../src/methods/lars/lars.pl').
|
|
|
|
|
|
%% usage example
|
|
|
TrainData = [5.1,3.5,1.4, 4.9,3.0,1.4, 4.7,3.2,1.3, 4.6,3.1,1.5],
|
|
|
TestData = [3,2,0, 5,1,4, 0,0,4, 3,3,5, 0,5,5, 2,5,5],
|
|
|
lars_initAndTrainModel(0, 0.0, 0.0, 1.0e-16, TrainData, 3, [0,1,0,1], 0, _),
|
|
|
lars_predict(TestData, 3, PredictList, 1).
|
|
|
```
|
|
|
|
|
|
# Available Predicates
|
|
|
|
|
|
* [initModelNoDataNoGram/4](/PrologMethods/Regression/lars#initmodelnodatanogram4)
|
|
|
* [initModelNoDataWithGram/7](/PrologMethods/Regression/lars#initmodelnodatawithgram7)
|
|
|
* [initModelWithDataNoGram/10](/PrologMethods/Regression/lars#initmodelwithdatanogram10)
|
|
|
* [initModelWithDataWithGram/13](/PrologMethods/Regression/lars#initmodelwithdatawithgram13)
|
|
|
* [activeSet/2](/PrologMethods/Regression/lars#activeset2)
|
|
|
* [beta/2](/PrologMethods/Regression/lars#beta2)
|
|
|
* [betaPath/3](/PrologMethods/Regression/lars#betapath3)
|
|
|
* [computeError/7](/PrologMethods/Regression/lars#computeerror7)
|
|
|
* [lambdaPath/2](/PrologMethods/Regression/lars#lambdapath2)
|
|
|
* [matUtriCholFactor/3](/PrologMethods/Regression/lars#matutricholfactor3)
|
|
|
* [predict/6](/PrologMethods/Regression/lars#predict6)
|
|
|
* [train/9](/PrologMethods/Regression/lars#train9)
|
|
|
* [lars_initAndTrainModel/9](/PrologMethods/Regression/lars#lars_initandtrainmodel9)
|
|
|
* [lars_activeSet/2](/PrologMethods/Regression/lars#lars_activeset1)
|
|
|
* [lars_beta/2](/PrologMethods/Regression/lars#lars_beta1)
|
|
|
* [lars_betaPath/3](/PrologMethods/Regression/lars#lars_betapath2)
|
|
|
* [lars_computeError/7](/PrologMethods/Regression/lars#lars_computeerror5)
|
|
|
* [lars_lambdaPath/2](/PrologMethods/Regression/lars#lars_lambdapath1)
|
|
|
* [lars_matUtriCholFactor/3](/PrologMethods/Regression/lars#lars_matutricholfactor2)
|
|
|
* [lars_predict/6](/PrologMethods/Regression/lars#lars_predict4)
|
|
|
|
|
|
---
|
|
|
|
|
|
[links/resources](/PrologMethods/Regression/lars#connected-linksresources)
|
|
|
|
|
|
## **_initModelNoDataNoGram/4_**
|
|
|
## **_lars_initAndTrainModel/9_**
|
|
|
|
|
|
Only initialize the LARS model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelNoDataNoGram( +integer,
|
|
|
+float32, +float32, +float32).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| useCholesky | +integer(bool) | Whether or not to use Cholesky decomposition when solving linear system (as opposed to using the full Gram matrix). | (0)false |
|
|
|
| lambda1 | +float | Regularization parameter for l1-norm penalty. | 0.0 |
|
|
|
| lambda2 | +float | Regularization parameter for l2-norm penalty. | 0.0 |
|
|
|
| tolerance | +float | Run until the maximum correlation of elements in (X^T y) is less than this. | 1e-16 |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_initModelNoDataWithGram/7_**
|
|
|
|
|
|
Initialize LARS model, and pass in a precalculated Gram matrix but dont train the model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelNoDataWithGram( +integer,
|
|
|
%% predicate definition
|
|
|
lars_initAndTrainModel(UseCholesky, Lambda1, Lambda2, Tolerance, DataList, DataPointsDim, ResponsesList, RowMajor, Error) :-
|
|
|
Tolerance > 0,
|
|
|
convert_list_to_float_array(DataList, DataPointsDim, array(Xsize, Xrownum, X)),
|
|
|
convert_list_to_float_array(ResponsesList, array(Ysize, Y)),
|
|
|
initAndTrainModelI(UseCholesky, Lambda1, Lambda2, Tolerance, X, Xsize, Xrownum, Y, Ysize, RowMajor, Error).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(initAndTrainModel, c, initAndTrainModelI( +integer,
|
|
|
+float32, +float32, +float32,
|
|
|
+pointer(float_array), +integer, +integer,
|
|
|
+float32, +float32, +float32).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| useCholesky | +integer(bool) | Whether or not to use Cholesky decomposition when solving linear system (as opposed to using the full Gram matrix). | (0)false |
|
|
|
| gramMatrix | +matrix | Gram matrix. | - |
|
|
|
| lambda1 | +float | Regularization parameter for l1-norm penalty. | 0.0 |
|
|
|
| lambda2 | +float | Regularization parameter for l2-norm penalty. | 0.0 |
|
|
|
| tolerance | +float | Run until the maximum correlation of elements in (X^T y) is less than this. | 1e-16 |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_initModelWithDataNoGram/10_**
|
|
|
|
|
|
Initialize LARS model, and train the model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelWithDataNoGram( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+integer,
|
|
|
+integer,
|
|
|
+float32, +float32, +float32).
|
|
|
+integer, -float32)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | Input data. | - |
|
|
|
| responses | +vector | A vector of targets. | - |
|
|
|
| transposeData | +integer(bool) | Should be true if the input data is column-major and false otherwise. | (1)true |
|
|
|
| useCholesky | +integer(bool) | Whether or not to use Cholesky decomposition when solving linear system (as opposed to using the full Gram matrix). | (0)false |
|
|
|
| lambda1 | +float | Regularization parameter for l1-norm penalty. | 0.0 |
|
|
|
| lambda2 | +float | Regularization parameter for l2-norm penalty. | 0.0 |
|
|
|
| tolerance | +float | Run until the maximum correlation of elements in (X^T y) is less than this. | 1e-16 |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_initModelWithDataWithGram/13_**
|
|
|
|
|
|
Initialize LARS model, pass in a precalculated Gram matrix and train the model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelWithDataWithGram( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+integer,
|
|
|
+integer,
|
|
|
+pointer(float_array), +integer, +integer,
|
|
|
+float32, +float32, +float32).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | Input data. | - |
|
|
|
| data | +matrix | Column-major input data (or row-major input data if rowMajor = true). | - |
|
|
|
| responses | +vector | A vector of targets. | - |
|
|
|
| transposeData | +integer(bool) | Should be true if the input data is column-major and false otherwise. | (1)true |
|
|
|
| useCholesky | +integer(bool) | Whether or not to use Cholesky decomposition when solving linear system (as opposed to using the full Gram matrix). | (0)false |
|
|
|
| gramMatrix | +matrix | Gram matrix. | - |
|
|
|
| lambda1 | +float | Regularization parameter for l1-norm penalty. | 0.0 |
|
|
|
| lambda2 | +float | Regularization parameter for l2-norm penalty. | 0.0 |
|
|
|
| tolerance | +float | Run until the maximum correlation of elements in (X^T y) is less than this. | 1e-16 |
|
|
|
| rowMajor | +integer(bool) | Set to false if the data is row-major. | (1)true |
|
|
|
| error | -float | minimum cost error | - |
|
|
|
|
|
|
---
|
|
|
|
|
|
## **_activeSet/2_**
|
|
|
## **_lars_activeSet/1_**
|
|
|
|
|
|
Get the set of active dimensions
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
activeSet(-pointer(float_array), -integer).
|
|
|
%% predicate definition
|
|
|
lars_activeSet(ActiveSetList) :-
|
|
|
activeSetI(Y, Ysize),
|
|
|
convert_float_array_to_list(Y, Ysize, ActiveSetList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(activeSet, c, activeSetI(-pointer(float_array), -integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -133,13 +82,18 @@ activeSet(-pointer(float_array), -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_beta/2_**
|
|
|
## **_lars_beta/1_**
|
|
|
|
|
|
Get the solution coefficients.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
beta(-pointer(float_array), -integer).
|
|
|
%% predicate definition
|
|
|
lars_beta(BetaList) :-
|
|
|
betaI(Y, Ysize),
|
|
|
convert_float_array_to_list(Y, Ysize, BetaList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(beta, c, betaI(-pointer(float_array), -integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -149,13 +103,18 @@ beta(-pointer(float_array), -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_betaPath/3_**
|
|
|
## **_lars_betaPath/2_**
|
|
|
|
|
|
Get the set of coefficients after each iteration. The solution is the last element.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
betaPath(-pointer(float_array), -integer, -integer).
|
|
|
%% predicate definition
|
|
|
lars_betaPath(BetaList, XCols) :-
|
|
|
betaPathI(X, XCols, XRows),
|
|
|
convert_float_array_to_2d_list(X, XCols, XRows, BetaList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(betaPath, c, betaPathI(-pointer(float_array), -integer, -integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -165,18 +124,24 @@ betaPath(-pointer(float_array), -integer, -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_computeError/7_**
|
|
|
## **_lars_computeError/5_**
|
|
|
|
|
|
Compute cost error of the given data matrix using the currently-trained LARS model.
|
|
|
|
|
|
Only ||y-beta\*X||2 is used to calculate cost error.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
computeError( +pointer(float_array), +integer, +integer,
|
|
|
%% predicate definition
|
|
|
lars_computeError(DataList, DataRows, ResponsesList, RowMajor, Error) :-
|
|
|
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
|
|
|
convert_list_to_float_array(ResponsesList, array(Ysize, Y)),
|
|
|
computeErrorI(X, Xsize, Xrownum, Y, Ysize, RowMajor, Error).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(computeError, c, computeErrorI( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
+integer,
|
|
|
[-float32]).
|
|
|
[-float32])).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -189,13 +154,18 @@ computeError( +pointer(float_array), +integer, +integer, |
|
|
|
|
|
---
|
|
|
|
|
|
## **_lambdaPath/2_**
|
|
|
## **_lars_lambdaPath/1_**
|
|
|
|
|
|
Get the set of values for lambda1 after each iteration; the solution is the last element.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
lambdaPath(-pointer(float_array), -integer).
|
|
|
%% predicate definition
|
|
|
lars_lambdaPath(LambdaPathList) :-
|
|
|
lambdaPathI(Y, Ysize),
|
|
|
convert_float_array_to_list(Y, Ysize, LambdaPathList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(lambdaPath, c, lambdaPathI(-pointer(float_array), -integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -205,13 +175,18 @@ lambdaPath(-pointer(float_array), -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_matUtriCholFactor/3_**
|
|
|
## **_lars_matUtriCholFactor/2_**
|
|
|
|
|
|
Get the upper triangular cholesky factor.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
matUtriCholFactor(-pointer(float_array), -integer, -integer).
|
|
|
%% predicate definition
|
|
|
lars_matUtriCholFactor(FactorList, XCols) :-
|
|
|
matUtriCholFactorI(X, XCols, XRows),
|
|
|
convert_float_array_to_2d_list(X, XCols, XRows, FactorList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(matUtriCholFactor, c, matUtriCholFactorI(-pointer(float_array), -integer, -integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -221,15 +196,21 @@ matUtriCholFactor(-pointer(float_array), -integer, -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_predict/6_**
|
|
|
## **_lars_predict/4_**
|
|
|
|
|
|
Predict y_i for each data point in the given data matrix using the currently-trained LARS model.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
predict( +pointer(float_array), +integer, +integer,
|
|
|
%% predicate definition
|
|
|
lars_predict(PointsList, PointsRows, PredicList, RowMajor) :-
|
|
|
convert_list_to_float_array(PointsList, PointsRows, array(Xsize, Xrownum, X)),
|
|
|
predictI(X, Xsize, Xrownum, Y, Ysize, RowMajor),
|
|
|
convert_float_array_to_list(Y, Ysize, PredicList).
|
|
|
|
|
|
%% foreign c++ predicate definition
|
|
|
foreign(predict, c, predictI( +pointer(float_array), +integer, +integer,
|
|
|
-pointer(float_array), -integer,
|
|
|
+integer).
|
|
|
+integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -241,38 +222,12 @@ predict( +pointer(float_array), +integer, +integer, |
|
|
|
|
|
---
|
|
|
|
|
|
## **_train/9_**
|
|
|
|
|
|
Train the LARS model with the given data.
|
|
|
|
|
|
The input matrix (like all mlpack matrices) should be column-major – each column is an observation and each row is a dimension. However, because LARS is more efficient on a row-major matrix, this method will (internally) transpose the matrix. If this transposition is not necessary (i.e., you want to pass in a row-major matrix), pass 'false' for the transposeData parameter.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
train( +pointer(float_array), +integer, +integer,
|
|
|
+pointer(float_array), +integer,
|
|
|
-pointer(float_array), -integer,
|
|
|
+integer,
|
|
|
[-float32]).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
|
|
| Name | Type | Description | Default |
|
|
|
|------|------|-------------|---------|
|
|
|
| data | +matrix | Column-major input data (or row-major input data if rowMajor = true). | - |
|
|
|
| responses | +vector | A vector of targets. | - |
|
|
|
| beta | -vector | Vector to store the solution (the coefficients) in. | - |
|
|
|
| transposeData | +integer(bool) | Set to false if the data is row-major. | (1)true |
|
|
|
| error | -float | minimum cost error( | |
|
|
|
|
|
|
---
|
|
|
|
|
|
# Connected Links/Resources
|
|
|
|
|
|
If you want a more detailed explanation, then go to the python documentation. There is most of the time a good explanation on how the methods work and what the parameters do.
|
|
|
|
|
|
* [**MLpack::lars_C++\_documentation**](https://www.mlpack.org/doc/stable/doxygen/classmlpack_1_1regression_1_1LARS.html)
|
|
|
* [**MLpack::lars_Python_documentation**](https://www.mlpack.org/doc/stable/python_documentation.html#lars)
|
|
|
* [**MLpack::lars_C++\_documentation**](https://www.mlpack.org/doc/mlpack-3.4.2/doxygen/classmlpack_1_1regression_1_1LARS.html)
|
|
|
* [**MLpack::lars_Python_documentation**](https://www.mlpack.org/doc/mlpack-3.4.2/python_documentation.html#lars)
|
|
|
|
|
|
added some of the links from the python documentation
|
|
|
|
... | ... | |