<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mrfaizanrafiq on Medium]]></title>
        <description><![CDATA[Stories by Mrfaizanrafiq on Medium]]></description>
        <link>https://medium.com/@mrfaizanrafiq?source=rss-e6d2d4ec9530------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*_5u-hk8odC_P-hQm</url>
            <title>Stories by Mrfaizanrafiq on Medium</title>
            <link>https://medium.com/@mrfaizanrafiq?source=rss-e6d2d4ec9530------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:13:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrfaizanrafiq/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Streaming Data Analytics Using Kinesis Streams for Anomaly Detection via the Random Forest…]]></title>
            <link>https://medium.com/cloud-computing-aws-faizan-rafiq/streaming-data-analytics-using-kinesis-streams-for-anomaly-detection-via-the-random-forest-e014a23350e3?source=rss-e6d2d4ec9530------2</link>
            <guid isPermaLink="false">https://medium.com/p/e014a23350e3</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kinesis]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[big-data-analytics]]></category>
            <category><![CDATA[random-forest]]></category>
            <dc:creator><![CDATA[Mrfaizanrafiq]]></dc:creator>
            <pubDate>Thu, 16 Apr 2020 17:07:59 GMT</pubDate>
            <atom:updated>2020-04-16T17:07:59.657Z</atom:updated>
            <content:encoded><![CDATA[<h3>Streaming Data Analytics Using Kinesis Streams for Anomaly Detection via the Random Forest Algorithm (ML)</h3><p><strong>Author: Faizan Rafiq</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LE1EFBfY1vVk7sP3" /></figure><h3>Introduction</h3><p>This article contains content on temperature data anomaly detection using the RandomCutForest ML algorithm. We have used cloud vendor<em> Amazon Web Services </em>as a development platform for our anomaly detection application. As a simulation of an actual IoT temperature sensor device, a python program is developed which sends the data real-time to the kinesis data stream, the ingested data get processed by kinesis data analytics application using a random cut forest algorithm. Normal temperature data was supposed to be <em>10 to 20</em> degrees in range and <em>100 to 120 </em>range of data was being sent to stream as a known anomaly supposed to be detected by the system.</p><p>Algorithms score a number against each real-time temperature record, and processed data get stored on target location Amazon S3 processed bucket using Kinesis Firehose stream. For the visualization of processed data with error classification on a graphical presentation, we have used the Amazon QuickSight tool.</p><h3>Use Case</h3><p>Temperature Data Anomaly Detection using Random_Cut_Forest ML algorithm of Kinesis Data Analytics.</p><h3>Implementation Architecture Diagram</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XDerUsc9HAGLG6Au" /><figcaption>Article Architecture Diagram</figcaption></figure><h3>Introduction to Cloud Services Used in This Article</h3><p><strong>Amazon Kinesis Data Streams</strong></p><p>You can use Amazon Kinesis Data Streams to build your own streaming application. This application can process and analyze streaming data by continuously capturing and storing terabytes of data per hour from hundreds of thousands of sources.</p><p>Kinesis Stream’s services are not part of AWS Free Tier, so please consult below page <a href="https://aws.amazon.com/kinesis/data-streams/pricing/">https://aws.amazon.com/kinesis/data-streams/pricing/</a></p><p><strong>Amazon Kinesis Data Analytics</strong></p><p>Kinesis Data Analytics provides an easy and familiar standard SQL language to analyze streaming data in real-time. One of its most powerful features is that there are no new languages, processing frameworks, or complex machine learning algorithms that you need to learn.</p><blockquote><strong>Kinesis Data Analytics Cost</strong></blockquote><blockquote>With Amazon Kinesis Data Analytics, you pay only for what you use. There are no resources to provision or upfront costs associated with Amazon Kinesis Data Analytics.</blockquote><p>You are charged an hourly rate based on the number of Amazon Kinesis Processing Units (or KPUs) used to run your streaming application. A single KPU is a unit of stream processing capacity comprised of 1 vCPU compute and 4 GB memory. Amazon Kinesis Data Analytics automatically scales the number of KPUs required by your stream processing application as the demands of memory and compute vary in response to processing complexity and the throughput of streaming data processed.</p><p><strong>Amazon Kinesis Data Firehose</strong></p><p>Kinesis Data Firehose is the easiest way to load streaming data into AWS. It can capture, transform, and load streaming data into Amazon S3, Amazon Redshift, and Amazon Elasticsearch Service.</p><h3>Solution summary step by step</h3><p>The following is a quick walkthrough of the use case which has been implemented.</p><p><strong>Step 1: Kinesis Data Stream (Source)</strong></p><p>Created a data stream with shard 1.</p><p><strong>Step 2: Real-Time Stream Data Ingestion — Live Data Feed</strong></p><p>IoT sensors send streaming data into Kinesis Firehose Streams. For this, use a Python script to simulate an IoT temperature sensor device that sends the streaming data.</p><p><strong>Step 3: Create a Kinesis Analytics Application on AWS</strong></p><p>By using the built-in RANDOM_CUT_FOREST function in Kinesis Data Analytics, you can detect anomalies in real-time with the sensor data that is stored in Kinesis Data Streams. RANDOM_CUT_FOREST is also an appropriate algorithm for many other kinds of anomaly-detection use cases, for example, the media sentiment example mentioned earlier in this post.</p><p><strong>Step 4: Store Processed Data to S3 (Processed)</strong></p><p>The processed anomaly data is then loaded into the Kinesis Data Firehose delivery stream. By using the built-in integration that Kinesis Data Firehose has with Amazon S3, you can easily export the processed anomaly data into the S3 bucket.</p><p><strong>Step 5: Show the Visualization of S3(Processed)</strong></p><p>Use AWS QuickSight to visualize the insights of Processed data.</p><h3>Implementation detail on Steps Mentioned Above</h3><p><strong>Step 1: Kinesis Data Stream (Live)</strong></p><p>Created Kinesis Data Stream, to have a real-time data feed on a live source.We will use a python script as a dummy IoT sensor, which will ingest data to the kinesis stream in <strong>JSON </strong>format. Kinesis Data Stream has been set for the data retention period of 24 hours.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4-sWrHNrpNW-vq9S" /></figure><p><strong>Step 2: Stream Ingestion</strong></p><p>Developed and executed a python program which is sending random temperature data to kinesis stream, This python script is the main source of sending data to stream real-time. For our anomaly detection use case, the temperature range is 10 to 20 degrees, for the artificial anomaly, we ingest 100 to 120 anomaly degree data to the stream which will be sent to stream randomly. When a successful message sent to a stream python program will print a message that “<em>Data Placed on Kinesis Stream, Shard Id:shardId-000000000000</em>”.</p><pre>import json<br>import random<br>import boto3<br><br># Kinesis boto3 client to place/put data on the AWS Kinesis stream<br>KINESIS_CLIENT = boto3.client(&#39;kinesis&#39;, region_name=&#39;us-east-1&#39;)<br><br><br>kinesis_stream_name = &quot;STREAM-NAME&quot;</pre><pre>def getData(iotName, lowVal, highVal):<br>   data = {}<br>   data[&quot;iotName&quot;] = iotName<br>   data[&quot;iotValue&quot;] = random.randint(lowVal, highVal)<br>   return data<br><br>while 1:<br>   rnd = random.random()<br>   if (rnd &lt; 0.01):<br>      data = json.dumps(getData(&quot;DemoSensor&quot;, 100, 120))<br>      kinesis_put_response = KINESIS_CLIENT.put_record(StreamName=kinesis_stream_name,<br>                                                       Data=data,<br>                                                       PartitionKey=str(rnd))<br>      # Get shard_id from put response of kinesis stream where data is placed<br>      if not kinesis_put_response[&quot;ShardId&quot;]:<br>         raise Exception(&#39;Error: Data was not placed on kinesis stream&#39;)<br>      else:<br>         data_placed_shard_id = kinesis_put_response[&quot;ShardId&quot;]<br>         print(&quot;Data Placed on Kinesis Stream, Shard Id:&quot; + data_placed_shard_id)<br>   else:<br>      data = json.dumps(getData(&quot;DemoSensor&quot;, 10, 20))<br>      kinesis_put_response = KINESIS_CLIENT.put_record(StreamName=kinesis_stream_name,<br>                                                       Data=data,<br>                                                       PartitionKey=str(rnd))<br>      # Get shard_id from put response of kinesis stream where data is placed<br>      if not kinesis_put_response[&quot;ShardId&quot;]:<br>         raise Exception(&#39;Error: Data was not placed on kinesis stream&#39;)<br>      else:<br>         data_placed_shard_id = kinesis_put_response[&quot;ShardId&quot;]<br>         print(&quot;Data Placed on Kinesis Stream, Shard Id:&quot; + data_placed_shard_id)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/0*uLbyOJMyYmcdr_Ah" /></figure><p><em>Real-Time Data Ingestion</em></p><p>Above mentioned python program is in execution, Data Streaming from Local Python Program to kinesis stream analysis process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*l4a9BzNTtsPKNqpf" /></figure><p><strong>Step 3: Create a Kinesis Data Analytics Application</strong></p><p>Created a consumer application for the created kinesis data stream as Kinesis Data Analytics Application, which will use Random_Cut_Forest Algorithm to detect the anomalies from the live data. The detection is actually a calculation of score of the anomaly, Algorithm itself calculates the score of each temperature (a record) in the stream. We can then expose the data set results to the visualization of data where we can see the anomalies with high score data and normal data set with low scores by the algorithm.</p><p>Below are the references to the created application on the AWS Kinesis services:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*voW8qDSqoqAULAvE" /></figure><p>Configuration Details:</p><p>Configured created kinesis stream as source of analytic application, analyzed its data schema</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1003/0*Uk2DO2dtnOoHPUjk" /></figure><p>Successful Configuration of Source(Kinesis Stream) of Application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RrWaEL3DHI7d5YyM" /><figcaption>Successfully Configured Kinesis Data Application (Source)</figcaption></figure><p>Write Processing Logic (Random_Cut_Forest Algorithm):</p><p>Go to SQL Editor, Use predefined SQL operation templates, as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZZPwFafntWKs_5Tu" /><figcaption>Selection of Template given by Kinesis Data Analytics (Logic)</figcaption></figure><p>Selected Anomaly Detection Template, which involves <a href="https://aws.amazon.com/kinesis/data-analytics/whitepaper-real-time-anomaly-detection/"><strong>Random_Cut_Forest </strong>algorithm</a> of Machine Learning. It’s a voting-based algorithm that creates decision trees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PayzJLk23yI_ZFPc" /><figcaption>Kinesis Data Analytics Application — Logical Processing Code</figcaption></figure><p>Applied SQL Operation on the Schema, with Random_Cut_Forest ML algorithm which is a built-in operation within Kinesis data analytics:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*stUieNeQJYybOhfD" /></figure><p>Kinesis Data Analytics Application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2rd9kXZqBNK92nFQ" /></figure><p>Data Ingestion Graph by Kinesis Data Analytics:</p><p>The below graph shows the data ingestion stats to the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CegZf-y4P2DOIeGC" /><figcaption>Real-time data ingestion graph of Kinesis Streams</figcaption></figure><p>Stream Data is being ingested from a python script, analyzing it on Kinesis Data Analytics Application, we can see the live data incoming to the stream and be processed by the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aiCjKGF85m42I196" /><figcaption>Real-Time Processing By Random Cut Forest Kinesis Application</figcaption></figure><p>Finalized Configured Kinesis Data Analytic Application:</p><ul><li><strong>Source</strong>: Kinesis Data Stream</li><li><strong>Processing</strong>: Random_Cut_Forest Algorithm anomaly detection</li><li><strong>Destination</strong>: Firehose (Step 4 Below)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EvLZrm8EVacQWE7J" /><figcaption>Successfully Configured Kinesis Data Analytics Application (Output)</figcaption></figure><p><strong>Kinesis Processing Units Consumption Graph:</strong></p><p>Below are the processing units being used during our live data feed and data analytics processing.</p><p>An in-application data stream is an entity that continuously stores data in your application for you to perform processing. Your applications continuously writes to and reads from in-application data streams. Kinesis Data Analytics elastically scales your application to accommodate the data throughput of your source stream and your query complexity for most scenarios. Kinesis Data Analytics provisions capacity in the form of Amazon Kinesis Processing Units (KPU). One KPU provides you with 1 vCPU and 4GB memory.</p><p>You are charged an hourly rate based on the number of Amazon Kinesis Processing Units (or KPUs) used to run your streaming application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KZNg0y6nU-jPQVbn" /><figcaption>Amazon CloudWatch Analysis on Compute Units used by Streams</figcaption></figure><p><strong>Step 4: Store Processed Data to S3 (Processed)</strong></p><p>Destination of processed data by Step-3 (Kinesis Data Analytics App), We have created a Kinesis Firehose Stream destination with the configuration of S3 bucket, where files will be placed with timeStamp folder format. Files are stored in CSV format automatically by the Kinesis Firehose stream.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RUWv3ICWpSfwg3pl" /><figcaption>Configured Amazon Kinesis Firehose Stream with S3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DkZD-HnKjvbDgh7-" /><figcaption>Kinesis Firehose Setup Console (Demo Data to S3)</figcaption></figure><p>Destination Processed Data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xSQq8NfeykqS_jwb" /><figcaption>Amazon S3 Bucket for Processed Files</figcaption></figure><p>Sample Values of processed data:</p><blockquote><em>DemoSensor,14,0.0</em></blockquote><blockquote><em>DemoSensor,10,0.0</em></blockquote><blockquote><em>DemoSensor,100,3.0</em></blockquote><blockquote><em>DemoSensor,20,0.0</em></blockquote><p><strong>Step 5: Show the Visualization of S3(Processed)</strong></p><p>Created an Amazon QuickSight account with the free package, In free 1 GB storage of QuickSight, there will be SPICE we need to import data in it in order to visualize the data on graphs and metrics visuals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/0*b5DO28zocjDKmUzH" /></figure><p>To create visual below are the steps:</p><ol><li>Created Data Analysis to the AWS S3 Bucket (Processed Data), which will be having data with scored anomalies of temperature records by Random_Cut_Forest.</li><li>Once the data set is created in QuickSight, the columns will be loaded into the SPICE.</li><li>Select the columns with appropriate dimensions and measures ( X-axis &amp; Y-axis ), It will visualize the data to graphs. You can select the visuals as per your choice and report format needed. I-e Donut Chart or Pie Chart.</li></ol><p>Analysis Report on Amazon QuickSight:</p><ul><li><strong>Column-2<br></strong>This column shows the temperature ranges from 0 to 120, where the normal temperature is between 10–20.</li><li><strong>Column-3<br></strong>It is the anomaly detection score by the Random_Cut_Forest ML algorithm. This stats demonstrate that for example, <strong>107 </strong>is the anomaly detected in the system and algorithm scored this count <strong>2</strong>, which is its truth precision</li></ul><h3>FAQs | Important Questions</h3><p><strong>How many virtual sensors were used in this article?</strong></p><p>There was only one IoT sensor simulation used, the simulation was done in a python script, which was used to send the raw temperature data exactly the same way the temperature sensor can be sent to streams or any data source.</p><p><strong>How much temperature data is generated?</strong></p><p>More than <strong>38000 </strong>temperature samples data were generated by the python program and ingested to Kinesis stream, which gets processed by Kinesis Data Analytic Application.</p><p>Below is the snapshot of the dataset processed, viewed in Amazon QuickSight.</p><p><strong>How much were the processing time and memory usage saved?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/0*h0dsUfj_CSdMMYTa" /></figure><p>A single KPU was used as a unit of stream processing capacity consisting of <strong>1 vCPU </strong>compute and <strong>4 GB memory</strong>. We have used 1 kinesis shard in our source data stream for which the raw data retention period was 24 hours.</p><p><strong>At which level do we detect anomaly data?</strong></p><p>When data ingestion is completed to Kinesis Data Stream from the Python Simulation program, just after the ingestion Kinesis Data Analytic Application calculates the random cut forest calculation on the live data in real-time. So the application itself detects the anomaly in its IN-APPLICATION stream “TEMP_STREAM”, where each real-time temperature sample record will contain an anomaly score, so the highest the amount of anomaly score will demonstrate the high chances of its anomaly in the data.</p><p>So, Anomaly is detected before the data arrives at the target (S3 Processed).</p><p><strong>Are detected anomalies stored in S3?</strong></p><p>Yes, processed data is being stored in the Processed S3 Bucket.</p><p>Kinesis Data Analytics Application destination has been configured with kinesis firehose, which sends the data to the S3 bucket in a timestamp folder structure.</p><p><strong>What happens to the detected data?</strong></p><p>Random Cut Forest algorithm calculates each temperature record with an Anomaly<em> </em><strong><em>Score </em></strong>figure, High score number indicates the high accuracy of the value to be described as Anomaly. There comes an extension of the anomaly score column to the raw data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e014a23350e3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloud-computing-aws-faizan-rafiq/streaming-data-analytics-using-kinesis-streams-for-anomaly-detection-via-the-random-forest-e014a23350e3">Streaming Data Analytics Using Kinesis Streams for Anomaly Detection via the Random Forest…</a> was originally published in <a href="https://medium.com/cloud-computing-aws-faizan-rafiq">Cloud Computing | AWS | Faizan Rafiq</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>