<?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 Rakesh M K on Medium]]></title>
        <description><![CDATA[Stories by Rakesh M K on Medium]]></description>
        <link>https://medium.com/@mkk.rakesh?source=rss-5796472804b9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lHNT-wptuVJOpk35SbLLSw.jpeg</url>
            <title>Stories by Rakesh M K on Medium</title>
            <link>https://medium.com/@mkk.rakesh?source=rss-5796472804b9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 09:43:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mkk.rakesh/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[Zero-Shot Probabilistic Time Series Forecasting using CHRONOS.]]></title>
            <link>https://medium.com/write-a-catalyst/zero-shot-time-series-forecasting-using-chronos-ad99240c8117?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad99240c8117</guid>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[probabilistic-forecast]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[time-series-analysis]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Wed, 01 May 2024 09:57:26 GMT</pubDate>
            <atom:updated>2024-05-02T14:05:41.983Z</atom:updated>
            <content:encoded><![CDATA[<h4>Exploring Amazon’s pretrained probabilistic forecasting model, CHRONOS with minimal code.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e9aXnL3_44_GdMoj" /><figcaption>Photo by <a href="https://unsplash.com/@m_malkovich?utm_source=medium&amp;utm_medium=referral">petr sidorov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>About CHRONOS</h4><p>CHRONOS is a language modeling framework for time series which is introduced by Amazon in early 2024. The probabilistic model treats the time series as a sequence and uses Google AI’s <strong>T5 model (text to text transfer transformer) </strong>for training<strong>. </strong>The zero-shot forecasting model boasts superior performance over classical statistical forecasting models and specialized deep learning models after a comprehensive evaluation on more that 40 datasets. <strong>Zero-shot forecasting</strong> refers to the<em> ability of models to generate to forecasts from unseen datasets</em>. This is obtained by using synthetic data along with real data to train CHRONOS models as per the original paper [1].</p><h4>How do CHRONOS models work ?</h4><p>First of all, the time stamps (hourly, daily, monthly etc..) are not taken into account and the entire values are considered as tokens. The time series values are tokenized by scaling (mean scaling) and quantizing (binning/grouping values) into a fixed vocabulary, followed by training a pretrained language model (T5).</p><p>For deeper understanding, refer to the image below and go through the paper [1].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*xP4Ljgff7x1oq7zyyKOIBw.png" /><figcaption>Image taken from <a href="https://arxiv.org/pdf/2403.07815">https://arxiv.org/pdf/2403.07815</a></figcaption></figure><h4>Forecasting using CHRONOS</h4><p>To work with CHRONOS, California daily female birth dataset(1959) with 365 samples is used. To access the data, visit below given link.(<a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv"><em>https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv</em></a><em> </em>)</p><ol><li><strong>To begin with, install prerequisites from github.</strong></li></ol><p>!pip install git+https://github.com/amazon-science/chronos-forecasting.git</p><p><strong>2. Import necessary libraries including Chronos Pipeline.</strong></p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>import pandas as pd<br>import torch  # PyTorch to convert values ino torch tensors.<br>from chronos import ChronosPipeline</pre><p><strong>3. Choose Model and initialize Pipeline.</strong></p><p>There are 5 models available with CHRONOS namely <strong>tiny, mini, small, base and large</strong> with increasing size and number of parameters. CHRONOS ‘<strong>tiny’</strong> model is chosen here since dataset is small and for faster response. device_map can be chosen appropriately as ‘cpu’ , ‘auto’, ‘cuda’ etc.. The pipeline is initialized as follows.</p><pre>&#39;&#39;&#39;initialize CHRONOS pipeline&#39;&#39;&#39;<br>pipeline = ChronosPipeline.from_pretrained(&quot;amazon/chronos-t5-tiny&quot;,<br>                                            device_map = &quot;auto&quot;,<br>                                            torch_dtype = torch.bfloat16)</pre><p><strong>4. Import and split the data</strong></p><pre>series = pd.read_csv(&quot;/content/femalebirthCali.csv&quot;) # import data<br>train = series.head(320) # train data<br>test = series.tail(45) # test data</pre><p><strong>5. Define variable and forecasting steps/horizon</strong></p><p>Forecasting ‘Births’ in the train dataframe for 45 steps (horizon).</p><pre>to_predict = torch.tensor(train[&#39;Births&#39;])# variable for training and predict<br>horizon = 45 # forecasting steps</pre><p><strong>6. Forecast using CHRONOS</strong></p><p>Forecast for a step of 45 using CHRONOS pipeline’s predict method.</p><pre>forecast = pipeline.predict(to_predict, horizon)</pre><p><strong>7. Visualize forecast</strong></p><p>Since CHRONOS does probabilistic forecast, the predicted values will be in the form of a dense tensor. For better understanding and visualization, median (50th percentile), upper bound (90th percentile), lower bound (10th percentile) are extracted from the forecasted vaues as below and plotted.</p><pre>up, median, low = np.quantile(forecast[0].numpy(),[0.1,0.5,0.9], axis = 0)<br><br>&#39;&#39;&#39;plot the forecat&#39;&#39;&#39;<br>plt.subplots(figsize=(10,3))<br>plt.plot(train.Births, label = &#39;train data&#39;)<br>plt.plot(test.Births, label = &#39;test data&#39;)<br>plt.plot(np.arange(320,365),median, label = &#39;median forecast&#39;)<br>plt.legend()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mjWxnIOYnVWn-wTLOMuyEA.png" /><figcaption>Train, test, and forecast</figcaption></figure><p><strong>8. Plot forecast with bounds</strong></p><pre>plt.subplots(figsize=(7,3))<br>plt.plot(median, label = &#39;forecast&#39;)<br>plt.plot(list(test[&#39;Births&#39;]), label = &#39;train data&#39;)<br>plt.fill_between(range(0,45), low, up, alpha=0.3, label=&quot;Bounds&quot;)<br>plt.legend()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*qVbWL5I04GLyOGohIBRzqg.png" /><figcaption>Forecast with 10–90 percentile bound</figcaption></figure><h4>Conclusion</h4><p>The forecast looks promising with the CHRONOS <strong>‘tiny’</strong> model. This page just aims to explain fundamental workings and demonstrate how the model can be accessed with ease. Performance evaluation can be done by comparing with other models by choosing metrics such as MAE , MSE etc.. It is strongly recommended to go through the base paper [1] for better understanding of the model architecture.</p><h4>References</h4><ol><li><a href="https://arxiv.org/pdf/2403.07815">https://arxiv.org/pdf/2403.07815</a></li><li><a href="https://machinelearningmastery.com/time-series-datasets-for-machine-learning/">https://machinelearningmastery.com/time-series-datasets-for-machine-learning/</a></li><li><a href="https://www.youtube.com/watch?v=WxazoCVkBhg">https://www.youtube.com/watch?v=WxazoCVkBhg</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad99240c8117" width="1" height="1" alt=""><hr><p><a href="https://medium.com/write-a-catalyst/zero-shot-time-series-forecasting-using-chronos-ad99240c8117">Zero-Shot Probabilistic Time Series Forecasting using CHRONOS.</a> was originally published in <a href="https://medium.com/write-a-catalyst">Write A Catalyst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sir. Issac Newton Beyond Gravity]]></title>
            <link>https://medium.com/write-a-catalyst/sir-issac-newton-beyond-gravity-000705f5c125?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/000705f5c125</guid>
            <category><![CDATA[facts-and-myths]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[facts]]></category>
            <category><![CDATA[newton]]></category>
            <category><![CDATA[biography]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Thu, 15 Feb 2024 18:09:18 GMT</pubDate>
            <atom:updated>2024-02-15T18:09:18.653Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>“Tact is the art of making a point without making an enemy.” — Sir. Issac Newton</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cEMiFV-EHAqZX8HZeZnT4Q.jpeg" /><figcaption>Image created with Bing Image Creator</figcaption></figure><p>Arguably one of the greatest scientists of all time, Let’s see some interesting facts about Sir. Issac Newton beyond his theories and findings about Universal Gravity, Optics and Laws of Motion.</p><h4><strong>No Apple fell on Newton’s head</strong>:</h4><p>We all have heard this story that young Newton was sitting under an apple tree and the <strong>‘eureka moment’ </strong>where<strong> </strong>the<strong> </strong>apple fell on his head triggered him to come up with the idea of gravity. However, there is no proof that it ever happened. In his biography “Memoirs of Sir Isaac Newton’s Life”, William Stukeley wrote that during a conversation with Sir. Issac Newton, the legend mentioned that the memory of an apple falling sparked the idea of gravity in his mind. But anyway, whether the apple story is a myth or not, it has provided humanity with a fundamental understanding of gravity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*8LwKnXL45R3t8IEWjjWGEg.png" /><figcaption>Source: <a href="https://www.twinkl.co.in/">https://www.twinkl.co.in/</a></figcaption></figure><h4><strong>In search of Philosopher’s stone:</strong></h4><p>It is clear from his manuscripts found in his personally library in 17th century that he used to practice <strong>Alchemy </strong>which was the forerunner of chemistry. The ultimate goal of alchemy was to find out the precise proportion of mercury, Sulphur and salt which will yield a mythical substance called ‘philosopher’s stone’, gifted with supernatural powers like <strong>transforming base metals to gold</strong>, <strong>cure illness, providing rejuvenation and immortality.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jJ7OC0ZivbAnlIzTAYVIxQ.jpeg" /><figcaption>Image source: <a href="https://tiredmummyoftwo.co.uk/">https://tiredmummyoftwo.co.uk/</a></figcaption></figure><p>It has been said that Newton had more interest towards alchemy, as he wrote and spent more time on this than physics and mathematics. Also, he never published his writings about Alchemy since he feared that they might undergo scrutiny and criticism.</p><h4><strong>Unconventional Devotee:</strong></h4><p>Even though Sir. Issac Newton was a devoted Christian his views were far from the orthodox, particularly in the <strong>doctrine of the Trinity</strong>. Newton’s inclination towards Arianism and the rejection of doctrine of trinity, have led scholars to categorize him as a <strong>Non-Trinitarian Arian.</strong></p><h4><strong>Project ‘Temple of Solomon:</strong></h4><p>Newton believed that the key to <strong>God’s Design of Universe </strong>is held by Solomon’s Temple. In this project he did detail study and analysis of the temple to measure the dimensions of each room [1] with an intuition that Bible is filled with hidden codes than can be cracked only by wise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*oxVgrOUlDM1TT_dCwVq1fQ.png" /><figcaption>Image Source: <a href="https://www.newtonproject.ox.ac.uk/">https://www.newtonproject.ox.ac.uk/</a></figcaption></figure><p>Apart from these, Newton has extreme <strong>affinity towards Mercury</strong> which might have arisen from the idea of Alchemy. His habit of intaking mercury vapor and even drinking it is well documented during the exhumation of his body. There was high level of mercury present in his hair which is even considered as the potential cause of his mental illness and death in later stage of his life [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*diGXQsGJddrdYpomn2xc-A.jpeg" /><figcaption>Source: <a href="https://www.azquotes.com/quote/213687">https://www.azquotes.com/quote/213687</a></figcaption></figure><h4>References</h4><ol><li><a href="https://www.newtonproject.ox.ac.uk/view/texts/normalized/THEM00190">Chapter 5: A Description of the Temple of Solomon (Normalized) (ox.ac.uk)</a></li><li><a href="https://www.jstor.org/stable/531510">https://www.jstor.org/stable/531510</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=000705f5c125" width="1" height="1" alt=""><hr><p><a href="https://medium.com/write-a-catalyst/sir-issac-newton-beyond-gravity-000705f5c125">Sir. Issac Newton Beyond Gravity</a> was originally published in <a href="https://medium.com/write-a-catalyst">Write A Catalyst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Text from Images with Google’s Gemini Pro Vision.]]></title>
            <link>https://medium.com/write-a-catalyst/poems-from-images-with-googles-gemini-pro-vision-e5d45be9fd2c?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5d45be9fd2c</guid>
            <category><![CDATA[poem]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[multimodal]]></category>
            <category><![CDATA[google-gemini]]></category>
            <category><![CDATA[image-to-text]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Mon, 05 Feb 2024 18:49:05 GMT</pubDate>
            <atom:updated>2024-02-11T14:22:31.266Z</atom:updated>
            <content:encoded><![CDATA[<h4>Developement of a webapp to generate poems from images leveraging Google’s Multimodal LLM, Gemini Pro Vision and Streamlit.</h4><p>(<em>Disclaimer: This page is solely to show the capability of Google’s Multimodal LLM, Gemini Pro Vision. AI generated contents are not suitable for publication</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*Dc0Cve_00yVBI6HNXpRP0g.png" /><figcaption>Painiting by Author.</figcaption></figure><p>Assuming that readers are well aware about Google’s <strong>S</strong>tate-<strong>O</strong>f-<strong>T</strong>he-<strong>A</strong>rt Multimodal LLM Gemini and its variants, this page intends to demonstrate how to make a simple webapp in Streamlit framework using Gemini Pro Vison model with few lines of code.</p><blockquote>It is recommended to create a seperate environment to avoid conflicts among libraries.</blockquote><h4>Step-1: Get your Gemini API key</h4><p>Visit <a href="https://makersuite.google.com/app/prompts/new_freeform">https://makersuite.google.com/app/prompts/new_freeform</a>. Find and click on button <strong>Get API key</strong> on top left corner. It will navigate to new page as in below snap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g6Pg5uoV_hTleEm_YoDBUQ.png" /></figure><p>Ckick on <strong>Create API key in new oroject </strong>and generate the key. (Do not expose the API key. It may get expaired)</p><h4>Step-2: Create a .env file</h4><p>Create a .env file and paste the copied generated API key as GOOGLE_API_KEY= ‘genetated api key‘ . (The API key can be passeed directly also)</p><h4>Step-3: Create requirements.txt file</h4><p>Following libraries need to be specified in requirement.txt</p><pre>&#39;&#39;&#39;requirements.txt&#39;&#39;&#39;<br>streamlit<br>python-dotenv<br>google-generativeai</pre><h4>Step-4: Install requirements</h4><p>Open terminal and run pip install -r requirements.txt . If your IDE is Jupyter notebook or Google Colab, you can run the command in the cell itself as !pip install -r requirements.txt .</p><h4>Step-5: Prepare script</h4><p>Import libraries:</p><pre>&#39;&#39;&#39;import genai module to access models&#39;&#39;&#39;<br><br>import google.generativeai as genai<br><br>&#39;&#39;&#39;import load_dotenv to load environment variables from .env file&#39;&#39;&#39;<br>from dotenv import load_dotenv<br><br>&#39;&#39;&#39;load environment varibles (GOOGLE_API_KEY)&#39;&#39;&#39;<br>load_dotenv() <br><br>import streamlit as st<br><br>&#39;&#39;&#39;Import image to load the image to generate content&#39;&#39;&#39;<br>from PIL import Image<br>import os</pre><h4>Step-6: Configure the generative model</h4><pre>genai.configure(api_key = os.getenv(&quot;GOOGLE_API_KEY&quot;))<br>genModel = genai.GenerativeModel(&#39;gemini-pro-vision&#39;)</pre><h4>Step-7: Define a function to generate and extarct content</h4><pre>def geniniResponse(prompt, image):<br>    response = genModel.generate_content([prompt, image])<br>    return response.text</pre><p>prompt is the string passed to the model specifying its task.</p><h4>Step-8: Set up streamlit page</h4><pre>&#39;&#39;&#39;Title for webapp&#39;&#39;&#39;<br>st.title(&#39;Gemini Poem Generator&#39;)<br><br>&#39;&#39;&#39;Upload Image&#39;&#39;&#39;<br>imageUploaded = st.file_uploader(&quot;Upload content image...&quot;)<br><br>&#39;&#39;&#39;initialise image variable&#39;&#39;&#39;<br>image = &#39;&#39; # Dont assign as None.. It may throw error<br><br>&#39;&#39;&#39;Display image&#39;&#39;&#39;<br>if imageUploaded is not None:<br> <br>    image = Image.open(imageUploaded)<br>    st.image(image, caption= &quot;Content Image&quot;, use_column_width = True)<br>    #use_column_width = True:To fit the image in the available column width<br><br>&#39;&#39;&#39;Add a click button to generate the content&#39;&#39;&#39;<br>generate = st.button(&quot;Generate content&quot;)</pre><p><strong>Prepare prompt:</strong></p><blockquote><strong>The quality of the model response relies on how refined the prompt is. It can be instructed to generate any content from the image such as description, poem, story, summary etc. with proper refined prompt.</strong></blockquote><pre>#write a prompt as per your requirement<br>prompt = &#39;&#39;&#39;You are an experienced,  world class poet. <br>I will upload an image and based on the uploaded imge, <br>you have to generate a  poem.&#39;&#39;&#39;</pre><p><strong>Display content:</strong></p><p>Once the Image is uploaded and generate button is clicked, the response will be displayed in the webapp with the help of below code.</p><pre>if submit:<br>&#39;&#39;&#39;get response from the model&#39;&#39;&#39;<br>    response = geminiResponse(image,prompt)<br>&#39;&#39;&#39;write text content on web page&#39;&#39;&#39;<br>    st.write(response)</pre><h4>Step-9: Wrap the codes and deploy the App</h4><p>Full code (Omitting comments): The file extension should be .py .</p><pre>&#39;&#39;&#39;app.py&#39;&#39;&#39;<br><br>import google.generativeai as genai<br>import streamlit as st<br>from PIL import Image<br>import os<br>from dotenv import load_dotenv<br>load_dotenv()<br><br>genai.configure(api_key = os.getenv(&quot;GOOGLE_API_KEY&quot;))<br>genModel = genai.GenerativeModel(&#39;gemini-pro-vision&#39;)<br><br>def geniniResponse(prompt, image):<br>    response = genModel.generate_content([prompt, image])<br>    return response.text<br><br>st.title(&#39;Gemini Poem Generator&#39;)<br>imageUploaded = st.file_uploader(&quot;Upload content image...&quot;)<br>image = &#39;&#39; <br><br>if imageUploaded is not None:<br>    image = Image.open(imageUploaded)<br>    st.image(image,caption=&quot;Uploaded Content Image&quot;,use_column_width=True)<br>generate = st.button(&quot;Generate content&quot;)<br><br>prompt1 = &#39;&#39;&#39;You a veteran  poet. I will upload an image <br>and based on the uploaded imge, you have to generate a  poem in English&#39;&#39;&#39;<br><br>prompt2 = &#39;&#39;&#39;You are an experienced,  world class poet. <br>I will upload an image and based on the uploaded imge, <br>you have to generate a  poem.&#39;&#39;&#39;<br><br>if generate:<br>    response = geminiResponse(image,prompt1)<br>    st.write(response)</pre><p>Deploy the App:</p><p>Run the app.py and make sure that it is error free. Go to teminal and run streamlit run app.py . In conda enviroment run the same command in powershell from whre the file is saved. It will redirect to the streamlit app in your default browser as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2f1mFWmo1SuYSDN0bQdHKA.png" /><figcaption>Deployed Streamlit App.</figcaption></figure><h4>Step-10: Upload image and generate content</h4><p>Upload some images to the model and generate content (poem)</p><p>Uploading cover image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*ZS2o0cRCrt_kvC7XSsQTDg.png" /></figure><p>I personally can’t say that it’s a great poem. Maybe my prompt was not that great. Let’s try with another image. (<em>Image by Edward Hopper, Two Comedians (1966). Image courtesy of Sotheby’s.</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*if4FezeT5yjGf0X1d9-FXA.png" /></figure><p>Above response looks better. Finally trying with a random image found on internet (<em>Source: </em><a href="https://www.istockphoto.com/"><em>https://www.istockphoto.com/</em></a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*iB5VWszsSMCE--h5SC1e9g.png" /></figure><h4>Summary</h4><p>I hope this page well explains the capability of Google’s trending Multimodal Gemini Pro Vision. It has done a wonderful job as <strong>image to text model</strong>. The response of the model can be more precise with a more keenly written prompt. The length, tone and other preferred features of the content also can be specified in the prompt which also highlight the importance of prompt engineering while dealing with LLMs.</p><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5d45be9fd2c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/write-a-catalyst/poems-from-images-with-googles-gemini-pro-vision-e5d45be9fd2c">Text from Images with Google’s Gemini Pro Vision.</a> was originally published in <a href="https://medium.com/write-a-catalyst">Write A Catalyst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Schrödinger’s Cat and Quantum Paradox.]]></title>
            <link>https://medium.com/@mkk.rakesh/schr%C3%B6dingers-cat-and-quantum-paradox-20d8f55ea8bc?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/20d8f55ea8bc</guid>
            <category><![CDATA[quantum-superposition]]></category>
            <category><![CDATA[copenhagen-interpretation]]></category>
            <category><![CDATA[quantum-mechanics]]></category>
            <category><![CDATA[schrodingers-cat]]></category>
            <category><![CDATA[quantum-physics]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Sun, 04 Feb 2024 12:41:31 GMT</pubDate>
            <atom:updated>2024-02-04T12:41:31.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q9XHZM03_u75SimHg0X6Tg.jpeg" /><figcaption>Image created using Bing Image creator.</figcaption></figure><p>To start with a paradox, this most decorated cat never existed. Also, it didn’t belong to Schrodinger. The Nobel Prize winning physicist Erwin Schrödinger introduced his famous thought experiment known as<strong> ‘Schrödinger’s cat’</strong> within the framework of the <strong>Copenhagen Interpretation </strong>in<strong> </strong>1935 to illustrate the concept of superposition in quantum mechanics.</p><p><strong>Copenhagen Interpretation </strong>is a collection of ideas developed in the field of quantum mechanics based on the works of Neils Bohr, Werner Heisenberg and others. Schrodinger Introduced his hypothetical cat experiment to provide insights into the <strong>superposition</strong> nature of quantum particles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*xkMB3t8I5IDH6cVf5WHCHg.jpeg" /><figcaption>Ervin Schrodinger (last row, middle) with other iconic scientists in Brussels 1927. Image source: <a href="https://i.pinimg.com/">i.pinimg.com</a></figcaption></figure><h4>The Schrodinger’s cat experiment</h4><p>As per the hypothetical experiment, a cat is placed in a sealed box with a radioactive material, a Geiger counter, a hammer and a bottle of poison. A Geiger counter is a device to detect ionizing radiation and produce some response. If the counter detects radiation, the hammer will be triggered to break the bottle of poison which will lead to death of the cat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*7MGYKhSAQVT1G9wg9mBlhw.png" /><figcaption>Schrodinger’s cat experiment</figcaption></figure><p>According to the principles of quantum mechanics, until the box is opened and observed the cat’s condition, the <strong>cat’s state is in superposition</strong>. i.e. the cat is considered to be both<strong> dead and alive </strong>at the same time or in other words, the cat is in <strong>multiple states at same time</strong>.</p><p>The concept of the cat’s state in Schrödinger’s thought experiment can be extended to quantum particles, illustrating that a quantum particle can exist in multiple states simultaneously. The determination of the particle’s state becomes defined only upon observation, reflecting the principle of superposition in quantum mechanics.</p><h4>Summary</h4><p>Through his thought experiment Schrodinger’s intention was to highlight the paradoxes and counterintuitive nature of quantum mechanics which are often puzzling. While the strange behavior of quantum particles contradicts with classical physics and challenges our understanding of reality, in a microscopic world of quantum physics, things are even more perplexing and complex.</p><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20d8f55ea8bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Time Series Forecasting using AutoGluon]]></title>
            <link>https://medium.com/@mkk.rakesh/time-series-forecasting-using-autogluon-34f8780b8214?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/34f8780b8214</guid>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[autogluon]]></category>
            <category><![CDATA[automl]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Fri, 02 Feb 2024 08:24:33 GMT</pubDate>
            <atom:updated>2024-02-02T09:15:40.797Z</atom:updated>
            <content:encoded><![CDATA[<h3>Time Series Forecasting using AutoGluon.</h3><h4>Introduction to Amazon’s Multimodal AutoML library with a time sereis forecasting problem.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s2g3NfHctwa_vePyt6n1JA.png" /><figcaption>Source: <a href="https://auto.gluon.ai/stable/tutorials/multimodal/index.html">https://auto.gluon.ai/stable/tutorials/multimodal/index.html</a></figcaption></figure><h4>AutoGluon.</h4><p>AutoGluon is an open-source Multimodal python library for AutoML, launched by Amazon. Built on top of PyTorch, the library makes use of <strong>State-Of-The-Art (SOTA)</strong> models to achieve the best performing models for various machine learning problems . Equiped with SOTA Deep Learning models, AutoGluon offers solutions for problems such as Image classification, Object detection, Text prediction, Image segmentation, Time Series forecasting and much more as in the cover image. This page intends to show the readers how to make use of AutoGluon for a a time series forecasting task by considering the well-known Airlines Dataset with <strong>minimal code</strong>.</p><h4>Install and import AutoGluon.</h4><p>It is advised to create a new python environment for Autogluon experiments to avoid conflicts with other libraries. Refer to <a href="https://auto.gluon.ai/stable/install.html">[1]</a> for more details. Once new environment is created install Autogluon by running command given below.</p><p>pip install autogluon <br>import autogluon</p><h4>Import Timeseries Predictor.</h4><p>AutoGluon TimeSeriesPredictor expects data in a specific format with multi-index. One index has to be the timestamp and the other should be a unique id (it can be any value). A normal pandas dataframe can be converted to this format with the help of TimeSeriesDataFrame.from_data_frame().</p><pre>from autogluon.timeseries import TimeSeriesPredictor, TimeSeriesDataFrame</pre><h4>Import Data</h4><pre>import pandas as pd<br><br>data = pd.read_excel(&#39;airlinesData.xlsx&#39;)<br>data[&#39;Month&#39;] = pd.to_datetime(data[&#39;Month&#39;])  #convert to datetime<br>id = [&#39;airline&#39;] * len(data) #create list of id to add to dataframe<br>data[&#39;id&#39;] = id #add new colum &#39;id&#39; to dataframe<br>data.head()()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*6UW4hehbvmdP4zNzPfcHBg.png" /></figure><h4>Convert Data to Timeseries DataFrame.</h4><pre>data = TimeSeriesDataFrame.from_data_frame(<br>    data,<br>    id_column=&quot;id&quot;,<br>    timestamp_column=&quot;Month&quot;<br>)<br>data.tail()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*_dc1RTRKAMiz8NhO3Js3KA.png" /></figure><h4>Split data into train and test.</h4><pre>data.shape<br>(96, 3)<br>data.columns<br>Index([&#39;Passengers&#39;], dtype=&#39;object&#39;)<br>&#39;&#39;&#39;split data into train and test&#39;&#39;&#39;<br>train = data.head(77)<br>test = data.tail(19)</pre><h4>Initialize and train the Predictor.</h4><p>The evaluation metric chosen depends on whether you are in need for probabilistic forecast or point forecast. A table containing different metrics is shown below but I strongly recommend you refer <a href="https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-metrics.html">[2]</a> for better understanding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*lt0sKOp2kCHM0lC1tHacIg.png" /><figcaption>Source: <a href="https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-metrics.html">Forecasting Time Series — Evaluation Metrics — AutoGluon 1.0.0 documentation</a></figcaption></figure><p>Different models trained on train data, parameter chosen and evaluation metric can be seen in the <strong>training log</strong>. The library leverages statistical models and SOTA Deep Learning models for training. By default, a WeightedEnsemble model also will be tried by the model. it can be disabled by setting enable_ensemble = Falsein .fit() method.</p><pre>predictor = TimeSeriesPredictor(target=&#39;Passengers&#39;, <br>                                prediction_length=19,<br>                                 eval_metric=&quot;MASE&quot;,).fit(train)</pre><pre>No path specified. Models will be saved in: &quot;AutogluonModels\ag-20240202_034220&quot;<br>Beginning AutoGluon training...<br>AutoGluon will save models to &#39;AutogluonModels\ag-20240202_034220&#39;<br>=================== System Info ===================<br>AutoGluon Version:  1.0.0<br>Python Version:     3.8.18<br>Operating System:   Windows<br>Platform Machine:   AMD64<br>Platform Version:   10.0.22621<br>CPU Count:          12<br>GPU Count:          0<br>Memory Avail:       0.59 GB / 7.33 GB (8.0%)<br>Disk Space Avail:   262.60 GB / 476.08 GB (55.2%)<br>===================================================<br><br>Fitting with arguments:<br>{&#39;enable_ensemble&#39;: True,<br> &#39;eval_metric&#39;: MASE,<br> &#39;hyperparameters&#39;: &#39;default&#39;,<br> &#39;known_covariates_names&#39;: [],<br> &#39;num_val_windows&#39;: 1,<br> &#39;prediction_length&#39;: 19,<br> &#39;quantile_levels&#39;: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],<br> &#39;random_seed&#39;: 123,<br> &#39;refit_every_n_windows&#39;: 1,<br> &#39;refit_full&#39;: False,<br> &#39;target&#39;: &#39;Passengers&#39;,<br> &#39;verbosity&#39;: 2}<br><br>Inferred time series frequency: &#39;MS&#39;<br>Provided train_data has 77 rows, 1 time series. Median time series length is 77 (min=77, max=77). <br><br>Provided dataset contains following columns:<br>	target:           &#39;Passengers&#39;<br><br>AutoGluon will gauge predictive performance using evaluation metric: &#39;MASE&#39;<br>	This metric&#39;s sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.<br>===================================================<br><br>Starting training. Start time is 2024-02-02 09:12:20<br>Models that will be trained: [&#39;SeasonalNaive&#39;, &#39;CrostonSBA&#39;, &#39;NPTS&#39;, &#39;AutoETS&#39;, &#39;DynamicOptimizedTheta&#39;, &#39;AutoARIMA&#39;, &#39;RecursiveTabular&#39;, &#39;DirectTabular&#39;, &#39;DeepAR&#39;, &#39;TemporalFusionTransformer&#39;, &#39;PatchTST&#39;]<br>Training timeseries model SeasonalNaive. <br>	-0.8728       = Validation score (-MASE)<br>	0.02    s     = Training runtime<br>	6.75    s     = Validation (prediction) runtime<br>Training timeseries model CrostonSBA. <br>	-1.4745       = Validation score (-MASE)<br>	0.00    s     = Training runtime<br>	15.24   s     = Validation (prediction) runtime<br>Training timeseries model NPTS. <br>	-2.3546       = Validation score (-MASE)<br>	0.02    s     = Training runtime<br>	2.85    s     = Validation (prediction) runtime<br>Training timeseries model AutoETS. <br>	-0.8152       = Validation score (-MASE)<br>	0.02    s     = Training runtime<br>	30.67   s     = Validation (prediction) runtime<br>Training timeseries model DynamicOptimizedTheta. <br>	-1.4460       = Validation score (-MASE)<br>	0.02    s     = Training runtime<br>	29.97   s     = Validation (prediction) runtime<br>Training timeseries model AutoARIMA. <br>	-0.7476       = Validation score (-MASE)<br>	0.02    s     = Training runtime<br>	21.59   s     = Validation (prediction) runtime<br>Training timeseries model RecursiveTabular. <br>	-0.5560       = Validation score (-MASE)<br>	4.89    s     = Training runtime<br>	0.38    s     = Validation (prediction) runtime<br>Training timeseries model DirectTabular. <br>	-3.5581       = Validation score (-MASE)<br>	0.65    s     = Training runtime<br>	0.08    s     = Validation (prediction) runtime<br>Training timeseries model DeepAR. <br>	-1.1489       = Validation score (-MASE)<br>	55.81   s     = Training runtime<br>	0.08    s     = Validation (prediction) runtime<br>Training timeseries model TemporalFusionTransformer. <br>	-0.8554       = Validation score (-MASE)<br>	144.14  s     = Training runtime<br>	0.02    s     = Validation (prediction) runtime<br>Training timeseries model PatchTST. <br>	-0.9520       = Validation score (-MASE)<br>	32.19   s     = Training runtime<br>	0.03    s     = Validation (prediction) runtime<br>Fitting simple weighted ensemble.<br>	Ensemble weights: {&#39;AutoARIMA&#39;: 0.09, &#39;NPTS&#39;: 0.07, &#39;RecursiveTabular&#39;: 0.04, &#39;SeasonalNaive&#39;: 0.35, &#39;TemporalFusionTransformer&#39;: 0.44}<br>	-0.2127       = Validation score (-MASE)<br>	1.67    s     = Training runtime<br>	31.59   s     = Validation (prediction) runtime<br>Training complete. Models trained: [&#39;SeasonalNaive&#39;, &#39;CrostonSBA&#39;, &#39;NPTS&#39;, &#39;AutoETS&#39;, &#39;DynamicOptimizedTheta&#39;, &#39;AutoARIMA&#39;, &#39;RecursiveTabular&#39;, &#39;DirectTabular&#39;, &#39;DeepAR&#39;, &#39;TemporalFusionTransformer&#39;, &#39;PatchTST&#39;, &#39;WeightedEnsemble&#39;]<br>Total runtime: 347.54 s<br>Best model: WeightedEnsemble<br>Best model score: -0.2127</pre><h4>To check the leaderboard</h4><pre>predictor.leaderboard()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*kGmIORE-qbAQRbQc2IuCTw.png" /></figure><h4>Prediction.</h4><p>Without specifying a model, the predictor will consider best model for prediction which is WeightedEnsemble in this case.</p><pre>predictions = predictor.predict(train)<br>predictions </pre><pre>Model not specified in predict, will default to the model with the best validation score: WeightedEnsemble</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*PdL2tevvVh3hPk55IymKEg.png" /></figure><p>By default, AutoGluon predicts quantile levels [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]. To predict a different set of quantiles, you can use quantile_levels arguments as:</p><p>predictor <strong>=</strong> TimeSeriesPredictor<strong>(</strong>eval_metric<strong>=</strong>”WQL”<strong>,</strong>quantile_levels<strong>=[0.1,</strong> <strong>0.5,</strong> <strong>0.75,</strong> <strong>0.9]) </strong>[2].</p><h4>Plot predictions.</h4><pre>import matplotlib.pyplot as plt<br><br>plt.figure(figsize=(20, 3))<br><br>item_id = &quot;airline&quot;<br>y_past = train.loc[item_id][&quot;Passengers&quot;]<br>y_pred = predictions.loc[item_id]<br>y_test = test.loc[item_id][&quot;Passengers&quot;]<br><br>plt.plot(y_past, label=&quot;Past time series values&quot;)<br>plt.plot(y_pred[&quot;mean&quot;], label=&quot;Mean forecast&quot;)<br>plt.plot(y_test, label=&quot;Future time series values&quot;)<br><br>plt.fill_between(<br>    y_pred.index, y_pred[&quot;0.1&quot;], y_pred[&quot;0.9&quot;], color=&quot;red&quot;, alpha=0.1, label=f&quot;10%-90% confidence interval&quot;<br>)<br>plt.legend();</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*9RT10one0WCcVPiNUlBULg.png" /></figure><p>To use a specific model from the trained models, choose from the leaderboard or the available models can be seen with following piece of code.</p><pre>available_models = predictor.get_model_names()<br>available_models</pre><pre>[&#39;SeasonalNaive&#39;,<br> &#39;CrostonSBA&#39;,<br> &#39;NPTS&#39;,<br> &#39;AutoETS&#39;,<br> &#39;DynamicOptimizedTheta&#39;,<br> &#39;AutoARIMA&#39;,<br> &#39;RecursiveTabular&#39;,<br> &#39;DirectTabular&#39;,<br> &#39;DeepAR&#39;,<br> &#39;TemporalFusionTransformer&#39;,<br> &#39;PatchTST&#39;,<br> &#39;WeightedEnsemble&#39;]</pre><p>Above list shows models available with current TimeSeriesPredictor .</p><h4><strong>Train with a chosen model from Model Zoo</strong></h4><p>A preferred model can be chosen from Model Zoo and used. Refer <a href="https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-model-zoo.html">[3]</a> for the complete list of available models. Choosing <strong>DeepAR</strong> as the model of interest from the model, training can be done as below.</p><pre>predictor = TimeSeriesPredictor(target=&#39;Passengers&#39;, prediction_length=19).fit(<br>   train,<br>   hyperparameters={<br>      &quot;DeepAR&quot;: {},<br>   },<br>)</pre><pre>Beginning AutoGluon training...<br>AutoGluon will save models to &#39;AutogluonModels\ag-20240201_193851&#39;<br>=================== System Info ===================<br>AutoGluon Version:  1.0.0<br>Python Version:     3.8.18<br>Operating System:   Windows<br>Platform Machine:   AMD64<br>Platform Version:   10.0.22621<br>CPU Count:          12<br>GPU Count:          0<br>Memory Avail:       1.51 GB / 7.33 GB (20.6%)<br>Disk Space Avail:   262.48 GB / 476.08 GB (55.1%)<br>===================================================<br><br>Fitting with arguments:<br>{&#39;enable_ensemble&#39;: True,<br> &#39;eval_metric&#39;: WQL,<br> &#39;hyperparameters&#39;: {&#39;DeepAR&#39;: {}},<br> &#39;known_covariates_names&#39;: [],<br> &#39;num_val_windows&#39;: 1,<br> &#39;prediction_length&#39;: 19,<br> &#39;quantile_levels&#39;: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],<br> &#39;random_seed&#39;: 123,<br> &#39;refit_every_n_windows&#39;: 1,<br> &#39;refit_full&#39;: False,<br> &#39;target&#39;: &#39;Passengers&#39;,<br> &#39;verbosity&#39;: 2}<br><br>Inferred time series frequency: &#39;MS&#39;<br>Provided train_data has 77 rows, 1 time series. Median time series length is 77 (min=77, max=77). <br><br>Provided dataset contains following columns:<br>	target:           &#39;Passengers&#39;<br><br>AutoGluon will gauge predictive performance using evaluation metric: &#39;WQL&#39;<br>	This metric&#39;s sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.<br>===================================================<br><br>Starting training. Start time is 2024-02-02 01:08:51<br>Models that will be trained: [&#39;DeepAR&#39;]<br>Training timeseries model DeepAR. <br>	-0.1003       = Validation score (-WQL)<br>	50.19   s     = Training runtime<br>	0.11    s     = Validation (prediction) runtime<br>Not fitting ensemble as only 1 model was trained.<br>Training complete. Models trained: [&#39;DeepAR&#39;]<br>Total runtime: 50.33 s<br>Best model: DeepAR<br>Best model score: -0.1003</pre><pre>predictions = predictor1.predict(train)<br>predictions</pre><pre>Model not specified in predict, will default to the model with the best validation score: DeepAR</pre><p><strong>Predict using DeepAR:</strong></p><pre>predictions = predictor.predict(train)<br>predictions</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*o0E-Rh8ZVLqIYbj2dfpUVw.png" /></figure><p><strong>Plot predictions:</strong></p><pre>import matplotlib.pyplot as plt<br><br>plt.figure(figsize=(20, 3))<br><br>item_id = &quot;airline&quot;<br>y_past = train.loc[item_id][&quot;Passengers&quot;]<br>y_pred = predictions.loc[item_id]<br>y_test = test.loc[item_id][&quot;Passengers&quot;]<br><br>plt.plot(y_past, label=&quot;Past time series values&quot;)<br>plt.plot(y_pred[&quot;mean&quot;], label=&quot;Mean forecast&quot;)<br>plt.plot(y_test, label=&quot;Future time series values&quot;)<br><br>plt.fill_between(<br>    y_pred.index, y_pred[&quot;0.1&quot;], y_pred[&quot;0.9&quot;], color=&quot;red&quot;, alpha=0.1, label=f&quot;10%-90% confidence interval&quot;<br>)<br>plt.legend();</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*VZlIEjuCn9huGa_Yhnqf9w.png" /></figure><p>To read more forecasting blogs visit below pages.</p><ul><li><a href="https://pub.towardsai.net/time-series-forecasting-using-pycaret-bc1519113864">Time Series Forecasting using PyCaret</a></li><li><a href="https://ai.plainenglish.io/arima-garch-a-hybrid-model-to-forecast-highly-volatile-data-8b2ed0155b34">ARIMA + GARCH: A Hybrid Model to Forecast Highly Volatile Data.</a></li><li><a href="https://pub.towardsai.net/brent-oil-forecasting-using-gru-887639a2c02c">Brent Crude Oil Price Forecasting using GRU.</a></li></ul><h4>Conclusion</h4><p>We have seen the capability of AutoGluon Multimodal library in this page where we can experiment different models with minimal code. The library offers much more for different types of Machine Learning problems which can be read elaborately by visiting <a href="https://auto.gluon.ai/stable/index.html">AutoGluon 1.0.0 documentation</a>.</p><h4>References</h4><ol><li><a href="https://medium.com/r?url=https%3A%2F%2Fauto.gluon.ai%2Fstable%2Finstall.html">https://medium.com/r?url=https%3A%2F%2Fauto.gluon.ai%2Fstable%2Finstall.html</a></li><li><a href="https://medium.com/r?url=https%3A%2F%2Fauto.gluon.ai%2Fstable%2Ftutorials%2Ftimeseries%2Fforecasting-metrics.html">https://medium.com/r?url=https%3A%2F%2Fauto.gluon.ai%2Fstable%2Ftutorials%2Ftimeseries%2Fforecasting-metrics.html</a></li><li><a href="https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-model-zoo.html">Forecasting Time Series — Model Zoo — AutoGluon 1.0.0 documentation</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34f8780b8214" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Processing Monalisa: Image processing with Scikit-image.]]></title>
            <link>https://pub.towardsai.net/monalisa-273cde56077a?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/273cde56077a</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[image-filter]]></category>
            <category><![CDATA[scikit-image]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[thresholding]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Mon, 29 Jan 2024 16:15:39 GMT</pubDate>
            <atom:updated>2024-01-30T13:02:42.637Z</atom:updated>
            <content:encoded><![CDATA[<h3>Processing Monalisa: Image Processing with Scikit-image.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h1KLKyod2bgqyCK0wjy1BA.jpeg" /><figcaption>Source: <a href="https://i.ytimg.com/vi/EF23VQcj01E/maxresdefault.jpg">https://i.ytimg.com</a></figcaption></figure><h4>Scikit-image.</h4><p>Scikit-image is an open-source Python library for image processing that offers various algorithms for color conversion, filtering, thresholding segmentation, denoising, etc. On this page, we will see some of them applied to the famous painting ‘Monalisa’ done by the Italian polymath Leonardo da Vinci in the early 16th century.</p><h4>Install Scikit-image.</h4><p>Scikit-image can be installed using pip or with conda, as given below.</p><p>!pip install scikit-image</p><p>conda install -c conda-forge scikit-image</p><h4>Import Monalisa</h4><p>The image is imported using imageio library and displayed using matplotlib. The shape of the image is 604x405x3 (height, width, channels). The 3 channels correspond to three color channels, which implies that it is an RGB image.</p><pre>import skimage<br>import imageio<br>import matplotlib.pyplot as plt<br><br>imagePath = &#39;monalisa.JPG&#39; # path to image<br>monalisa = imageio.imread(imagePath)<br>plt.imshow(monalisa)<br>plt.title(&#39;Original Image&#39;)<br>plt.show()</pre><pre>print(monalisa.shape) # checking shape of the image<br>(604, 405, 3)</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*uWVhggN5GLiYMHAHg21img.png" /></figure><h4>RGB to Grayscale</h4><p>In grayscale conversion, the number of channels will be reduced to 1. Generally, the grayscale values range from 1 to 255 but scikit-image outputs normalized values in a range [0,1] for numerical stability. (<em>Normalization: each pixel value/255). </em>The conversion can be done with the function rgb2gray by importing from skimage.color.</p><pre>from skimage.color import rgb2gray<br><br>monalisaGray = rgb2gray(monalisa)<br>colorSpace = [&#39;Original (RGB)&#39;, &#39;Grayscale&#39;]<br>images = [monalisa, monalisaGray]<br>fig, axes = plt.subplots(1, len(colorSpace), figsize= (10,4))<br><br>for idx, s in enumerate(colorSpace):<br>    axes[idx].imshow(images[idx])<br>    axes[idx].set_title(f&#39;{colorSpace[idx]}&#39;)<br>plt.show()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*Jrkc_HAC1JP8BPUcvrWMLw.png" /><figcaption>RGB to Grayscale conversion</figcaption></figure><p>The change in shape can be seen in the snippet below.</p><pre>monalisa.shape, monalisaGray.shape</pre><pre>(604, 405, 3)), ((604, 405)</pre><h4>Color conversion.</h4><p>Transforming the color representation of an image from one color space to another is called color conversion. Some of the color spaces are &#39;HSV&#39;, &#39;XYZ&#39;, &#39;YUV&#39;, &#39;RGB CIE&#39; etc. The below snippet converts the original image to listed color spaces as in the figure with use of convert_colorspace the function. To read more about color spaces and their uses elaborately, visit the wiki page <a href="https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses">List of color spaces and their uses — Wikipedia</a>.</p><pre>from skimage.color import convert_colorspace </pre><pre>&#39;&#39;&#39;create list of required color spaces.&#39;&#39;&#39;<br>colorSpace = [&#39;HSV&#39;, &#39;XYZ&#39;, &#39;YUV&#39;, &#39;RGB CIE&#39;]</pre><pre>fig, axes = plt.subplots(1, len(colorSpace), figsize= (20,6))<br><br>for idx, s in enumerate(colorSpace):<br>    monalisaColor = convert_colorspace(monalisa ,&#39;RGB&#39;, s)<br>    axes[idx].imshow(monalisaColor)<br>    axes[idx].set_title(f&#39;colorspace = {s}&#39;)<br>plt.show()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*VCscyez6oRZPYhpuXjYF_w.png" /><figcaption>Conversion to different color spaces.</figcaption></figure><p>Alternatively, the same can be done using functionrgb2hsv, rgb2xyz, rgb2yuv etc. by importing these from skimage.color . e.g. image = rgb2hsv(monalisa).</p><h4>Thresholding and Inverted thresholding</h4><p>Thresholding classifies pixel values with respect to a specified threshold value. Pixel value above the threshold will be converted to 1 and below as 0 <strong>in thresholding</strong> whereas in opposite manner <strong>in inverse thresholding</strong>. It is straightforward to do thresholding on grayscale images, but for RGB images, thresholding on each channel should be done separately and combined later. A threshold of 0.3 (since the grayscale obtained by the image is normalized) is used for the processing of the image in the below code.</p><p>Different color mapping can be used to display the converted image by setting cmap inside plt.imshow() for different visual experiences. e.g. plt.imshow(image, cmap = ‘jet’). To check different available color mappings in matplotlib, visit <a href="https://matplotlib.org/stable/users/explain/colors/colormaps.html">Choosing Colormaps — Matplotlib 3.8.2 documentation</a>.</p><pre>th = 0.3 # set threshold value<br><br>img1 = monalisa &gt;= th # thresholding<br>img2 = monalisa &lt;= th # inverted thresholding<br><br>img = [monalisa, img1, img2]<br>title = [&#39;Original&#39;, &#39;Thresholded&#39;, &#39;Inverted Thresholded&#39;]<br><br>fig, axes = plt.subplots(1, len(title), figsize= (20,6))<br>for idx, s in enumerate(title):<br>    axes[idx].imshow(img[idx])<br>    axes[idx].set_title(f&#39;colorspace = {s}&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*O4fKCyqfO1NQNURNxbsKNw.png" /><figcaption>Thresholding (cmap = None)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*vIhBbtpvATZgFIwX4MxmdA.png" /><figcaption>Thresholding (cmap = ‘Gray’)</figcaption></figure><h4>Filtering-1: Gaussian</h4><p>Gaussian filtering is a type of low-pass filter that is mainly used to introduce noise or blur in the image(smoothening). Parameter sigma is the <strong>standard deviation</strong> of the Gaussian distribution, which determines the spread of the Gaussian function. Once sigma increases, the smoothening or noise introduced in the image increases, as you see in the below plot.</p><pre>from skimage.filters import gaussian</pre><pre>&#39;&#39;&#39;set list of sigma values.&#39;&#39;&#39;<br>sigma = [1, 3, 6, 9]<br><br>fig, axes = plt.subplots(1, len(sigma), figsize= (20,6))<br>for idx, s in enumerate(sigma):<br>    axes[idx].imshow(gaussian(monalisa , sigma = s))<br>    axes[idx].set_title(f&#39;sigma = {s}&#39;)<br>plt.show()</pre><figure><img alt="png" src="https://cdn-images-1.medium.com/proxy/1*UeB7LPBXYvz4u7tQ53qwTQ.png" /><figcaption>Gaussian Filtering</figcaption></figure><h4>Filtering-2: Butterworth</h4><p>Butterworth filter is a frequency domain filter that is used to enhance the images. It works by passing certain frequencies and attenuating others. order and cutoff_frequency_ratio are the main parameters of the filter. The parameter order can be understood as the number of times the filtering is applied to the image. Higher order leads to rapid increases in attenuation beyond cutoff frequency.</p><p>Butterworth filtering with various values of cutoff_frequency_ratio :</p><pre>from skimage.filters import butterworth</pre><pre>&#39;&#39;&#39;create a list of cutoff frequency ratio.&#39;&#39;&#39;<br>f_cutoff = [.001,.005,.05, .5]</pre><pre>fig, axes = plt.subplots(1, len(f_cutoff), figsize= (20,6))<br>for idx, s in enumerate(f_cutoff):<br>    axes[idx].imshow(butterworth(monalisa , cutoff_frequency_ratio = f_cutoff[idx]))<br>    axes[idx].set_title(f&#39;cutoff_frequency_ratio = {f_cutoff[idx]}&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D-9tRgmu_1riqdY4Q2Hzrg.png" /><figcaption>Butterworth filtering with different value of cutoff-frequency-ratio.</figcaption></figure><p>Butterworth filtering with various value of order = [1,2,3,4]keeping cutoff_frequency_ratio = 0.5 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FpSnZH_bJQGeWlqF1geRsw.png" /><figcaption>Butterworth filtering with various value of order.</figcaption></figure><p><strong>For more filters on RGB images (</strong>Laplace, Median etc.), refer to <a href="https://scikit-image.org/docs/stable/api/skimage.filters.html#">skimage.filters — skimage 0.22.0 documentation (scikit-image.org)</a>.</p><h4>Filtering-3: Farid, Scharr, Prewitt, Sobel, Roberts (for bimodal Images).</h4><p>These filters are used for edge detection of bimodal (grayscale) images. Filters can be applied along vertical and horizontal axes also. The filtered images represent the gradient of the original image along each axis where gradient is the rate of change of pixel intensity. The below code written for <strong>farid</strong> filters can be applied to <strong>sobel</strong>, <strong>prewitt</strong> and <strong>scharr</strong> filters (<em>Only filtered images included in this page</em>). For roberts filter, it is roberts_neg_diag ,roberts_pos_diag instead of horizontal and vertical where <strong>neg</strong> and <strong>pos</strong> denote the diagonal direction of kernel.</p><p><em>Note: The below code can be extended to all other filters. Only filter outputs are included.</em></p><pre>from skimage.filters import farid, farid_h , farid_v<br><br>monalisaF = farid(monalisaGray)<br>monalisaFh = farid_h(monalisaGray)<br>monalisaFv = farid_v(monalisaGray)<br><br><br>F = [&#39;Grayscale&#39;,&#39;farid_h&#39;, &#39;farid_v&#39;, &#39;farid&#39;]<br>I = [monalisaGray, monalisaF , monalisaFh, monalisaFv]<br>fig, axes = plt.subplots(1, len(I), figsize=(20,6))<br><br>for idx, img in enumerate(I):<br>    axes[idx].imshow(img,cmap = &#39;gray&#39;)<br>    axes[idx].set_title(f&#39;filter: {F[idx]}&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OB6ALFlyyDUf376QyAq5dw.png" /><figcaption>Farid filter outputs.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhzJK5ytxo92ZppQKJ6G2A.png" /><figcaption>Prewitt filter outputs.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OWtCpEtVxnRSPWZeY9fG5A.png" /><figcaption>Sobel filter outputs.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XgF74MH1-6vcn6o_89TPZA.png" /><figcaption>Roberts filter outputs.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DOLBS8iufpZMvtZpDbObig.png" /><figcaption>Roberts filter outputs.</figcaption></figure><p>Even if the output of all these filters looks same, the pixel values differ in value. Checking just one element in the filtered image pixel values of all filters as below it can be clearly understood.</p><pre>print(monalisaSob[1][1], monalisaS[1][1],monalisaR[1][1], monalisaP[1][1], monalisaF[1][1])</pre><pre>0.05072484484173028 0.05344169652064987 0.0697070304160717 0.04828679324051692 0.017526469293676204</pre><h4>Filtering-4: Hessian, Frangi.</h4><p>Frangi and Hessian filters have application beyond edge detection and and are mainly used in medical image processing. Both of these filters make use of properties of<strong> Hessian matrix which is a square matrix of second order partial derivatives.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*19Xk4ZRS0No6XJGwe4C2gg.png" /><figcaption>Hessian Matrix of function f (Source:<a href="https://en.wikipedia.org/wiki/Hessian_matrix">Hessian matrix — Wikipedia</a>)</figcaption></figure><p><strong>Hessian filters use the Hessian matrix as kernel, whereas Frangi filters use eigenvalues of a Hessian matrix. Frangi filters highlight the tubular structures and curvatures in the image and are specially designed for medical images such that they can detect blood vessels.</strong> Hessian filters are useful for detecting structures based on their local shape, which makes them useful for the segmentation of medical images.</p><pre>from skimage.filters import frangi , hessian<br><br>F = [&#39;Grayscale&#39;,&#39;frangi&#39;, &#39;hessian&#39;, &#39;hessian(on RGB)&#39;]<br>I = [monalisaGray,frangi(monalisaGray), hessian(monalisa), hessian(monalisaGray),]<br>fig, axes = plt.subplots(1, len(I), figsize=(20,6))<br><br>for idx, img in enumerate(I):<br>    axes[idx].imshow(img, cmap = &#39;gray&#39;)<br>    axes[idx].set_title(f&#39;filter = {F[idx]}&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bv0qt0j82ywR6jGa5m1NGg.png" /><figcaption>Frangi and Hessian filter outputs</figcaption></figure><p>While it can be used with both grayscale and RGB images, this filter works well with grayscale images.</p><h4>Filtering on RGB images.</h4><p>The filters which we used for edge detection of bimodal images (scharr, sobel, farid, roberts etc.) can be used for RGB images with the help of <strong>decorators</strong> as below. The filter acts on each channel, and later, the filtered values of all channels are combined. An example of the Sobel filter is given below.</p><p>To read more about Python decorators visit: <a href="https://realpython.com/primer-on-python-decorators/">https://realpython.com/primer-on-python-decorators/</a></p><pre>from skimage.color import adapt_rgb, each_channel, hsv_value<br>from skimage.exposure import rescale_intensity<br>from skimage import data, filters<br><br>@adapt_rgb(each_channel)<br>def sobelEach(image):<br>    return filters.sobel(image)<br><br>@adapt_rgb(hsv_value)<br>def sobelHSV(image):<br>    return filters.sobel(image)<br><br>img1 = rescale_intensity(1- scharrEach(monalisa))<br>img2 = rescale_intensity(1- scharrHSV(monalisa))<br><br><br>I = [monalisa, img1, img2]<br>F = [&#39;original&#39;,&#39; sobel (each channel)&#39;, &#39;sobel (HSV)&#39;]<br><br>fig, axes = plt.subplots(1, len(I), figsize=(20,6))<br><br>for idx, img in enumerate(I):<br>    axes[idx].imshow(img,cmap = &#39;gray&#39;)<br>    axes[idx].set_title(f&#39;filter = {F[idx]}&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8OVV5s_zVJVnzaWfeyxNJw.png" /><figcaption>Sobel filter on RGB image.</figcaption></figure><h4>Conclusion</h4><p>We have seen some of the features of Scikit-image on this page, mainly filters. The library offers many more algorithms, such as drawing shapes, thresholding, segmentation, image restoration, denoising, and much more. Those can be explored from the page <a href="https://scikit-image.org/">scikit-image: Image processing in Python — scikit-image</a>.</p><h4>References</h4><ol><li><a href="https://scikit-image.org/">scikit-image: Image processing in Python — scikit-image</a></li><li><a href="https://en.wikipedia.org/wiki/Hessian_matrix">Hessian matrix — Wikipedia</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=273cde56077a" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/monalisa-273cde56077a">Processing Monalisa: Image processing with Scikit-image.</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multi-Label Image Classification using AutoKeras.]]></title>
            <link>https://pub.towardsai.net/untitled-3fc5213e9385?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fc5213e9385</guid>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[image-classification]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Sat, 27 Jan 2024 11:11:46 GMT</pubDate>
            <atom:updated>2024-01-28T12:24:38.598Z</atom:updated>
            <content:encoded><![CDATA[<h4>Code accompanied explanation of CIFAR-10 image classification using AutoKeras.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IVK-GUd7A-l4YGFVuRrIhg.jpeg" /><figcaption>Image created with Microsoft Bing Image Maker</figcaption></figure><h4>AutoKeras</h4><p>AutoKeras is Python’s Keras-based AutoML library for developing Deep Learning models. Built on top of TensorFlow by DATA Lab at Texas A&amp;M University, it offers a solutions for classification, regression, time series forecasting, and much more. This page aims to explain how to solve a multilabel classification problem with minimal code focusing on a familiar CIFAR-10 image dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1IuVXPjyfvZMi1eid7-avQ.png" /></figure><h4>Environment Setup</h4><p>As the first step, AutoKeras should be installed and imported along with TensorFlow.</p><pre>!pip install autokeras<br><br>import autokeras as ak<br>import tensorflow as tf<br>import pandas as pd </pre><h4>Import and walk through the CIFAR-10 dataset</h4><p>The CIFAR-10 image dataset is readily available in TensorFlow, which is already split into train and test. There are 60000 32x32 color images in 10 different classes, where 10000 are test images. The different classes of the dataset is as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*4-IAYLuD5yU7vjTrZnn_Cw.png" /><figcaption>CIFAR-10 Dataset. Source: <a href="https://www.cs.toronto.edu/~kriz/cifar.html">https://www.cs.toronto.edu/~kriz/cifar.html</a></figcaption></figure><p>Let’s import the dataset and walk through it.</p><pre>&#39;&#39;&#39;import image dataset&#39;&#39;&#39;<br>from tensorflow.keras.datasets import cifar10<br><br>(xTrain, yTrain), (xTest, yTest) = cifar10.load_data()<br>print(xTrain.shape)  <br>print(yTrain.shape)<br>print(xTest.shape)<br>print(yTest.shape)<br><br>(50000, 32, 32, 3) #shape of train data<br>(50000, 1)         #shape of train labels<br>(10000, 32, 32, 3) #shape of test data<br>(10000, 1)         #shape of test labels</pre><p>Some random train images and corresponding labels.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br><br>r = np.random.randint(0, 5000,10) # generate 10 random numbers<br>fig, axes = plt.subplots(1, len(r), figsize=(15, 3))<br><br>for idx, i in enumerate(r):<br>    axes[idx].imshow(xTrain[i], cmap=plt.cm.binary)<br>    axes[idx].set_title(yTrain[i])<br>    axes[idx].axis(&#39;off&#39;) <br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qcWZ-ckvbf8yV7pbW7M4iA.png" /></figure><h4>Initialize and fit the Image Classifier</h4><p>The classifier can be initialized using ImageClassifier() class. The hyperparameter max_trials determines how many model to try. Setting it to 1 here as it is just a demonstration of how to use the library. overwrite = True will overwrite the previous model if the next one performs better (while max_trials &gt;1) which in turn saves the best model.</p><pre># Initialize the image classifier.<br><br>tf.random.set_seed(102)<br>ImgClassifier = ak.ImageClassifier(overwrite=True, max_trials=1)<br># Feed the image classifier with training data.<br>ImgClassifier.fit(xTrain, yTrain, epochs=40)</pre><p>The process starts by printing the current model architecture and hyperparameters as below. Different layers, optimizer, and learning rate can be observed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gcPYikkzNb_AnbOZpjyOwg.png" /></figure><h4>Model Summary and Evaluation</h4><p>Model architecture can be printed as below.</p><pre>print(ImgClassifier.export_model().summary())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XrcJdA42HSvCFRXOojNfJw.png" /></figure><p>Prediction and evaluation</p><pre># Predict with model on test data.<br>prediction = ImgClassifier.predict(xTest)<br>print(prediction)<br><br># Evaluate the model.<br>print(ImgClassifier.evaluate(xTest, yTest))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xecuSinsv5AOIrUMxK7xmw.png" /></figure><p>For 40 epochs and 1 trial, the model obtained is quite decent. Better models can be obtained by setting max_trials greater than 1 and running the model for more epochs .</p><p>Detailed analysis of model performance can be analyzed with scilit-learn library by generating a <strong>confusion matrix</strong> and <strong>classification report</strong> using the below code.</p><pre>&#39;&#39;&#39;To print classification report&#39;&#39;&#39;<br>from sklearn.metrics import classification_report<br>print(classification_report(prediction.astype(int) , yTest))<br><br>&#39;&#39;&#39;To display confusion matrix&#39;&#39;&#39;<br>from sklearn.metrics import ConfusionMatrixDisplay,confusion_matrix<br>cM = ConfusionMatrixDisplay(confusion_matrix(prediction.astype(int), yTest), display_labels=None)<br>cM.plot()</pre><p>AutoKeras also offers customized search space for advanced users where one can use <strong>AutoModel</strong>. For more details, visit <a href="https://autokeras.com/tutorial/image_classification/#customized-search-space">https://autokeras.com/tutorial/image_classification/#customized-search-space</a></p><blockquote>To read about forecasting using PyCaret AutoML library, visit below page.</blockquote><p><a href="https://pub.towardsai.net/time-series-forecasting-using-pycaret-bc1519113864">Time Series Forecasting using PyCaret</a></p><h4>Conclusion</h4><p>From the page, it is evident that the AutoKeras library facilitates the automation of developing deep learning models with minimal code.I hope this page has provided a helpful understanding of the utility and implementation of the library to some extent. By configuring hyperparameters as per specific requirements and leveraging the library’s customizable search space, AutoKeras proves to be a valuable tool for data scientists and ML engineers.</p><h4>References</h4><ol><li><a href="https://autokeras.com/">https://autokeras.com</a>/</li><li><a href="https://www.cs.toronto.edu/~kriz/cifar.html">https://www.cs.toronto.edu/~kriz/cifar.html</a></li></ol><p>✉️mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fc5213e9385" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/untitled-3fc5213e9385">Multi-Label Image Classification using AutoKeras.</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Time Series Forecasting using PyCaret]]></title>
            <link>https://pub.towardsai.net/time-series-forecasting-using-pycaret-bc1519113864?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc1519113864</guid>
            <category><![CDATA[automl]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[pycaret]]></category>
            <category><![CDATA[time-series-analysis]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Thu, 25 Jan 2024 10:03:15 GMT</pubDate>
            <atom:updated>2024-01-30T10:22:47.611Z</atom:updated>
            <content:encoded><![CDATA[<h4>This page explains how to do forecasting using Python’s low-code AutoML library PyCaret.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*VorjVImhrPH-7OaC71si7Q.jpeg" /><figcaption>Source: <a href="https://www.bing.com/images/search?q=pycaret&amp;form=HDRSC3&amp;first=1">pycaret — Search Images (bing.com)</a></figcaption></figure><h4>PyCaret.</h4><p>PyCaret is a low-code Python library for Machine Learning that automates workflows and offers efficient end-to-end experiments. Automating major steps in evaluation and comparing models makes it easier for us to examine the performance of different models and select the best one. On this page, let’s see how to approach a time series forecasting problem of familiar airline data with PyCaret.</p><h4>Install and import PyCaret.</h4><pre>pip install pycaret<br>import pycaret</pre><h4>Prepare data.</h4><p>Data available within the PyCaret library can be imported with pycaret.datasets.get_data(). Otherwise, import using pandas from the device as usual.</p><pre>&#39;&#39;&#39;load Airline data&#39;&#39;&#39;<br><br>import pandas as pd<br>data = pd.read_excel(&#39;Airlines_data.xlsx&#39;)<br>data.set_index(&#39;Month&#39;,inplace=True)<br># data = pycaret.datasets.get_data(&#39;airlines) # to load from pycaret library<br>data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*hyCp0tIn2RLttP-s-kc9rA.png" /><figcaption>Airlines data</figcaption></figure><h4>Setup Environment.</h4><p>The setup() function initializes the environment for training and creates the transformation pipeline. It is important to call setup() before executing any other function. For the Time Series experiment, the following parameters should be passed to the setup function.</p><p><strong>fh:</strong> <em>forecasting horizon</em></p><p><strong>fold:</strong> <em>number of folds for cross validation</em></p><p><strong>session_id:</strong> <em>random seed for experiment</em></p><pre>&#39;&#39;&#39;Import Time Series libraries&#39;&#39;&#39;<br>from pycaret.time_series import *<br><br>&#39;&#39;&#39;Initialize the training environment&#39;&#39;&#39;<br>s = setup(data, fh = 3, fold = 5, session_id = 101)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*IexDiYBR-_9SZPBH88QESg.png" /></figure><p>As you can see in the above table, AutoML analyzes the time series data and provides a detailed summary, especially about seasonality. Have a look into entries Seasonality Present and Significant Seasonal Period(s) in the above table generated by AutoML.</p><h4>Choose API and Compare Models.</h4><p>PyCaret has two API’s. <strong>Functional</strong> and <strong>OOP</strong>. Functional API follows a procedural approach and offers a straightforward interface. Since the settings are applied globally across functions, this API is convenient for quick experiments. The OPP API follows an object-oriented approach, providing a customizable and modular workflow and, hence, more suitable for running multiple experiments independently. In this experiment, I am using functional API (<em>Codes for OOP API are commented</em>).</p><p>The compare_model() function returns a table containing different models and performance metrics. It can be observed from the table that Exponential smoothing outperforms all other models.</p><pre>&#39;&#39;&#39;Compare models using Functional API&#39;&#39;&#39;<br>best = compare_models()<br><br># &#39;&#39;&#39;OOP API&#39;&#39;&#39;<br># best = s.compare_models()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2dCvxDglp5LFdLA6jF2UxA.png" /></figure><h4>Plot the Forecast and Diagnose Residuals.</h4><p>Now let’s plot the forecast using plot_model() function. In data_kwargs the dictionary, we can also give keywords for the plot, such as title, legend, opacity, height, width etc. as per requirement.</p><pre>&#39;&#39;&#39;Plot forecasting using functional API&#39;&#39;&#39;<br>plot_model(best, plot = &#39;forecast&#39;, data_kwargs = {&#39;fh&#39; : 24})<br><br># OOP API<br># s.plot_model(best, plot = &#39;forecast&#39;, data_kwargs = {&#39;fh&#39; : 24})</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*y63NxDOqAkZVvTuDGzDnBA.png" /></figure><p>The model’s prediction on training data (in sample) can be plotted as below.</p><pre>&#39;&#39;&#39;Insample plot using functional API&#39;&#39;&#39;<br>plot_model(best, plot = &#39;insample&#39;)<br><br># OOP API<br># s.plot_model(best, plot = &#39;insample&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*l9uBbkLhWyACEMv9Q89d-A.png" /></figure><p>To diagnose the residuals, we can use the same plot_model() function with key word plot = ‘diagnostics’ as below.</p><pre>&#39;&#39;&#39;Diagnose best model using functional API&#39;&#39;&#39;<br>plot_model(best, plot = &#39;diagnostics&#39;)<br><br># OOP API<br># s.plot_model(best, plot = &#39;diagnostics&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/997/1*J0CyyMDw5XixCAlSV_Y4IA.png" /></figure><h4>Forecast using Best Model</h4><p>Since we have Exponential Smoothing as our best model, let’s forecast and plot for a horizon of 24.</p><pre>&#39;&#39;&#39;Forecast using functional API&#39;&#39;&#39;<br>forecast = predict_model(best, fh = 24)<br><br># OOP API<br># s.predict_model(best, fh = 24)<br><br>&#39;&#39;&#39;Plot forecast&#39;&#39;&#39;<br>import matplotlib.pyplot as plt<br>plt.plot(forecast.index.strftime(&#39;%Y-%m&#39;), forecast[&#39;y_pred&#39;].astype(float),label = &#39;forecast&#39;)<br>plt.legend(loc=[1,1])<br>plt.xticks(rotation=45);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*8saGF-72rGoAIUsR1mMQhQ.png" /></figure><h4>Save and Load the Best Model.</h4><p>The model can be saved using save_model() function that saves the model and returns the model architecture.</p><pre>&#39;&#39;&#39;Save model using functional API&#39;&#39;&#39;<br><br>finalModel = finalize_model(best)<br>save_model(finalModel, &#39;bestModel&#39;)<br><br># OOP API<br># s.save_model(finalModel, &#39;bestModel&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*7ksWy_faZ4Mfhw14TnR9mg.png" /></figure><p>Saved model can be loaded using load_model() function as follows.</p><pre>&#39;&#39;&#39;Load saved model using functional API&#39;&#39;&#39;<br>loaded_model = load_model(&#39;bestModel&#39;)<br># OOP API<br># loaded_model = s.load_model(&#39;my_final_best_model&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*mat_otI01WYfCvDy0aIPNQ.png" /></figure><blockquote>To read about forecasting of volatile data using hybrid models visit below page.</blockquote><p><a href="https://ai.plainenglish.io/arima-garch-a-hybrid-model-to-forecast-highly-volatile-data-8b2ed0155b34">ARIMA + GARCH: A Hybrid Model to Forecast Highly Volatile Data.</a></p><h4>Conclusion.</h4><p>We have observed on this page that PyCaret simplifies tasks such as data preprocessing, model selection, hyperparameter tuning, and model evaluation, reforming the workflow for users. The AutoML capability significantly reduces the coding effort required for developing robust machine learning models, making it an efficient and user-friendly tool for both beginners and experienced data scientists. Also, PyCaret can be used for other machine-learning tasks such as Regression, Classification, Clustering, Anomaly detection, etc.</p><h4>References.</h4><ol><li><a href="https://pycaret.gitbook.io/docs/">PyCaret 3.0 — Docs (gitbook.io)</a></li><li><a href="https://medium.com/python-in-plain-english/pycaret-simplified-mastering-low-code-machine-learning-e0277faed913">https://medium.com/python-in-plain-english/pycaret-simplified-mastering-low-code-machine-learning-e0277faed913</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc1519113864" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/time-series-forecasting-using-pycaret-bc1519113864">Time Series Forecasting using PyCaret</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prediction of DNA-Protein Interaction using CNN and LSTM.]]></title>
            <link>https://pub.towardsai.net/prediction-of-dna-protein-interaction-using-cnn-and-lstm-0a7aa7e074f1?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a7aa7e074f1</guid>
            <category><![CDATA[lstm]]></category>
            <category><![CDATA[binary-classification]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[tensorflow]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Thu, 04 Jan 2024 17:41:13 GMT</pubDate>
            <atom:updated>2024-01-26T15:53:07.738Z</atom:updated>
            <content:encoded><![CDATA[<h4>Development of a deep neural network to predict whether a DNA sequence can bind to a protein or not, explained with code.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A19VI-5CMddmplZI" /><figcaption>Photo by <a href="https://unsplash.com/@nci?utm_source=medium&amp;utm_medium=referral">National Cancer Institute</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Table of contents</h4><ol><li>Introduction</li></ol><p>2. About Data</p><p>3. Data Preprocessing</p><p>4. Model Building</p><p>5. Performance Analysis</p><p>6. Conclusion</p><p>7. References</p><h4>Introduction</h4><p>To begin with, let’s keep in mind that there exists some binding between DNA and protein. DNA-binding proteins are proteins that have DNA-binding domains and thus have a specific or general affinity for single- or double-stranded DNA [1]. Keeping genomics apart, the aim of my work is to develop a deep neural network that can predict whether a DNA sequence can bind to a protein or not. So, we will be heading to a binary classification problem, and I intend to show how a deep neural network composed of CNN and LSTM can effectively solve it.</p><h4>About Data</h4><p>The DNA sequence and labels are downloaded from <a href="https://github.com/abidlabs/deep-learning-genomics-primer/blob/master/sequences.txt">https://github.com/abidlabs/deep-learning-genomics-primer/blob/master/sequences.txt</a> and <a href="https://github.com/abidlabs/deep-learning-genomics-primer/blob/master/labels.txt">https://github.com/abidlabs/deep-learning-genomics-primer/blob/master/labels.txt</a> respectively which are publicly accessible. Let’s import and have a look at the data.</p><pre>import pandas as pd<br><br>df1 = pd.read_csv(&#39;sequences.txt&#39;, sep=&#39; &#39;, header=None, names=[ &#39;Sequence&#39;])<br>df2 = pd.read_csv(&#39;labels.txt&#39;, sep=&#39; &#39;, header=None, names=[&#39;Label&#39;])<br>df3 = pd.concat([df1,df2],axis=1)<br>df3.head(8)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*qZtkIstKceCjCKPXCaUq3w.png" /></figure><p>DNA consists of four bases, which are adenine [A], cytosine [C], guanine [G], or thymine [T]. DNA sequence is a laboratory process of determining the sequence of these four bases in a DNA molecule [1]. There are 2000 records, and each are of length 200. Also, the data is reasonably balanced as you see below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*6dG7TZHdIyNGJROYYSnHcw.png" /></figure><h4>Data Preprocessing</h4><p>The text sequence needs to be converted into numbers before feeding to the model. One-hot encoding is done on the sequence since neural networks often works well with the same. The encoding code is done as below.</p><pre>def one_hot_encode(sequence):<br>    nucleotide_to_index = {&#39;A&#39;: 0, &#39;T&#39;: 1, &#39;G&#39;: 2, &#39;C&#39;: 3}<br>    one_hot_encoded = []<br><br>    for nucleotide in sequence:<br>        one_hot_vector = [0] * 4<br>        one_hot_vector[nucleotide_to_index[nucleotide]] = 1<br>        one_hot_encoded.append(one_hot_vector)<br><br>    return np.array(one_hot_encoded)<br><br>df3[&#39;one-hot&#39;]<br><br>0       [[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 0], [1,...<br>1       [[0, 0, 1, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0,...<br>2       [[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0,...<br>3       [[0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0,...<br>4       [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0,...<br>                              ...                        <br>1995    [[0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0,...<br>1996    [[0, 0, 1, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0,...<br>1997    [[1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], [0,...<br>1998    [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1,...<br>1999    [[1, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0,...<br>Name: one-hot, Length: 2000, dtype: object</pre><h4>Model Building</h4><p>A combination of Convolutional Neural Network (CNN) and Long Short-Term Memory (LSTM) layers are mainly used for the model. <strong>Conv-1D layers are mainly used in problems that involve sequential data.</strong> Conv1D layers are efficient in capturing patterns regardless of the position in the sequence. Since these layers have fewer parameters, they are computationally efficient and also known for their ability to capture local patterns in sequential data.</p><p><strong>LSTM is a Recurrent Neural Network that is designed to overcome the issue of the vanishing gradient problem.</strong> The three gates in the cell input gate, output gate and forget gate incorporate to retain and propagate information in the sequence.</p><p>The data is split to train and test as below.</p><pre>from sklearn.model_selection import train_test_split<br><br>&#39;&#39;&#39;split the data&#39;&#39;&#39;<br>trainSeq,testSeq,trainLabels,testLabels = train_test_split(df3[&quot;one-hot&quot;].to_numpy(),<br>                                                                      df3[&#39;Label&#39;].to_numpy(),<br>                                                                      test_size=0.2,<br>                                                                      random_state=101)<br>&#39;&#39;&#39;convert train and test data to numpy array&#39;&#39;&#39;<br>seqTrain1 = np.array(seqTrain)<br>seqTest1 = np.array(seqTest)<br><br>&#39;&#39;&#39;reshape the array since the neural layers expect data in 3D&#39;&#39;&#39;<br>X_train = np.reshape(seqTrain1, (seqTrain1.shape[0], seqTrain1.shape[1], 1))  # (1600, 200, 1)<br>X_test =  np.reshape(seqTest1, (seqTest1.shape[0], seqTest1.shape[1], 1))  # (1600, 200, 1)<br><br>&#39;&#39;&#39;check shape of train and test data&#39;&#39;&#39;<br>X_train.shape,  X_test.shape<br>((1600, 200, 1), (400, 200, 1))</pre><p>The model is configured in <strong>TensorFlow</strong> framework using<strong> keras</strong> as the below code shows.</p><pre>import tensorflow as tf<br>from tensorflow.keras import layers<br>tf.random.set_seed(101)<br><br><br>inputs= layers.Input(shape=(200,1 ))<br><br>x= layers.Conv1D(filters=7,kernel_size=5)(inputs)<br>x= layers.LSTM(units=64,return_sequences =True,)(x)<br>x= layers.MaxPooling1D(2,2)(x)<br>x= layers.Flatten()(x)<br>x=tf.keras.layers.Masking(mask_value=0)(x)<br>x=layers.Dense(64,activation = &#39;relu&#39;)(x)<br>x=layers.Dropout(0.1)(x)<br><br><br>outputs=layers.Dense(1,activation=&quot;sigmoid&quot;)(x)<br><br>modelLSTM=tf.keras.Model(inputs,outputs,name=&quot;modelLSTM&quot;)</pre><p>tf.keras.layers.Masking(mask_value=0) masks all timesteps where the value is equal to 0. During training, the masked timesteps will be ignored, and the network will not consider them, which eventually makes the computation efficient.</p><p>The model is trained for 20 epochs. A validation accuracy of .99 is quite impressive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tV-MH3o3Tue3n2sZZoK2dg.png" /></figure><p>Accuracy and loss curve during training of model are shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*J4jFZ7Uo9lMMlZNK5pdHpQ.png" /><figcaption>Accuracy Curve</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*41rBA6jxAV9oqCUBU8GY_g.png" /><figcaption>Loss Curve</figcaption></figure><h4>Performance Analysis</h4><p>Let’s evaluate the model with test data and have a look into the classification report as well as the confusion matrix.</p><pre>&#39;&#39;&#39;evaluating the model on test data&#39;&#39;&#39;<br>modelLSTM.evaluate(X_test,testLabels)<br><br>[0.055308908224105835, 0.9900000095367432]</pre><p><strong>Classification report:</strong></p><pre>             precision    recall  f1-score   support<br><br>         0.0       0.98      1.00      0.99       195<br>         1.0       1.00      0.98      0.99       205<br><br>    accuracy                           0.99       400<br>   macro avg       0.99      0.99      0.99       400<br>weighted avg       0.99      0.99      0.99       400</pre><p><strong>Confusion matrix:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*NT6bWrn50T-0StFcmdqG-A.png" /><figcaption>Confusion matrix</figcaption></figure><p>From the above results, the performance of the model is much more impressive other than negligible false negatives.</p><h4>Conclusion</h4><p>Statistical models such as SVM, KNN, and Random Forest were tried before moving into a neural network, and the maximum accuracy obtained was 0.83 with SVM. The deep model has outperformed all these models with stunning accuracy and an f1 score of 0.99.</p><h4>References</h4><ol><li><a href="https://medium.com/mlearning-ai/apply-machine-learning-algorithms-for-genomics-data-classification-132972933723">Apply Machine Learning Algorithms for Genomics Data Classification | by Ernest Bonat, Ph.D. | MLearning.ai | Medium</a></li><li><a href="https://www.tensorflow.org/tutorials/images/cnn">Convolutional Neural Network (CNN) | TensorFlow Core</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a7aa7e074f1" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/prediction-of-dna-protein-interaction-using-cnn-and-lstm-0a7aa7e074f1">Prediction of DNA-Protein Interaction using CNN and LSTM.</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DNA Sequence Classification Using Naive Baye’s Algorithm]]></title>
            <link>https://pub.towardsai.net/dna-sequence-classification-using-naive-bayes-algorithm-48fdaa65bcec?source=rss-5796472804b9------2</link>
            <guid isPermaLink="false">https://medium.com/p/48fdaa65bcec</guid>
            <category><![CDATA[classification-algorithms]]></category>
            <category><![CDATA[vectorization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[naive-bayes-classifier]]></category>
            <category><![CDATA[bioinformatics]]></category>
            <dc:creator><![CDATA[Rakesh M K]]></dc:creator>
            <pubDate>Tue, 02 Jan 2024 08:41:43 GMT</pubDate>
            <atom:updated>2024-01-26T15:55:23.477Z</atom:updated>
            <content:encoded><![CDATA[<h4>This page explains the classification of DNA sequences as ‘gene family’ with the Multinomial Naive Bayes algorithm and provides accompanying code.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nmy8atQK6fo1xFMG" /><figcaption>Photo by <a href="https://unsplash.com/@warrenumoh?utm_source=medium&amp;utm_medium=referral">Warren Umoh</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Table of Contents</h4><ol><li>Introduction</li><li>About Data</li><li>Data Preprocessing</li><li>Modeling</li><li>Performance Analysis</li><li>Conclusion</li><li>References</li></ol><h4>Introduction</h4><p>As Genomics and bioinformatics are thriving nowadays, analysis of DNA and RNA sequences is becoming a crucial part of medical research. The information obtained from these genetic materials offers a better understanding of the molecular basis of life and diseases.</p><p>A DNA molecule is a double helix structure consisting of two twisted paired strands, as you see in the cover photo. DNA consists of four bases which are adenine [A], cytosine [C], guanine [G], or thymine [T]. DNA sequence is a laboratory process of determining the sequence of these four bases in a DNA molecule<strong> [1].</strong></p><p>In this page, I would like to discuss how to use Machine Learning to predict the gene family associated with a particular DNA sequence. A Multinominal Naive Bayes model, which is trained on more than 4000 DNA sequences, is used for the same.</p><h4>About the Data</h4><p>The data is downloaded from (<a href="https://github.com/nageshsinghc4/DNA-Sequence-Machine-learning/blob/master/human_data.txt">https://github.com/nageshsinghc4/DNA-Sequence-Machine-learning/blob/master/human_data.txt</a>) which is publicly accessible. The text file is imported and converted to Pandas&#39; dataframe as below, and some portion of the data is displayed.</p><pre>import pandas as pd<br>with open(&#39;human_data.txt&#39;, &#39;r&#39;) as file: # import text file<br>    text_content = file.read()<br><br>lines = text_content.split(&#39;\n&#39;)# Initialize lists for features and labels<br>features = [] #empty list for features<br>labels = [] #empty list for features<br><br>for line in lines[1:]:# Skip the header line<br>    line_elements = line.split(&#39;\t&#39;)# Split each line into sequence and class using &#39;\t&#39; as the separator<br>    <br>    if len(line_elements) == 2:# Check if there are enough elements<br>        sequence, label = line_elements<br>        <br>        features.append(sequence)# Append the values to the lists<br>        labels.append(int(label)) <br>    else:<br>        print(f&quot;Skipping line due to insufficient elements.&quot;)<br><br>data = pd.DataFrame({&#39;feature&#39;:features ,&#39;label&#39;:labels})<br>pd.set_option(&#39;display.max_colwidth&#39;, None)<br>data(100)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*OFA6sGMfYXp4tTVOnvnr2w.png" /></figure><p>There are 4380 rows in the data, and sequences are of varying lengths<em>. </em><strong>Also, it is imbalanced, as you see in the below plot. I am proceeding without any resampling to check how well the model can work on the original data.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*NyrnoY6vyPE-8yl8UCJADg.png" /><figcaption>Imbalance in classes.</figcaption></figure><p>Gene families corresponding to each label are given below.</p><pre>    0: &#39;G protien coupled receptors&#39;,<br>    1: &#39;Tyrosine kinase&#39;,<br>    2: &#39;Tyrosine phosphatase&#39;,<br>    3: &#39;Synthatase&#39;,<br>    4: &#39;Synthase&#39;,<br>    5: &#39;Ion channel&#39;,<br>    6: &#39;Transcription factor&#39;</pre><h4>Data Preprocessing</h4><p>The data needs to be converted into numbers with the condition that it should catch short-term patterns in the sequence since it really matters in case of a DNA/RNA sequence. So, for preprocessing the data, techniques such as <strong>k-mers</strong><em> </em>and<em> </em><strong>count vectorization </strong>are used.</p><p><strong>k-mers: </strong>k-mers are substrings of a DNA/RNA sequence of length k which are mainly used in genomics and bioinformatics. For example, let the DNA sequence be <strong>ATGCCCCAACTA...</strong> k-mers for this sequence for k =6 will be <strong>ATGCCC, TGCCCC, GCCCCA, CCCCAA, CCCAAC, CCAACT, CAACTA.</strong> i.e., a sequence of 6 is extracted by shifting one position rightwards till the end of the sequence. The value of k-should be chosen in a way such that it can catch the pattern in the data properly. I have chosen k=6 for the process.</p><pre>from Bio.Seq import Seq<br>from Bio.SeqUtils import nt_search<br>&#39;&#39;&#39;function to generate k-mers&#39;&#39;&#39;<br>def generate_kmers(sequence, k):<br>    return [str(sequence[i:i+k]) for i in range(len(sequence) - k + 1)]<br>&#39;&#39;&#39;function to create dataframe&#39;&#39;&#39;<br>def create_dataframe_column_kmers(df, sequence_column, k):<br>    data[&#39;kmers&#39;] = data[&#39;feature&#39;].apply(lambda x: generate_kmers(Seq(x), k))<br>    return data<br>k_value = 6<br>&#39;&#39;&#39;create dataframe with sequence and k-mers as colums&#39;&#39;&#39;<br>dataDNA = create_dataframe_column_kmers(data, &#39;sequence&#39;, k_value)<br><br>dataDNA.head() # display fist row of dataframe</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*DBi7rWbBfuPzjwpiMohzkw.png" /></figure><p><strong>Count vectorization: </strong>The count vectorizer converts the text data to a sparse matrix that represents the occurrence or frequency of words in the document (substrings of sequence in this case).</p><pre>from sklearn.feature_extraction.text import CountVectorizer<br>&#39;&#39;&#39;create a column of text with k-mers&#39;&#39;&#39;<br>dataDNA[&#39;text&#39;] = dataDNA[&#39;kmers&#39;].apply(lambda x: &#39; &#39;.join(x))<br>vectorizer = CountVectorizer(analyzer=&#39;word&#39;, ngram_range=(1, 3))<br>&#39;&#39;&#39;fit and transform data to vectorizer&#39;&#39;&#39;<br>vectorizer.fit(dataDNA[&#39;text&#39;])<br>X = vectorizer.transform(dataDNA[&#39;text&#39;])<br>&#39;&#39;&#39;convert vectors to array&#39;&#39;&#39;<br>X_array = X.toarray()<br>y = data[&#39;label&#39;].to_numpy() # extract labels<br><br>print(X_array) # print and see vectorized data<br><br>[[1 0 0 ... 0 0 0]<br> [0 0 0 ... 0 0 0]<br> [0 0 0 ... 0 0 0]<br> ...<br> [4 1 0 ... 0 0 0]<br> [1 0 0 ... 0 0 0]<br> [0 0 0 ... 0 0 0]]</pre><p>As you can see above, sparse matrices are generated with count vectorizer. As the next step, data was split into train and test data.</p><pre>from sklearn.model_selection import train_test_split<br>trainSeq,testlSeq,trainLabels,testLabels = train_test_split(X_array,y,<br>                                                           test_size=0.2,<br>                                                           random_state=101)</pre><h4>Modeling</h4><p>Naive Baye’s model is based on Baye’s theorem, which explains conditional probability. For the discrete features, Multinominal NB (MNB) is used for classification. Hyperparameter alpha is the smoothing parameter that handles the zero probability issues while estimating class probabilities. fit_prior = True makes sure that prior probabilities are learned by the model. alpha = 0 is set to avoid additive smoothing. force_alpha = False ensures that alpha will not fall below 1e-10 which may cause numerical errors. For further details refer to [2].</p><pre>from sklearn.feature_extraction.text import TfidfVectorizer<br>from sklearn.naive_bayes import MultinomialNB<br><br>&#39;&#39;&#39;set up the model&#39;&#39;&#39;<br>model_0 = MultinomialNB(fit_prior=True, alpha = 0,  force_alpha=False)<br>&#39;&#39;&#39;fit the model&#39;&#39;&#39;<br>model_0.fit(trainSeq, trainLabels)</pre><h4>Performance Analysis</h4><pre>&#39;&#39;&#39;train accuracy&#39;&#39;&#39;<br>model_0.score(trainSeq , trainLabels)<br>0.997431506849315<br><br>&#39;&#39;&#39;test accuracy&#39;&#39;&#39;<br>model_0.score(valSeq , valLabels)<br>0.9372146118721462</pre><p>Test accuracy of 0.937 is quite impressive for a multi-class problem, given that the data is highly imbalanced. Let’s see the classification report of the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*naoYUKOMeKi_iCOjeYEonA.png" /><figcaption>Classification report of MNB</figcaption></figure><h4>Conclusion</h4><p>With a sequence of varying lengths and with an imbalance in data, the test result is quite satisfactory. With the upsampling of data, we may obtain better results as the prediction of minority classes is a bit low, which can be seen in the above classification report. Also, the parameter ngram_range The vectorizer has a major role in preprocessing, as increasing the range up to a limit may increase the performance of the model since it can capture the information in the pattern better.</p><p>So, the performance of the model can be increased by adjusting the above hyperparameters. Further analysis of model performance can be done considering a validation split in the data, which I have omitted in this work.</p><h4>References</h4><ol><li><a href="https://medium.com/mlearning-ai/apply-machine-learning-algorithms-for-genomics-data-classification-132972933723">Apply Machine Learning Algorithms for Genomics Data Classification | by Ernest Bonat, Ph.D. | MLearning.ai | Medium</a></li><li><a href="https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html">sklearn.naive_bayes.MultinomialNB — scikit-learn 1.3.2 documentation</a></li></ol><p>✉️ mkk.rakesh@gmail.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48fdaa65bcec" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/dna-sequence-classification-using-naive-bayes-algorithm-48fdaa65bcec">DNA Sequence Classification Using Naive Baye’s Algorithm</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>