Redis^{e} Modules
RedisML : Machine Learning Model Server
Accelerate machine learning model serving and updates
Implementing machine learning models in production requires complex, timeconsuming development and choosing the right model to serve from disk introduces latency. Accelerate machine learning implementations with 13x improvement in model execution from Redis with RedisML. Share machine learning models among different applications with Redis as the native ML model storage and serving layer.
How it works
The module includes these primary features:
 Decision Tree ensembles (random forests) classification and regression
 Linear regression
 Logistic regression
Commands

ML.FOREST.ADD
key tree path ((NUMERICCATEGORIC) attr val  LEAF val) [...]
Available since: 3.4.0
Time Complexity: O(M*log(N)) where N is the tree’s depth and M is the number of nodes added
Module: redisml
This commands adds one or more nodes to the tree in the forest that’s stored under key. Trees are identified by numeric ids, treeid, that must begin at 0 and incremented by exactly 1 for each new tree.
Each of the nodes is described by its path and definition. The path argument is the path from the tree’s root to the node. A valid path always starts with the period character (.), which denotes the root. Optionally, the root may be followed by left or right branches , denoted by the characters l and r, respectively. For example, the path “.lr” refers to the right child of the root’s left child.
A node in the decision tree can either be a splitter or a terminal leaf. Splitter nodes are either numerical or categorical, and are added using the NUMERIC or CATEGORIC keywords. Splitter nodes also require specifying the examined attribute (attr) as well as the value (val) used in the comparison made during the branching decision. val is expected to be a doubleprecision floating point value for numerical splitters, and a string for categorical splitter nodes.
The leaves are created with the LEAF keyword and only require specifying their doubleprecision floating point value (val).
Return Value
Simple string reply
Examples
redis> ML.FOREST.ADD myforst 0 . NUMERIC 1 0.1 .l LEAF 1 .r LEAF 0 OKredis> ML.FOREST.ADD myforst 1 . NUMERIC 1 0.1 .l LEAF 1 .r LEAF 0 OKredis> ML.FOREST.ADD myforst 2 . NUMERIC 1 0.1 .l LEAF 0 .r LEAF 1 OKredis> ML.FOREST.RUN myforst 1:0.01 CLASSIFICATION “1”redis> ML.FOREST.RUN myforst 1:0.2 CLASSIFICATION “0”

ML.FOREST.RUN
key sample (CLASSIFICATIONREGRESSION)
The forest that’s stored in
key
is used for generating the predicted value for thesample
. The sample is given as a string that is a vector of attributevalue pairs in the format ofattr:val
. For example, the sample “gender:male” has a single attribute, gender, whose value is male. A sample may have multiple such attributevalue pairs, and these must be commaseparated (,
) in the string vector. For example, a sample of a 25 years old male is expressed as “gender:male,age:25”.Return Value
Bulk string reply, specifically the predicted value of the sample
Examples
redis> ML.FOREST.ADD myforst 0 . NUMERIC 1 0.1 .l LEAF 1 .r LEAF 0
OK
redis> ML.FOREST.ADD myforst 1 . NUMERIC 1 0.1 .l LEAF 1 .r LEAF 0
OK
redis> ML.FOREST.ADD myforst 2 . NUMERIC 1 0.1 .l LEAF 0 .r LEAF 1
OK
redis> ML.FOREST.RUN myforst 1:0.01 CLASSIFICATION
“1”
redis> ML.FOREST.RUN myforst 1:0.2 CLASSIFICATION
“0”

ML.LINREG.SET
key intercept coefficient [...]
This command sets the linear regression predictor that’s stored in
key
. The predictor’s intercept is specified by intercept, followed by one or morecoefficient
arguments of the independent variables.Return Value
Examples
redis> ML.LINREG.SET linear 2 3 4 5
OK
redis> ML.LINREG.PREDICT linear 1 1 1
“14”

ML.LINREG.PREDICT
ML.LINREG.PREDICT key feature [...]
The linear regression predictor stored in
key
is used for predicting the result based on one or more features that are given by thefeature
argument(s).Return Value
Bulk string reply, specifically the predicted value
Examples
redis> ML.LINREG.SET linear 2 3 4 5
OK
redis> ML.LINREG.PREDICT linear 1 1 1
“14”

ML.LOGREG.SET
key intercept coefficient [...]
This command sets the logistic regression predictor that’s stored in
key
. The predictor’s intercept is specified byintercept
, followed by one or morecoefficient
arguments of the independent variables.Return Value
Examples
redis> ML.LOGREG.SET logistic 0 2 2
OK
redis> ML.LOGREG.PREDICT logistic 3 1
“0.017986209962091559”

ML.LOGREG.PREDICT
key feature [...]
The logistic regression predictor stored in
key
is used for predicting the result based on one or more features that are given by thefeature
argument(s).Return Value
Bulk string reply, specifically the predicted value
Examples
redis> ML.LOGREG.SET logistic 0 2 2
OK
redis> ML.LOGREG.PREDICT logistic 3 1
“0.017986209962091559”

ML.MATRIX.SET
key n m entry11 .. entrynm
Sets
key
to store a matrix ofn
rows,m
columns and doubleprecision float entries ranging fromentry11
toentrynm
.Return Value
Examples
redis> ML.MATRIX.SET rowvector 1 3 1 2 3
OK
redis> ML.MATRIX.SET square 3 3 1 2 3 4 5 6 7 8 9
OK

ML.MATRIX.GET
key
Available since: 3.4.0
Time Complexity: O(N*M) where N is the number of rows and M is the number of columns
Module: redismlReturns the matrix’s dimensions and entries.
Return Value
Array reply, specifically:
1. The first element is the number of rows
2. The second element is the number of columns
3. The following elements are the entriesExamples
redis> ML.MATRIX.SET a 2 3 1 2 5 3 4 6
OK
redis> ML.MATRIX.GET a
1) (integer) 2
2) (integer) 3
3) “1”
4) “2”
5) “3”
6) “4”
7) “5”
8) “6”

ML.MATRIX.ADD
matrix1 matrix2 sum
The result of adding the two matrices stored in
matrix1
andmatrix2
is set insum
.Return Value
Examples
redis> ML.MATRIX.SET a 2 3 1 2 5 3 4 6
OK
redis> ML.MATRIX.SET b 2 3 1 2 3 4 7 1
OK
redis> ML.MATRIX.ADD a b ab
OK
redis> ML.MATRIX.GET ab
1) (integer) 2
2) (integer) 3
3) “2”
4) “3”
5) “8”
6) “7”
7) “11”
8) “7”

ML.MATRIX.MULTIPLY
matrix1 matrix2 product
The result of multiplying the two matrices stored in
matrix1
andmatrix2
is set inproduct
.Return Value
Examples
redis> ML.MATRIX.SET a 2 3 1 2 5 3 4 6
OK
redis> ML.MATRIX.SET b 3 2 1 2 3 4 7 1
OK
redis> ML.MATRIX.MULTIPLY a b ab
OK
redis> ML.MATRIX.GET ab
1) (integer) 2
2) (integer) 2
3) “42”
4) “15”
5) “57”
6) “28”

ML.MATRIX.SCALE
key scalar
Available since: 3.4.0
Time Complexity: O(N*M) where N is the number of rows and M is the number of columns
Module: redismlUpdates the entries of the matrix stored in
key
by multiplying them withscalar
.Return Value
Examples
redis> ML.MATRIX.SET a 2 3 1 2 5 3 4 6
OK
redis> ML.MATRIX.SCALE a 2
OK
redis> ML.MATRIX.GET a
1) (integer) 2
2) (integer) 3
3) “2”
4) “4”
5) “10”
6) “6”
7) “8”
8) “12”