In this post, we're going to look at the matrix operations provided by the Redis-ML module and show some examples of how to process Matrix data using the Redis database.
Technical requirements: The example code in this post is written in Python and requires a Redis instance running Redis-ML. The instructions for setting up Redis can be found in either Part 1 or Part 2 of this series.
Matrices in Redis
Matrices are common in machine learning, statistics, finance, and a host of other domains, so they were a natural addition to Redis. The Redis-ML module adds matrices as a native Redis data type. It also provides mathematical operations that combine matrices to create new values.
Reading and writing matrix values is performed through the
ML.MATRIX.SET and the
ML.MATRIX.GET commands which have the following syntax:
ML.MATRIX.SET key n m entry11 .. entrynm ML.MATRIX.GET key
When working with the Redis-ML module, remember that commands use row-major format. Multiplication and addition are supported by the
ML.MATRIX.MULTIPLY and the
ML.MATRIX.MULTIPLY a b result ML.MATRIX.ADD a b result
These commands combine two matrices that are already in Redis and store the result in a new key.
If I wanted to compute a basic Matrix equation such as y = Ax + b using the Redis-ML module, I would enter the following commands into the Redis CLI:
127.0.0.1:6379> ML.MATRIX.SET a 3 3 4 0 0 0 2 0 0 0 1 127.0.0.1:6379> ML.MATRIX.SET x 3 1 1 4 1 127.0.0.1:6379> ML.MATRIX.SET b 3 1 1 1 1 127.0.0.1:6379> ML.MATRIX.MULTIPLY a x tmp 127.0.0.1:6379> ML.MATRIX.ADD tmp b y 127.0.0.1:6379> ML.MATRIX.GET y 1) (integer) 3 2) (integer) 1 3) "5" 4) "9" 5) "2"
Redis returns the result to our client with the shape of the matrix (in this case three rows and one column) followed by each of the elements of the matrix in row-major order.
I could also compute the matrix equation A' = cA (where c is a scalar value) using the following code:
Matrices are used for a wide range of applications, from linear transforms to representing multivariate probability distributions. In the next post, we'll look at decision trees and random forests, two additional classification models supported by Redis.
Please connect with me on twitter if you have questions regarding this or previous posts in the series.