{{announcement.body}}
{{announcement.title}}

# An Introduction to Redis-ML (Part 4)

DZone 's Guide to

# An Introduction to Redis-ML (Part 4)

### Redis-ML can implement machine learning models as Redis data types. As we reach Part 4 in the series, it's time to dive a little deeper and look at matrices!

· AI Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Before reading, be sure to check out Part 1, Part 2, and Part 3!

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.ADD` commands.

``````ML.MATRIX.MULTIPLY 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.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.

Topics:
ai, machine learning, matrix, redis-ml, tutorial

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Published at DZone with permission of Tague Griffith , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.