diff --git a/docs/howtos/redisai/getting-started/index-gettingstarted.mdx b/docs/howtos/redisai/getting-started/index-gettingstarted.mdx new file mode 100644 index 0000000000..1cd76d8487 --- /dev/null +++ b/docs/howtos/redisai/getting-started/index-gettingstarted.mdx @@ -0,0 +1,259 @@ +--- +id: index-gettingstarted +title: RedisAI Tutorial +sidebar_label: RedisAI Tutorial +slug: /howtos/redisai/getting-started +authors: [ajeet] +--- + + +RedisAI is a Redis module for executing deep learning/machine learning models and managing their data. It provides tensors as a data type and deep learning model execution on CPUs and GPUs. RedisAI turns Redis Enterprise into a full-fledged deep learning runtime.The RedisAI module is seamlessly plugged into Redis. It is a scalable platform that addresses the unique requirements for both AI training and AI inference in one server. It provides a complete software platform that allows data scientists to easily deploy and manage AI solutions for enterprise applications. + +The platform combines popular open source deep learning frameworks (PyTorch, ONNXRuntime, and TensorFlow), software libraries, and Redis modules like RedisGears, RedisTimeSeries, and more. With RedisAI, AI application developers no longer have to worry about tuning databases for performance. Requiring no added infrastructure, RedisAI lets you run your inference engine where the data lives, decreasing latency. + +Below is an interesting example of Iris (a genus of species of flowering plants with showy flowers) classification based on measurement of width and length of sepal/petals that makes up input tensors and how to load these measurements into RedisAI: + +### Step 1. Installing RedisAI + + ```bash + docker run \ + -p 6379:6379 \ + redislabs/redismod \ + --loadmodule /usr/lib/redis/modules/redisai.so \ + ONNX redisai_onnxruntime/redisai_onnxruntime.so + ``` + +You will see that ONNX backend getting loaded as shown below in the results. + + ```bash + 1:C 09 Jun 2021 12:28:47.985 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo + 1:C 09 Jun 2021 12:28:47.985 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=1, just started + 1:C 09 Jun 2021 12:28:47.985 # Configuration loaded + 1:M 09 Jun 2021 12:28:47.987 * Running mode=standalone, port=6379. + 1:M 09 Jun 2021 12:28:47.987 # Server initialized + 1:M 09 Jun 2021 12:28:47.987 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. + 1:M 09 Jun 2021 12:28:47.989 * Redis version found by RedisAI: 6.0.1 - oss + 1:M 09 Jun 2021 12:28:47.989 * RedisAI version 10003, git_sha=7f808a934dff121e188cb76fdfcc3eb1f9ec7cbf + 1:M 09 Jun 2021 12:28:48.011 * ONNX backend loaded from /usr/lib/redis/modules/backends/redisai_onnxruntime/redisai_onnxruntime.so + 1:M 09 Jun 2021 12:28:48.011 * Module 'ai' loaded from /usr/lib/redis/modules/redisai.so + 1:M 09 Jun 2021 12:28:48.011 * Ready to accept connections + ``` + +You can verify if the RedisAI module is loaded or not by running the following command: + + ```bash + 127.0.0.1:6379> info modules + # Modules + module:name=ai,ver=10003,api=1,filters=0,usedby=[],using=[],options=[] + + # ai_git + ai_git_sha:7f808a934dff121e188cb76fdfcc3eb1f9ec7cbf + + # ai_load_time_configs + ai_threads_per_queue:1 + ai_inter_op_parallelism:0 + ai_intra_op_parallelism:0 + ``` + + +### Step 2. Setup Python Environment + +Ensure that Python3.8+ is installed. + + ```bash + brew install python + ``` + +Create a Python virtual environment and activate it: + +```bash + python3 -m venv venv + . ./venv/bin/activate +``` + + + +### Step 3. Install PIP + + ```bash + pip install --upgrade pip + ``` + +### Step 4. Clone the repository + + ```bash + git clone https://github.com/redis-developer/redisai-iris + ``` + +### Step 5. Install the dependencies + + ```bash + pip install -r requirements.txt + ``` + + +### Step 6. Build the ONNX Model + +RedisAI supports DL/ML identifiers and their respective backend libraries, including: + +- TF: The TensorFlow backend +- TFLITE: The TensorFlow Lite backend +- TORCH: The PyTorch backend +- ONNX: ONNXRuntime backend + +A complete list of supported backends is in the [release notes for each version](https://docs.redis.com/latest/modules/redisai/release-notes/redisai-1.0-release-notes/). + + ```bash + python build.py + ``` + +### Step 7: Deploy the Model into RedisAI + +A Model is a Deep Learning or Machine Learning frozen graph that was generated by some framework. The RedisAI Model data structure represents a DL/ML model that is stored in the database and can be run. Models, like any other Redis and RedisAI data structures, are identified by keys. A Model’s key is created using the `AI.MODELSET` command and requires the graph payload serialized as a protobuf for input. + + +NOTE: This requires redis-cli. If you don't have redis-cli, I've found the easiest way to get it is to download, build, and install Redis itself. Details can be found at [the Redis quickstart page](https://redis.io/topics/quickstart) + + ```bash + redis-cli -x AI.MODELSET iris ONNX CPU BLOB < iris.onnx + ``` + +### Step 8. Make Some Predictions + +[The `AI.TENSORSET` command](https://oss.redis.com/redisai/commands/#aitensorset) stores a tensor as the value of a key. + +Launch redis-cli: + + ```bash + redis-cli + ``` + +### Step 9. Set the input tensor + + +This will set the key 'iris' to the 2x4 RedisAI tensor (i.e. 2 sets of inputs of 4 values each). + + + + ```bash + AI.TENSORSET iris:in FLOAT 2 4 VALUES 5.0 3.4 1.6 0.4 6.0 2.2 5.0 1.5 + ``` + +where, +- iris:in refers to the tensor's key name, +- FLOAT is a tensor's data type +- {5.0 3.4 1.6 0.4} refers to 1st item with 4 features +- {6.0 2.2 5.0 1.5} refers to 2nd item with 4 features + + +### Step 10. Display TENSORGET in BLOB format + +The `AI.TENSORGET` command returns a tensor stored as key's value. +The BLOB indicates that data is in binary format and is provided via the subsequent data argument + + + ```bash + redis-cli AI.TENSORGET iris:in BLOB + "\x00\x00\xa0@\x9a\x99Y@\xcd\xcc\xcc?\xcd\xcc\xcc>\x00\x00\xc0@\xcd\xcc\x0c@\x00\x00\xa0@\x00\x00\xc0?" + ``` + + +### Step 11. Check the predictions + + ```bash + redis-cli AI.TENSORGET iris:in VALUES + 1) "5" + 2) "3.4000000953674316" + 3) "1.6000000238418579" + 4) "0.40000000596046448" + 5) "6" + 6) "2.2000000476837158" + 7) "5" + 8) "1.5" + ``` + + + +### Step 12. Display TENSORGET META information + +The META used with `AI.TENSORGET` returns the tensor's metadata as shown below: + + + ```bash + redis-cli AI.TENSORGET iris:in META + 1) "dtype" + 2) "FLOAT" + 3) "shape" + 4) 1) (integer) 2 + 2) (integer) 4 + + ``` + +### Step 13. Display TENSORGET META information with tensor values + + ```bash + redis-cli AI.TENSORGET iris:in META VALUES + 1) "dtype" + 2) "FLOAT" + 3) "shape" + 4) 1) (integer) 2 + 2) (integer) 4 + 5) "values" + 6) 1) "5" + 2) "3.4000000953674316" + 3) "1.6000000238418579" + 4) "0.40000000596046448" + 5) "6" + 6) "2.2000000476837158" + 7) "5" + 8) "1.5" + ``` + + +### Step 14. Run the model + +Define inputs for the loaded model. + + + ```bash + redis-cli AI.MODELRUN iris INPUTS iris:in OUTPUTS iris:inferences iris:scores + OK + ``` + + +### Step 15. Make the prediction + + ```bash + redis-cli AI.TENSORGET iris:inferences VALUES META + 1) "dtype" + 2) "INT64" + 3) "shape" + 4) 1) (integer) 2 + 5) "values" + 6) 1) (integer) 0 + 2) (integer) 2 + ``` + +### References + +- [Sample IRIS Classification Source Code](https://github.com/redis-developer/redisai-iris) +- [RedisAI - A Server for Machine and Deep Learning Models](https://oss.redis.com/redisai/) + + +### Redis University + +#### RedisAI Explained + +
+ +
+ + + +#### RedisAI from the Command Line + +
+ +
+ + diff --git a/docs/howtos/redisai/index-redisai.mdx b/docs/howtos/redisai/index-redisai.mdx index adcd7c3b43..388338d2db 100644 --- a/docs/howtos/redisai/index-redisai.mdx +++ b/docs/howtos/redisai/index-redisai.mdx @@ -1,250 +1,31 @@ --- id: index-redisai -title: RedisAI Tutorial -sidebar_label: RedisAI Tutorial -slug: /howtos/redisai -authors: [ajeet] +title: RedisAI Tutorial +sidebar_label: Overview +slug: /howtos/redisai/ --- +import RedisCard from '@site/src/theme/RedisCard'; -RedisAI is a Redis module for executing deep learning/machine learning models and managing their data. It provides tensors as a data type and deep-learning model execution on CPUs and GPUs. RedisAI turns Redis Enterprise into a full-fledged deep- learning runtime.The RedisAI module is seamlessly plugged into Redis. It is a scalable platform that addresses the unique requirements for both AI training and AI inference in one server. It provides a complete software platform that allows data scientists to easily deploy and manage AI solutions for enterprise applications. -The platform combines popular open source deep learning frameworks (PyTorch, ONNXRuntime, and TensorFlow), software libraries, and Redis modules like RedisGears, RedisTimeSeries, and more. With RedisAI, AI application developers no longer have to worry about tuning databases for performance. Requiring no added infrastructure, RedisAI lets you run your inference engine where the data lives, decreasing latency. -Below is an interesting example of Iris(a genus of species of flowering plants with showy flowers) classification based on measurement of width and length of sepal/petals that makes up input tensors and how to load these measurements into RedisAI: +The following links provides you with the available options to get started with RedisTimeSeries -### Step 1. Installing RedisAI +
- ```bash - docker run \ - -p 6379:6379 \ - redislabs/redismod \ - --loadmodule /usr/lib/redis/modules/redisai.so \ - ONNX redisai_onnxruntime/redisai_onnxruntime.so - ``` - -You will see that ONNX backend getting loaded as shown below in the results. - - ```bash - 1:C 09 Jun 2021 12:28:47.985 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo - 1:C 09 Jun 2021 12:28:47.985 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=1, just started - 1:C 09 Jun 2021 12:28:47.985 # Configuration loaded - 1:M 09 Jun 2021 12:28:47.987 * Running mode=standalone, port=6379. - 1:M 09 Jun 2021 12:28:47.987 # Server initialized - 1:M 09 Jun 2021 12:28:47.987 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. - 1:M 09 Jun 2021 12:28:47.989 * Redis version found by RedisAI: 6.0.1 - oss - 1:M 09 Jun 2021 12:28:47.989 * RedisAI version 10003, git_sha=7f808a934dff121e188cb76fdfcc3eb1f9ec7cbf - 1:M 09 Jun 2021 12:28:48.011 * ONNX backend loaded from /usr/lib/redis/modules/backends/redisai_onnxruntime/redisai_onnxruntime.so - 1:M 09 Jun 2021 12:28:48.011 * Module 'ai' loaded from /usr/lib/redis/modules/redisai.so - 1:M 09 Jun 2021 12:28:48.011 * Ready to accept connections - ``` - -You can verify if RedisAI module is loaded or not by running the below CLI - - ```bash - 127.0.0.1:6379> info modules - # Modules - module:name=ai,ver=10003,api=1,filters=0,usedby=[],using=[],options=[] - - # ai_git - ai_git_sha:7f808a934dff121e188cb76fdfcc3eb1f9ec7cbf - - # ai_load_time_configs - ai_threads_per_queue:1 - ai_inter_op_parallelism:0 - ai_intra_op_parallelism:0 - ``` - - -### Step 2. Setup Python Environment - -Ensure that Python3.8+ is installed. - - ```bash - brew install python - ``` - -### Step 3. Install PIP - - ```bash - pip install --upgrade pip - ``` - -### Step 4. Clone the repository - - ```bash - git clone https://github.com/redis-developer/redisai-iris - ``` - -### Step 5. Install the dependencies - - ```bash - pip install -r requirements.txt - ``` - - -### Step 6. Build the ONNX Model - -RedisAI supports DL/ML identifiers and their respective backend libraries, including: - -- TF: The TensorFlow backend -- TFLITE: The TensorFlow Lite backend -- TORCH: The PyTorch backend -- ONNX: ONNXRuntime backend - -A complete list of supported backends is in the [release notes for each version](https://docs.redis.com/latest/modules/redisai/release-notes/redisai-1.0-release-notes/). - - ```bash - python3 build.py - ``` - -### Step 7: Deploy the Model into RedisAI - -Model is a Deep Learning or Machine Learning frozen graph that was generated by some framework. The RedisAI Model data structure represents a DL/ML model that is stored in the database and can be run.Models, like any other Redis and RedisAI data structures, are identified by keys. A Model’s key is created using the AI.MODELSET command and requires the graph payload serialized as protobuf for input. - - -NOTE: This requires redis-cli. If you don't have redis-cli, I've found the easiest way to get it is to download, build, and install Redis itself. Details can be found at [the Redis quickstart page](https://redis.io/topics/quickstart) - - ```bash - redis-cli -x AI.MODELSET iris ONNX CPU BLOB < iris.onnx - ``` - -### Step 8. Make Some Predictions - -[The AI.TENSORSET command](https://oss.redis.com/redisai/commands/#aitensorset) stores a tensor as the value of a key. - -Launch redis-cli: - - ```bash - redis-cli - ``` - -### Step 9. Set the input tensor - - -This will set the key 'iris' to the 2x4 RedisAI tensor( i.e. 2 sets of inputs of 4 values each) - - - - ```bash - AI.TENSORSET iris:in FLOAT 2 4 VALUES 5.0 3.4 1.6 0.4 6.0 2.2 5.0 1.5 - ``` - -where, -- iris refers to the tensor's key name, -- FLOAT is a tensor's data type -- {5.0 3.4 1.6 0.4} refers to 1st item with 4 features -- {6.0 2.2 5.0 1.5} refers to 2nd item with 4 features - - -### Step 10. Display TENSORGET in BLOB format - -The AI.TENSORGET command returns a tensor stored as key's value. -The BLOB indicates that data is in binary format and is provided via the subsequent data argument - - - ```bash - redis-cli AI.TENSORGET iris:in BLOB - "\x00\x00\xa0@\x9a\x99Y@\xcd\xcc\xcc?\xcd\xcc\xcc>\x00\x00\xc0@\xcd\xcc\x0c@\x00\x00\xa0@\x00\x00\xc0?" - ``` - - -### Step 11. Check the predictions - - ```bash - redis-cli AI.TENSORGET iris:in VALUES - 1) "5" - 2) "3.4000000953674316" - 3) "1.6000000238418579" - 4) "0.40000000596046448" - 5) "6" - 6) "2.2000000476837158" - 7) "5" - 8) "1.5" - ``` - - - -### Step 12. Display TENSORGET META information - -The META used with `AI.TENSORGET` returns the tensor's metadata as shown below: - - - ```bash - redis-cli AI.TENSORGET iris:in META - 1) "dtype" - 2) "FLOAT" - 3) "shape" - 4) 1) (integer) 2 - 2) (integer) 4 - - ``` - -### Step 13. Display TENSORGET META information with tensor values - - ```bash - redis-cli AI.TENSORGET iris:in META VALUES - 1) "dtype" - 2) "FLOAT" - 3) "shape" - 4) 1) (integer) 2 - 2) (integer) 4 - 5) "values" - 6) 1) "5" - 2) "3.4000000953674316" - 3) "1.6000000238418579" - 4) "0.40000000596046448" - 5) "6" - 6) "2.2000000476837158" - 7) "5" - 8) "1.5" - ``` - - -### Step 14. Run the model - -Define inputs for the loaded model. - - - ```bash - redis-cli AI.MODELRUN iris INPUTS iris:in OUTPUTS iris:inferences iris:scores - OK - ``` - - -### Step 15. Make the prediction - - ```bash - redis-cli AI.TENSORGET iris:inferences VALUES META - 1) "dtype" - 2) "INT64" - 3) "shape" - 4) 1) (integer) 2 - 5) "values" - 6) 1) (integer) 0 - 2) (integer) 2 - ``` - -### References - -- [Sample IRIS Classification Source Code](https://github.com/redis-developer/redisai-iris) -- [RedisAI - A Server for Machine and Deep Learning Models](https://oss.redis.com/redisai/) - - -### Redis University - -#### RedisAI Explained - -
- +
+
- - -#### RedisAI from the Command Line - -
- +
+ +
- - diff --git a/docs/howtos/redisai/market-basket-analysis/images/image1.png b/docs/howtos/redisai/market-basket-analysis/images/image1.png new file mode 100644 index 0000000000..3605d184d3 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image1.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image2.png b/docs/howtos/redisai/market-basket-analysis/images/image2.png new file mode 100644 index 0000000000..634e1dcc88 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image2.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image3.png b/docs/howtos/redisai/market-basket-analysis/images/image3.png new file mode 100644 index 0000000000..9d4a3f93c2 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image3.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image4.png b/docs/howtos/redisai/market-basket-analysis/images/image4.png new file mode 100644 index 0000000000..67c1e761e0 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image4.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image5.png b/docs/howtos/redisai/market-basket-analysis/images/image5.png new file mode 100644 index 0000000000..157cf54126 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image5.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image6.png b/docs/howtos/redisai/market-basket-analysis/images/image6.png new file mode 100644 index 0000000000..2668281b0c Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image6.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_10.png b/docs/howtos/redisai/market-basket-analysis/images/image_10.png new file mode 100644 index 0000000000..e3ca766177 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_10.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_11.png b/docs/howtos/redisai/market-basket-analysis/images/image_11.png new file mode 100644 index 0000000000..2440860eae Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_11.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_12.png b/docs/howtos/redisai/market-basket-analysis/images/image_12.png new file mode 100644 index 0000000000..2321fdea30 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_12.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_13.png b/docs/howtos/redisai/market-basket-analysis/images/image_13.png new file mode 100644 index 0000000000..1ac3009ed8 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_13.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_14.png b/docs/howtos/redisai/market-basket-analysis/images/image_14.png new file mode 100644 index 0000000000..39254811d6 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_14.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_7.png b/docs/howtos/redisai/market-basket-analysis/images/image_7.png new file mode 100644 index 0000000000..1600b6a753 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_7.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_8.png b/docs/howtos/redisai/market-basket-analysis/images/image_8.png new file mode 100644 index 0000000000..947f0d4d93 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_8.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/images/image_9.png b/docs/howtos/redisai/market-basket-analysis/images/image_9.png new file mode 100644 index 0000000000..deaaf73978 Binary files /dev/null and b/docs/howtos/redisai/market-basket-analysis/images/image_9.png differ diff --git a/docs/howtos/redisai/market-basket-analysis/index-market-basket-analysis.mdx b/docs/howtos/redisai/market-basket-analysis/index-market-basket-analysis.mdx new file mode 100644 index 0000000000..3de687d0f8 --- /dev/null +++ b/docs/howtos/redisai/market-basket-analysis/index-market-basket-analysis.mdx @@ -0,0 +1,322 @@ +--- +id: index-market-basket-analysis +title: How E-Commerce Websites Can Improve Market Basket Analysis with RedisAI and RedisGears +sidebar_label: Market-Basket Analysis using RedisAI and RedisGears +slug: /howtos/redisai/market-basket-analysis +authors: [ajeet,christian] +--- + +![Market-Basket](images/image1.png) + +Whenever you shop online, there is a recommendation system (just like a digital salesman) that is busy guiding you toward the most likely product you might purchase. One good example is movie recommendations offered up when you are searching through the leading entertainment services such as Amazon Prime or Netflix. Modern multi-channel retailers are turning to real-time retail data analytics to understand their customer behavior so that they can properly plan and promote products, increase sales, and optimize supply chain performance. + +### What is Market Basket Analysis? + +With the rapid growth of e-commerce data, more and more organizations are discovering ways of using **Market Basket Analysis** (MBA) to gain useful insights into associations and hidden relationships. A predictive version of market basket analysis is gaining popularity across many sectors in an effort to identify sequential purchases. + +Market basket analysis is a technique based on buying a group item. The approach is based on the theory that customers who buy a certain item (or group of items) are more likely to buy another specific item (or group of items). It creates If-Then scenario rules; for example, if item A is purchased, then item B is likely to be purchased. The rules are probabilistic in nature or, in other words, they are derived from the frequencies of co-occurrence in the observations. + + If {A} then likelihood of B is probably more accurate + +Say you are in a mobile retail shop to buy a mobile phone. Based on the analysis, are you more likely to buy a leather phone case or earphones in the same transaction than somebody who did not buy a phone? + + + +![alt_text](images/image2.png "image_tooltip") + + +If someone buys a mobile phone, they are more likely to buy accessories like a leather cover, battery pack, wireless headphones, and so on. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Basket + Product 1 + Product 2 + Product 3 +
1 + Mobile Phone + Leather Cover + Tempered Glass Screen +
2 + Mobile Phone + Tempered Glass Screen + +
3 + Mobile Phone + 64GB SD card + Battery Pack +
4 + Leather Cover + 1 year Damage Protection + +
5 + Mobile Phone + 1 year Damage Protection + Tempered Glass Screen +
+ + +As shown above, there are five baskets containing varying combinations of Mobile Phone, Earphone, Battery Pack, and Leather Cover. Retailers use the results of the Market Basket Analysis to guide product placement in stores as well as cross-category and co-marketing promotions. + +Online fraud, which is soaring due to growth in online transactions, digital identity threats, cybercrime, and customer fraud, [is a $20 billion problem](https://www.marketsandmarkets.com/Market-Reports/fraud-detection-prevention-market-1312.html) that just keeps getting worse and worse, especially for financial services companies. According to a recent report, 79% of retailers have observed an increase in fraud. All of these results are driving fraud detection to be much more important than ever before. + +Interestingly, Market Basket Analysis is also used in fraud detection. It may be possible to identify purchase behavior that can be associated with fraud on the basis of MBA data that contain credit card information. Watch this [video](https://www.youtube.com/watch?v=UBRuYVn4MjQ) for more information on this topic. + +In the following example, we demonstrate how you can quickly run your Market Basket Analysis using RedisAI and RedisGears. This tutorial will allow you to score various baskets based on pre-populated users and profiles. The data was pulled from [Amazon reviews datasets](https://s3.amazonaws.com/amazon-reviews-pds/readme.html) and the model was built using reviews with verified purchases. Individual user profiles were compiled and analyzed to describe baskets that were trained to build the model that is included in this repo. + +### Prerequisites: + + + +* Docker +* Docker Compose + +### Step 1. Clone the repository + + +``` +git clone https://github.com/redis-field-engineering/demo-market-basket-analysis +``` + + +### Step 2. Bring up the services + +The below compose file specifies the two services: redis as a backend and Market Basket Analysis as a frontend application. It uses a redismod Docker image that is exposed to port 6379 while the application service uses `maguec/ai_basket_analysis:latest `image that runs a container exposed to port 8080. + + +#### Redis service + +The `redis` service uses a public Redis image called redismod that holds all the necessary Redis modules, such as RedisGears and RedisAI. It then binds the container and the host machine to the exposed port, `6379`. + + +#### Frontend service + +The `application `service uses an image `maguec/ai_basket_analysis:latest` pulled from the Docker Hub registry. The Docker image uses [app.py](https://github.com/redis-field-engineering/demo-market-basket-analysis/blob/master/app.py) that is just 161 lines of Python code written to be as simple as possible. It then binds the container and the host machine to the exposed port, `8080.` + + +``` +version: '3' + + +services: + redis: + image: "redislabs/redismod:edge" + ports: + - "6379:6379" + application: + image: "maguec/ai_basket_analysis:latest" + links: + - "redis:redis" + ports: + - "8080:8080" + environment: + - REDIS_SERVER=redis + - REDIS_PORT=6379 + +``` + + +From your project directory, start up your application by running `docker-compose up`. + + +``` +docker-compose up -d +``` + + +### Step 3. List the service status + + +``` +docker-compose ps +NAME COMMAND SERVICE STATUS PORTS +demo-market-basket-analysis-application-1 "python3 app.py" application running 0.0.0.0:8080->8080/tcp +demo-market-basket-analysis-redis-1 "redis-server --load…" redis running 0.0.0.0:6379->6379/tcp + +``` + + +### Step 4. Access the app + +Open [https://localhost:8080](https://localhost:8080) to access the application. + + + + +![alt_text](images/image3.png "image_tooltip") + + +Select your preferred user from the list (e.g., pcgamer) + +Once you select the user, click login and that will open the profile page: + + +![alt_text](images/image4.png "image_tooltip") + + +Click on the cart tab and add new items. + + + + + +![alt_text](images/image5.png "image_tooltip") + + +Click “Score” and you will find that all the previously purchased categories will get pre-checked. + + + + +![alt_text](images/image6.png "image_tooltip") + + +As shown above, red indicates that this category of item has not been purchased by the customer before, while green indicates what has previously been purchased. Let us select a different user and experiment with baskets. + + + + +![alt_text](images/image_7.png "image_tooltip") + + +Click “Login.” + + + + +![alt_text](images/image_8.png "image_tooltip") + + +Click “Cart.” + + + + +![alt_text](images/image_9.png "image_tooltip") + + +Click “Score.” + + + + +![alt_text](images/image_10.png "image_tooltip") + + + +### How does it work? + + + + +![alt_text](images/image_11.png "image_tooltip") + + +User profiles are stored in Redis as [hash data structures](https://redis.io/commands#hash). After the user adds items to the cart for scoring, the cart is transformed into a tensor scored by [RedisAI](https://redisai.io/), and a confidence score is returned. + + + + +### Scoring + +The scores vary between 0 and 1. A score closer to 1 indicates that the basket of items by category is more likely to mirror broader purchasing patterns. Let’s look at another example for a user profile named "splurgenarrow": + + + + +![alt_text](images/image_12.png "image_tooltip") + + +If we add Wireless, then this is a highly likely basket with a score of 0.99989. Adding items categorized as "Wireless" results in a basket with a high likelihood of matching the purchasing patterns that we have modeled from previous data. + + + + +![alt_text](images/image_13.png "image_tooltip") + + +You can use the Redis [MONITOR](https://redis.io/commands/monitor) command in order to stream back every command processed by the Redis server. As shown below, you will find Redis data structures called “Hashes” commands that hold various field-value pairs for representing data objects. + + +``` +"GET" "session:53ee3f94-c507-4075-a60f-871546e2f90f" +"EXISTS" "USERLIST" +"SMEMBERS" "USERLIST" +... +"SETEX" +"GET" "session:53ee3f94-c507-4075-a60f-871546e2f90f" +"SETEX" +.. +"GET" "session:53ee3f94-c507-4075-a60f-871546e2f90f" +"HGETALL" "user:splurgenarrow" +``` + + +But if we add Major_Appliances, it will be an unlikely basket with a score of 0.00917 + + + +![alt_text](images/image_14.png "image_tooltip") + + + +``` +"GET" "session:53ee3f94-c507-4075-a60f-871546e2f90f" +"AI.TENSORSET" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f" "FLOAT" "1" "43" "VALUES" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "2.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "2.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "1.0" "0.0" "0.0" "0.0" "0.0" "0.0" "0.0" "16.0" "0.0" "0.0" "0.0" "0.0" "0.0" +"AI.MODELRUN" "profile_model" "INPUTS" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f" "OUTPUTS" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f:results" +"AI.TENSORGET" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f:results" "META" "BLOB" +"DEL" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f" +"DEL" "TENSOR:53ee3f94-c507-4075-a60f-871546e2f90f:results" +"HGETALL" "user:splurgenarrow" +"SETEX" + +``` + + +### Conclusion + +As digital marketing continues to grow, data-mining techniques such as Market Basket Analysis, powered with Redis Modules like RedisAI and RedisGears, are increasingly necessary for online retailers to better understand customer purchasing patterns. Retailers can use this insight into which items are frequently purchased together in order to optimize product placement, offer special deals, and create new product bundles to increase sales while also being better able to identify online fraud. + +### Further References: + + + +* [Source code of Market Basket Analysis ](https://github.com/redis-field-engineering/demo-market-basket-analysis) +* [Real-Time Fraud Detection with Azure and Redis Enterprise](https://www.youtube.com/watch?v=UBRuYVn4MjQ) + diff --git a/sidebars.js b/sidebars.js index 4af5b0e548..04faf8a0d5 100644 --- a/sidebars.js +++ b/sidebars.js @@ -307,7 +307,16 @@ module.exports = { ] }, 'howtos/redisgears/index-redisgears', - 'howtos/redisai/index-redisai', + { + type:'category', + label: 'RedisAI Tutorial', + items: + [ + 'howtos/redisai/index-redisai', + 'howtos/redisai/getting-started/index-gettingstarted', + 'howtos/redisai/market-basket-analysis/index-market-basket-analysis' + ] + }, 'howtos/leaderboard/index-leaderboard', 'howtos/ratelimiting/index-ratelimiting', 'howtos/caching/index-caching',