<?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 Gozie Ibekwe on Medium]]></title>
        <description><![CDATA[Stories by Gozie Ibekwe on Medium]]></description>
        <link>https://medium.com/@igibekwe13?source=rss-6404d80b263------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EEU1x7faqCZ0yxcp</url>
            <title>Stories by Gozie Ibekwe on Medium</title>
            <link>https://medium.com/@igibekwe13?source=rss-6404d80b263------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:18:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@igibekwe13/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[Exorcising the demons of fatalism]]></title>
            <link>https://medium.com/@igibekwe13/exorcising-the-demons-of-fatalism-36553d47f171?source=rss-6404d80b263------2</link>
            <guid isPermaLink="false">https://medium.com/p/36553d47f171</guid>
            <category><![CDATA[motivation]]></category>
            <category><![CDATA[talking]]></category>
            <category><![CDATA[psychology]]></category>
            <dc:creator><![CDATA[Gozie Ibekwe]]></dc:creator>
            <pubDate>Fri, 24 Jan 2025 10:28:50 GMT</pubDate>
            <atom:updated>2025-01-24T10:28:50.413Z</atom:updated>
            <content:encoded><![CDATA[<h4>On the fine lines between luck and agency</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SSgu4ZxGHp9Q5097RUAlRg.png" /></figure><p>I’ve heard a lot of deep sayings cosplaying as regular conversation in my life. Some more than others, as usual. The one I’ll discuss here followed an angry barrage of questions from my mother about my performance in a particular term in Junior Secondary School. As you can probably guess, I had an absolute stinker that term (many more would follow in my senior years, but I digress). She took off her reading glasses, lines of worry crisscrossing her forehead dotted by a rueful grimace of disappointment. With a deep sigh, she whispered, “I was like you as a child, and look where it got me.”</p><p>Although I’d always been described as a precocious child, empathy was far from being functionally developed in my mind. In my adolescent brain, I never imagined a world where my mother would regret her life choices. After all, she was my mother. If she didn’t have all the right answers, then who did? Despite a staunchly Christian indoctrination which preached that only God had the powers of omniscience, 12-year-old me held to the childhood belief that the ability extended to parents.</p><p>Not to delve too deeply into my mother’s lore in case she ever stumbles across this, but she had a distinct flair for the Arts, harboring dreams of a career in Mass Communication. Undeniably brilliant — according to all accounts from my aunt and herself — and blessed with an annoyingly sharp memory, she was on the fast track to stardom if she could get the fundamentals right.</p><p>However, when gifted children aren’t put in a learning environment with enough intellectual competition, they tend to slack off and develop bad focusing habits. That basically describes the story of my mother’s formative years, and her lackadaisical attitude to self-application would end up costing her her dream career. <em>Note: She would eventually end up having a fulfilling 35-year career as a nurse, but that’s besides the point.</em></p><p>To bite into the meat of this issue in earnest, retelling her story underscores the importance of agency. Without painstaking planning and execution, your life can be completely thrown off the rails. Nobody thinks they can be the pitiful 45-year-old going through a crippling mid-life crisis, wondering what could have been and chasing highs they can never realistically reach. So how to avoid this? Let me cook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y0AFFkInVG_RQySA.jpeg" /><figcaption>Stay with me now.</figcaption></figure><p>As much as it physically pains me to admit this, the Gen-Z kids are not kids anymore. Where’d all the time go? I’m reminded of this excerpt from a poem I came across on TikTok some years ago:</p><p>“And suddenly it’s December,</p><p>And you’re not 17 anymore,</p><p>And you haven’t been 17 for a very long time”</p><p>Now, I’ve never been one to subscribe to mawkish literary pieces that try to cling to the flimsy cloth of childhood innocence. However, time flies, as they say, and the poem reminds me of my mother’s bittersweet life experiences I narrated earlier.</p><p>As I write this, I approach the dawn of my 23rd year on this Earth, and I finally understand the nuances of the message my mother was trying to pass across all those years ago. I’ll relate this to the entire Gen-Z populace at large, partly because I don’t feel like shouldering the sole responsibility for my outlook on life, but also because I feel this is relatable to the general demographic.</p><p>For the past year or thereabouts on social media, we’ve been fighting for our lives against the audaciously disrespectful iPad kids. Odds are you’ve come across someone in the comments of a trending TikTok casually mention their birth year (which is dangerously close to yours, btw) and get a truckload of replies asking what it was like marching for freedom in the civil rights era with Martin Luther King Jr.</p><p>As a result, I’ve seen more and more of us attempt to cling to the last vestiges of childhood. This has manifested primarily in the topic of frontal lobe development. Usually, I can see the humorous side of things, but this is probably the most unfunny “cutesy” joke ever created — that is, if people are joking around, and not being for real, as I suspect they are.</p><p>Anyways, the <a href="https://journeytocollege.mo.gov/when-does-the-brain-reach-maturity-its-later-than-you-think/">frontal lobe cortex argument</a> (kileleyi really?) is mostly brought up in the context of dating. It basically infantilizes anyone under the age of 25, the same way the “randomly gaining consciousness one day as a four-year-old” memes existed back in the day. While much of social media sentiment is pointless drivel not to be taken seriously, the adoption of the mantra speaks to a greater issue at play. Mentally, nobody wants to grow up, and the frontal lobe discussion attempts to postpone the intrinsic responsibilities of adulthood.</p><p>I mean, who can blame anybody with this outlook? It’s probably harder to get jobs now than ever before, especially in the tech space. Inflation is doing its best Christian Gray impression of the choke kink. You’re working harder than you’ve ever worked before, and yet, you’re worse off than ever before — physically, mentally, and financially (do I really need to explain T-Pain?). So, if society seems hell bent on wanting you to remain a dependent of your boomer parents, why not construct new ways to cling on to that reality? Swim with the tide, they say.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JdWovyCUCcUfhEsD.jpeg" /><figcaption>There is nothing we can do. Or is there?</figcaption></figure><p>If you’re like me (that is, in your seventh year of a five-year course, through no fault of your own — cheers ASUU), you’ve probably surrendered to the <em>Que Sera Sera</em> mantra seeping into your bones at 3AM when you try to build resolve to get your life together. Quite literally, whatever will be will be. Right?</p><p>Wrong. Doris Day’s heart was probably in the right place of soothing worries when she waxed the hit record back in 1956, but I don’t think she envisioned the level of fatalism that would emerge from it.</p><p>To relate this on a personal level, I’ve always felt like things would work out exactly how they should, and I’d always come out on top. (Note: I am aware this is called a God complex these days. No, I will not be seeking therapy.) This has been my brain’s status quo for the longest time, and it has consistently made me put in the barest minimum effort to succeed. It’s come clutch a lot, but it’s also caused its fair share of catastrophes.</p><p>I’d tell myself “it’ll all be fine in the end” and take whatever hand life dealt me, knowing full well I could have turned things around with a little more of a push. However, these chiding thoughts would quickly be replaced with the proverbial red pill that is fatalism. I’d gaslight myself into thinking I had nothing left to give, and I’d gotten what life decided to give me. Efforts, it seemed, were independent of Lady Luck’s randomly spun wheel of chance.</p><p>This mindset started to change when I came across <a href="https://www.tiktok.com/@thevictorokafor">Victor Okafor’s TikTok channel</a> sometime in September last year. He was performing a challenge he coined “Watch a Man Change”. Over a 90-day period, he tasked himself to accomplish a set of ridiculous goals through the sheer force of willpower. In his chosen fields, he would show up, come rain or shine, for three straight months.</p><p>The purpose of this challenge, according to him, was to see for himself what he could achieve if he gave it his all. What motivated this extremity? The same thing I mentioned earlier. He didn’t want to get to his forties and look back, wondering how differently his life would look if only he tried just a little harder.</p><p>The results were almost scary. Despite setting what appeared to be delulu-driven targets at the start, Victor accomplished, or got very close to accomplishing, all except one of them. While my reasons for watching the entire duration of his daily TikTok videos were different at the time (he’s a really engaging conversationalist), the challenge’s message resonated with me.</p><p>I’ve dreamed of creating an entirely new life through a daily grind to finally quell the flickering flame of inconsistency that has plagued me for so long. To this end, I even spoke about a detailed planning system as part of <a href="https://medium.com/@igibekwe13/out-with-the-old-a-forgettable-year-and-building-one-to-remember-a0d7d800ab4c">a modernized outlook on New Year Resolutions</a>. Unfortunately, I wasn’t able to imbibe the right habits for long enough.</p><p>That isn’t to say I regressed to the same fatalistic wretch I’d always been. I somewhat morphed into an unfinished article; unwavering dedication in some areas, woefully lacking conviction in others.</p><p>I found that the main mistake I made was patting myself on the back for the little I did do. Excuse the toxic rhetoric for a minute. I’m aware it goes against everything I wrote for <a href="https://open.substack.com/pub/thebell/p/issue-20-the-one-about-fighting-burnout">Salim’s wonderful mental health newsletter</a> back in August. Stay with me now.</p><p>The second I begin to praise myself for something, I immediately lose all motivation to see the job through to the end. For example, I recently resolved to start a daily learning grind in Machine Learning and Computer Vision. I started strong, digesting tons of material and adopting all the right attitudes. Then I started doing less and less. I’d stepped back from my hourly commitment to 45 minutes. After all, I have a job, a business venture, and a degree to attain. I should be allowed to slack off sometimes. I’m just a boy, yes?</p><p>45 became 30. 30 became 15. Eventually, 15 became none.</p><p>The point I’m trying to make is, complacency is one of the biggest killers of dreams, and it’s so expertly insidious it makes the cliche Russian sleeper agents in American spy movies look amateur in comparison. It tricks you into thinking you’re doing enough in light of your conditions, whatever they may be. The moment you accept this mentality and let your conditions dictate the effort you put in, you’ve lost.</p><p>I don’t know about you, but I’m a famously sore loser. So here’s how to make sure you snatch agency back from the jaws of complacency:</p><ul><li><strong>Abandon the “small wins” mindset:</strong> It’s a set up. You’ll pat yourself on the back and before you know it, you’ll fall asleep.</li><li><strong>Discard the “I no fit kill myself” mantra:</strong> You absolutely can (please don’t take this literally). Give everything you can and then some.</li><li><strong>Just do things:</strong> Act first, think later. Nike cooked with their “Just Do It” slogan. Gaslight yourself into thinking you can, and you will.</li></ul><p>Ultimately, I’ve discovered that I can’t be “just a boy” and get what I want out of life. Don’t infantilize yourself because it’s chic and in vogue. Fortnite makes you pay for fashionable skins and guns with V-Bucks. Life, on the other hand, makes you pay for being cute and easygoing with broken dreams.</p><p>Find agency and unlock the gems of compounding, for agency is luck in and of itself.</p><p>Ciao.</p><h3><strong>Note</strong></h3><p>I started <a href="https://gozieibekwe.substack.com/">a Substack</a> as part of my resolve to write more often. Join me over there xoxo.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36553d47f171" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Out With the Old: A Forgettable Year and Building One to Remember]]></title>
            <link>https://medium.com/@igibekwe13/out-with-the-old-a-forgettable-year-and-building-one-to-remember-a0d7d800ab4c?source=rss-6404d80b263------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0d7d800ab4c</guid>
            <category><![CDATA[year-in-review]]></category>
            <category><![CDATA[new-year-resolution]]></category>
            <category><![CDATA[life]]></category>
            <category><![CDATA[personal]]></category>
            <dc:creator><![CDATA[Gozie Ibekwe]]></dc:creator>
            <pubDate>Sat, 30 Dec 2023 16:57:41 GMT</pubDate>
            <atom:updated>2023-12-30T16:57:41.668Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Brief Recap of a largely wasted 2023 and musings for the future.</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D4ux_2HSoxlRQU9jDx6sXw.jpeg" /><figcaption>Hopeful theme song.</figcaption></figure><p>Anxiety. The fear of not doing enough (or even being enough) weighs on my subconscious more often and heavily than I&#39;d like to admit. Everything has a season, my mother would often say as I compared myself to my mates who seemed to be doing life better than me. Indeed, just as the festive season characterized by Santa Claus and his winter sled barrels towards its conclusion with the turn of the year, the flowers of anxiety are in full bloom.</p><p>Nobody likes performance reviews, not even the nerdy enthusiastic workaholic you roll your eyes at in your workplace. So, when the season for internal reflection that is the New Year comes around, it’s only natural to feel worked up, especially when the achievements section in your Year in Review is looking painfully scanty. But at least you’re alive. Amen? Amen.</p><p>I&#39;ve never done a recap before, and this year had no reason to be any different. You see, going into <a href="/service/tel:2023">2023</a>, I was bursting with excitement for my next trip around the sun on this little blue planet. I&#39;d just come off a spectacular Q4 in <a href="/service/tel:2022">2022</a>; getting my first job, signing a contract for another short-term gig, and making my first big boy purchase in my gaming laptop to name a few. I was climbing life&#39;s ladder two rungs at a time, and the wind of achievements were fanning my inner flames of glee. As much as I like to call myself a realist, I can also be an unfettered optimist, and I assumed my pen had many more pages of accomplishment to write in <a href="/service/tel:2023">2023</a>. Instead, this year had distinctly different plans - to keep me decidedly stagnant.</p><p>As I write this piece while sitting in my living room being serenaded by Jennifer Hudson’s passionate cries in <em>Ain’t No Way</em>, my status in everything is just about the same as I came into the year. Ain’t no way indeed, because if you told me I’d be in the same place in so many facets of life, I’d have laughed in your face. Now, as 2023 breathes its last labouring breaths, I am forced to confront reality. The anxiety of a wasted year is doubled by my looming 22nd birthday, and this brings me to what this piece is truly about: New Year Resolutions, as I’m determined to not ply the same route in 2024. I don’t want to make my way back from Mars at 22, as Five for Fighting mournfully sounded in <em>100 Years</em>, so I’ll be making drastic changes in my daily living.</p><h4><strong>Much Ado about New Year’s Resolutions</strong></h4><figure><img alt="Popular meme of basketball player J.R. Smith talking about the prospect of joining LeBron’s “superteam”" src="https://cdn-images-1.medium.com/max/1024/1*AhA8xa3h3iRIpw84yn8myg.jpeg" /><figcaption>Thinking about all the things I’ll do in 2024.</figcaption></figure><p>We&#39;re all familiar with the tradition of setting lofty goals on January 1st. We try to change habits, be nicer to people, and take our lives seriously. However, everything usually starts to return to factory settings a few weeks down the road, as the &quot;No be me kill Jesus&quot; mindset sets in. Life is already hard enough, after all. Fuel is about N700 per litre and 0-0-1 is the order of the day, so where&#39;s the time to truly focus on self-improvement? For the better disciplined amongst us, it might be months before you get sidetracked with life, or you might even be in the 1% that can grind towards your goals for a whole year. Congratulations. My mother would have loved you as a blender and told all her friends about you. But I digress.</p><p>The problem I&#39;ve found with goal setting isn&#39;t how high you&#39;re aiming or how hard you are (or aren&#39;t) working. It&#39;s all about the approach. The saying &quot;The journey of a thousand miles starts with a single step&quot; is my new mantra for this. The destination is what you have in mind, but you won&#39;t get there if you don&#39;t trudge along step by painful step. You see, the human mind needs to be directed constantly, because distractions will come. It&#39;s why every school uses a syllabus and timetables. There&#39;s a broader goal of passing some final exam, and the syllabus breaks the goal down into smaller sections, while the timetable assigns time to each subject.</p><p>Similarly, to-do lists work wonders for achieving goals. I know, this is kind of like me claiming to have discovered fire in <a href="/service/tel:2023">2023</a>. Truly riveting, revolutionary stuff. However, instead of only defining a lofty goal like buying a new car, it&#39;s easier to break it down to its smallest atom (yay Chemistry, I don&#39;t hate you so much anymore). Whatever the car costs, knowing exactly what you need to do on a monthly, weekly, and even daily basis goes a long way to making that dream Tesla becoming a reality.</p><h4>Making Necessary Changes</h4><p>In the spirit of breaking things down, I&#39;m trying this new thing where I&#39;m setting goals for quarters instead of the entire year. While I have an idea of where I want to be in a year, the quarter method is easier to plan in smaller blocks to eventually get there. Studies say it takes 21 days to form a new habit, and 90 for it to become a permanent lifestyle change. Every quarter has 90 days (give or take), and why have one big resolution when you can have four?</p><p>Importantly, I&#39;ll also be setting up a reward system for each milestone I accomplish, no matter how small. It could be with an episode viewing of Luffy and the Straw Hat Pirates in One Piece, my favourite anime, or even an indulgent childhood meal like Capri-Sun and Maryland cookies. The brain is like a cat with an attitude; it needs a good pat on the back for a task well-done. The behaviourism school of thought (more specifically, operant conditioning) has proven results with its rewards and punishments theme, and small rewards might be the way to unlock your potential. For punishments, I will not be there, life is already hard enough biko. However, you&#39;ll need to find what works for you to keep your natural indulgences in check.</p><p>Even as I dread the existential crisis that comes with my looming 22nd year, there&#39;s a renewed wave of hope being painted on the horizon. It remains to be seen if I can grab onto it long enough to change my life&#39;s present outlook, but I&#39;d be trying my hardest, and I hope this piece motivates you to, too.</p><p>Arrivederci,</p><p>Gozie.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0d7d800ab4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Image Classification of the Amazon Rainforest using Custom Convolutional Neural Networks]]></title>
            <link>https://medium.com/@igibekwe13/image-classification-of-the-amazon-rainforest-using-custom-convolutional-neural-networks-16ec01696c25?source=rss-6404d80b263------2</link>
            <guid isPermaLink="false">https://medium.com/p/16ec01696c25</guid>
            <category><![CDATA[streamlit-app-deployment]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[image-classification]]></category>
            <dc:creator><![CDATA[Gozie Ibekwe]]></dc:creator>
            <pubDate>Sat, 01 Oct 2022 01:18:40 GMT</pubDate>
            <atom:updated>2022-10-01T01:18:40.657Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Picture of the Amazon Rainforest" src="https://cdn-images-1.medium.com/max/1024/0*TJYoCHbBoDvjyf1Z.jpg" /></figure><h3>Introduction</h3><p>Convolutional Neural Networks (CNN) are currently the most advanced technique used in the interpretation of multidimensional information for tasks such as image classification and natural language processing. A CNN consists of an input layer, an output layer and multiple hidden layers.</p><p>Building a convolutional neural network from scratch can be some hard work. You’ve probably heard of transfer learning and the ways to apply it to a dataset of your choice, so why bother building one of your own? We learn by doing, and there’s a better ground understanding of the topic to be gained if you try your hand at it.</p><h3>Goal</h3><p>This project was aimed at classifying several cropped satellite images of the Amazon using multiple pre-defined labels such as atmospheric conditions and classes of land cover. The dataset can be found on <a href="https://www.kaggle.com/competitions/planet-understanding-the-amazon-from-space/data">Kaggle</a>.</p><p>Let’s begin.</p><h3>Importing Packages</h3><pre># Importing Required Libraries<br>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import cv2<br>import os<br>import tensorflow as tf<br>from keras.preprocessing.image import ImageDataGenerator<br>from tensorflow.keras.models import Sequential<br>from tensorflow.keras.layers import Dense, Flatten, BatchNormalization, Conv2D<br>from tensorflow.keras.optimizers import Adam<br>from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint<br>from tensorflow.keras.regularizers import l2<br>from tensorflow.keras.layers import MaxPooling2D<br>from tensorflow.keras.layers import Dropout</pre><p>Downloading the dataset from this <a href="https://www.kaggle.com/datasets/nikitarom/planets-dataset">link</a> reveals a zip file with several folders and .csv files inside it. The first step in any project is to view the dataset you’re working with. The dataset can be obtained using the following code.</p><pre>classes = pd.read_csv(r&quot;C:/Users/uchei/OneDrive/Desktop/Gozie/planet/planet/train_classes.csv&quot;)<br>classes.head()</pre><figure><img alt="First five rows of the dataset" src="https://cdn-images-1.medium.com/max/381/1*2pRA7cNQkQSKDThm365i5A.png" /></figure><h3>Data Preprocessing</h3><p>After loading the dataset, we can see that the ‘tags’ column is a bit difficult to make sense of. It contains different atmospheric conditions and land cover labeled for a specific image. Some images can have multiple classifications for land cover. For instance, train_1 has a clear atmospheric condition and the land cover is described as primary, water and agriculture.</p><p>To understand these tags and what they represent, click <a href="https://www.kaggle.com/competitions/planet-understanding-the-amazon-from-space/data">here</a>.</p><p>As with any multiclass dataset, we have to perform One Hot Encoding to separate these tags.</p><pre>#Creating a list of all labels contained in the dataframe<br>labels = set()<br>for i in range(len(classes)):<br>    tags = classes[&#39;tags&#39;][i].split(&#39; &#39;)<br>    labels.update(tags)<br>       <br>label_list = list(labels)<br># Add onehot features for every label in the list<br>for label in label_list:<br>    classes[label] = classes[&#39;tags&#39;].apply(lambda x: 1 if label in x.split(&#39; &#39;) else 0)</pre><figure><img alt="First five rows of the dataset with one hot encoding" src="https://cdn-images-1.medium.com/max/1008/1*jY0Ut3Qs-_NX2KrsmNS-VQ.png" /></figure><p>The dataset now has a column for each unique tag found in the ‘tags’ column. The value 0 indicates the tag is not present in a particular image, else 1 is assigned. There are 17 unique tags found in the tags column, so there are 17 columns.</p><p>The next step is to load the training data containing the images into our notebook. I worked locally with Jupyter Notebook for this project, so I used the directory where the files were contained to import them. For the notebook to successfully read the data as images, the Python package cv2 (OpenCV) was used.</p><pre>#loading all training data<br>rootdir = &#39;C:/Users/uchei/OneDrive/Desktop/Gozie/planet/planet/train-jpg&#39;<br>for subdir, dirs, files in os.walk(rootdir):<br>    for file in files:<br>        train = cv2.imread(os.path.join(subdir, file))</pre><p>Subsequently, the data was loaded into an ImageDataGenerator using a few parameters for augmentation to generate more images at the same time. Some users prefer to load the raw data first and train their models before augmenting and training a second time to improve accuracy. However, training a CNN model can take a very long time, especially if there are several layers in the network. Hence, I carried out loading and augmentation at the same time.</p><p>Also, I split the dataset into training and validation sets in an 80–20 split using the ‘subset’ parameter. The ImageDataGenerator is indeed a powerful tool.</p><pre>#Generating images and augmenting at the same time<br>train_image_generator =ImageDataGenerator(rescale=1/255,validation_split=0.2, rotation_range = 90, shear_range = 0.2, zoom_range = 0.2,<br>width_shift_range = 0.2, height_shift_range = 0.2, horizontal_flip = True)</pre><pre># generating the 80% training image data<br>train_generator = train_image_generator.flow_from_dataframe(dataframe=classes,<br>directory=rootdir, x_col=&#39;image_name&#39;, y_col=image_types,<br>target_size=(128, 128), class_mode=&#39;raw&#39;, seed=0, batch_size=32, subset=&#39;training&#39;)</pre><pre># generating the 20% validation image data<br>validation_generator = train_image_generator.flow_from_dataframe(dataframe=classes,<br>directory=rootdir, x_col=&#39;image_name&#39;, y_col=image_types, target_size=(128, 128), class_mode=&#39;raw&#39;, seed=0, batch_size=32, subset=&#39;validation&#39;)</pre><p>With the loading task complete, we need to calculate the number of steps our model would need to cover all the samples in our data. This can be achieved with the code below.</p><pre>train_step_size = int(np.ceil(train_generator.samples / train_generator.batch_size))<br>val_step_size = int(np.ceil(validation_generator.samples / train_generator.batch_size))</pre><p>For the dataset mentioned above, a custom accuracy function called fbeta was used in the competition on Kaggle. The function fbeta can be defined as follows.</p><pre>#defining accuracy function<br>def fbeta(y_true , y_pred, beta=2, epsilon=1e-4):<br>    squared_beta = beta**2</pre><pre>y_true = tf.cast(y_true, tf.float32)<br>    y_pred = tf.cast(tf.greater(tf.cast(y_pred, tf.float32), tf.constant(0.2)), tf.float32)<br>        <br>    tp = tf.reduce_sum(y_true * y_pred, axis=1)<br>    fp = tf.reduce_sum(y_pred, axis=1) - tp<br>    fn = tf.reduce_sum(y_true, axis=1) - tp<br>    <br>    p = tp/(tp+fp+epsilon)   #precision<br>    r = tp/(tp+fn+epsilon)   #recall<br>    <br>    fb = (1+squared_beta)*p*r / (squared_beta*p + r + epsilon)<br>    return fb</pre><h3>Model Building</h3><p>As with any model, we want to make sure our model fits as well as possible — without overfitting. For tasks in Tensorflow, the ModelCheckpoint and EarlyStopping packages were made to stand as checks against overfitting.</p><pre>#Creating checkpoints to prevent model overfitting<br>checkpoint = ModelCheckpoint(filepath=&#39;weight_model.hdf5&#39;,monitor=&#39;val_fbeta&#39;, <br>                             save_best_only=True, save_weights_only=True, mode=&#39;max&#39;, verbose = 1)<br>early = EarlyStopping(monitor=&#39;val_fbeta&#39;, min_delta=0, patience=5, verbose=1, mode=&#39;max&#39;)</pre><p>ModelCheckpoint would save the model in a hdf5 file each time there was an improvement in fbeta accuracy, while EarlyStopping would end the training process if validation loss did not drop for five consecutive epochs.</p><p>Now, let’s build our custom convolutional neural network.</p><pre>#Defining training parameters<br>planet_model = Sequential([<br>    Conv2D(32, kernel_size = 3, kernel_regularizer = l2(0.0005), strides = (1,1), padding = &#39;valid&#39;, activation = &#39;relu&#39;,input_shape = (128, 128, 3)),<br>    MaxPooling2D(pool_size = (2,2)),<br>    BatchNormalization(),<br>    Conv2D(64, 3, activation = &#39;relu&#39;),<br>    MaxPooling2D(pool_size = (2,2)),<br>    BatchNormalization(),<br>    Conv2D(128, 3, activation = &#39;relu&#39;),<br>    MaxPooling2D(pool_size = (2,2)),<br>    BatchNormalization(),<br>    Flatten(),<br>    Dense(512, activation = &#39;relu&#39;),<br>    Dense(17, activation = &#39;sigmoid&#39;)<br>])<br>planet_model.compile(loss = &#39;binary_crossentropy&#39;, optimizer = Adam(learning_rate=0.0001),metrics = [fbeta])</pre><p>One thing to note is the first Conv2D layer (called the input) has to take in the same shape as the target_size parameter in the ImageDataGenerator. A MaxPooling2D layer follows each Conv2D to reduce the dimensions of the feature maps created by the convolutional layers. It helps to increase accuracy and reduce training time, as there are less features for the model to train.</p><p>After each MaxPool layer follows a BatchNormalization layer. This layer prevents overfitting and also serves to speed up training time. Once all the Conv2D layers have successfully been built, the Flatten function follows to flatten the input into a single dimension. We connect the result to a Fully Connected Dense layer, which is then connected to the output dense layer. The number of neurons in the output dense layer has to correspond with the number of unique labels in the dataset. We then train our custom model.</p><pre>#training the model<br>history = planet_model.fit(train_generator,<br>                             epochs = 20,<br>                             steps_per_epoch = train_step_size,<br>                             validation_data = validation_generator,<br>                             validation_steps = val_step_size,<br>                             callbacks = [checkpoint, early])</pre><figure><img alt="Model training in epochs" src="https://cdn-images-1.medium.com/max/995/1*eDDeyT7OfjkX91LUbne85A.png" /></figure><p>The model trains up to the very last epoch, obtaining an fbeta validation accuracy of 88.36%, which is even higher than the training set. Save your model for use later with this code.</p><pre>planet_model.save(&#39;model.hdf5&#39;)</pre><h3>Deployment</h3><p>Great work! Building a model is never easy. Now comes the next step; deployment. It’s always good practice to deploy a trained model. People need to see what you’ve been working on!</p><p>This was my very first experience with deploying a model and it proved to be really challenging but I stuck through it. I made use of the Streamlit app and I’ll break down the process nice and easy.</p><p>For starters, the code for a Streamlit application needs to be written in a separate Python file. This can be done with VSCode. Be sure to import all the relevant packages and defined functions to this new Python file to avoid any errors.</p><p>One challenge I faced was how to get the model to print out the labels it had predicted. To solve this problem, I took inspiration from the decode_predictions function found in the ResNet model <a href="https://martin-thoma.com/image-classification/">here</a>. I created a dictionary containing all the custom labels contained in my dataset and defined my custom function below.</p><pre>def decode_predictions(preds, top = 17, class_list_path = index_list):<br>    if len(preds.shape) != 2 or preds.shape[1] != 17:<br>        raise ValueError(&#39;`decode_predictions` expects &#39;<br>                         &#39;a batch of predictions &#39;<br>                         &#39;(i.e. a 2D array of shape (samples, 17)). &#39;<br>                         &#39;Found array with shape: &#39; + str(preds.shape))<br>    listOfIndexes = index_list<br>    results = []<br>    for pred in preds:<br>        top_indices = pred.argsort()[-top:][::-1]<br>        result = [tuple(listOfIndexes[str(i)]) + (pred[i],) for i in top_indices]<br>        result.sort(key = lambda x: x[2], reverse = True)<br>        results.append(result)<br>    return results</pre><p>Using this function, I built the Streamlit app with the following code.</p><pre>#Creating Streamlit app<br>%%writefile app2.py<br>import streamlit as st<br>import tensorflow as tf<br>import io<br>from PIL import Image, ImageOps<br>import numpy as np<br>import cv2<br>from tensorflow.keras.preprocessing.image import load_img</pre><pre><a href="http://twitter.com/st">@st</a>.cache(allow_output_mutation = True)<br>def load_model():<br>    model = tf.keras.models.load_model(&#39;model3.hdf5&#39;, custom_objects = {&quot;fbeta&quot;: fbeta})<br>    return model<br>with st.spinner(&#39;Model is being loaded...&#39;):<br>    model = tf.keras.models.load_model(&#39;model3.hdf5&#39;, custom_objects = {&quot;fbeta&quot;: fbeta})<br>    st.write(&quot;&quot;&quot;# Image Classification&quot;&quot;&quot;)<br>    file = st.file_uploader(&quot;Upload the image to be classified&quot;, type = [&quot;jpg&quot;, &quot;png&quot;, &quot;jpeg&quot;])<br>    st.set_option(&#39;deprecation.showfileUploaderEncoding&#39;, False)</pre><pre>if file is None:<br>    st.text(&quot;Please upload an image&quot;)<br>elif file is not None:<br>    img = Image.open(file)<br>    file_name = file.name<br>    st.image(img, use_column_width = True)<br>    with open(file_name, &quot;wb&quot;) as f:<br>        f.write(file.getbuffer())</pre><pre>img2 = load_img(file_name, target_size = (128,128))<br>img2 = np.array(img2)<br>img2 = img2 / 255.0<br>img2 = img2.reshape(1, 128, 128, 3)<br>prediction = model.predict(img2)<br>pred_class = decode_predictions(prediction, top = 3)<br>image_class = str(pred_class[0][0][1])<br>score = pred_class[0][0][2]<br>for idn, name, likelihood in pred_class[0]:<br>    st.write(&quot;The image is classified as: {} - {:2f}&quot;.format(name, likelihood))<br>    st.write(&quot;The top predicted class is&quot;, image_class, &quot;with a similarity score of&quot;, score)</pre><p>The Streamlit application is complete now! We can run the app with the code below in our command prompt.</p><pre>streamlit run app2.py</pre><figure><img alt="View of the deployed model predicting classes of an image" src="https://cdn-images-1.medium.com/max/834/1*ra0pfI2dlKs0Qa-ilqpW6A.png" /></figure><p>Our app prints the top three predictions of the model. Pretty cool right?</p><h3>Conclusion</h3><p>Building a custom model gets easier with practice, so get some reps in with your models. You can find my GitHub repo <a href="https://github.com/Gozie02/Convolutional-Amazon-Model">here</a> and the web deployed Streamlit app <a href="https://gozie02-convolutional-amazon-model-app2-71shcm.streamlitapp.com/">here</a>.</p><p>Happy building and thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16ec01696c25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multivariate Time Series Analysis on Air Quality using Random Forest Regressor]]></title>
            <link>https://medium.com/@igibekwe13/multivariate-time-series-analysis-on-air-quality-using-random-forest-regressor-cac06610d93f?source=rss-6404d80b263------2</link>
            <guid isPermaLink="false">https://medium.com/p/cac06610d93f</guid>
            <category><![CDATA[random-forest]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Gozie Ibekwe]]></dc:creator>
            <pubDate>Fri, 09 Sep 2022 12:42:25 GMT</pubDate>
            <atom:updated>2022-09-09T12:42:25.240Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Time Series is a type of sequential data containing data points mapped at a certain successive duration of time. This can vary in units of time, from seconds to hours, days to weeks and months to years. Time Series is used across different sectors to see how a certain variable changes with time. For example, the most popular use of time series is seen in financial markets to monitor changes in price and volume of a specific commodity and give insight into patterns or trends of that commodity.</p><p>There are two types of time series analysis: univariate and multivariate. While univariate time series analysis is concerned with predicting only a single variable, multivariate is broader and more applicable to the real world, as it takes other variables into account that may affect the value of the dependent variable.</p><p>In our case, we are interested in monitoring the levels of pollutants contained in air across different laboratory stations in Madrid. The dataset was obtained from the <a href="https://www.kaggle.com/datasets/decide-soluciones/air-quality-madrid?select=stations.csv">Air Quality</a> dataset on Kaggle, as part of my internship projects. This dataset was a bit difficult to work with, as I had never previously dealt with Time Series Analysis.</p><p>In fact, I had been avoiding Time Series due to its difficult reputation in the data science community. However, necessity proved again to be the mother of invention, and I had to complete this project as part of my duties in the aforementioned internship. It stretched my functional coding knowledge and challenged me to do more, thus I am grateful for the experience and the steep learning curve.</p><h3>Goal</h3><p>I spent a long time thinking about my problem statement, which I eventually decided would be predicting the levels of Carbon Monoxide (CO), as it is one of the better known hazardous air pollutants in the world, using the levels of other pollutants.</p><h3>Importing Packages</h3><pre><strong>import</strong> pandas <strong>as</strong> pd<br><strong>import</strong> glob<br><strong>from</strong> sklearn.experimental <strong>import</strong> enable_iterative_imputer<br><strong>from</strong> sklearn.impute <strong>import</strong> IterativeImputer<br><strong>from</strong> sklearn <strong>import</strong> linear_model<br><strong>import</strong> matplotlib.pyplot <strong>as</strong> plt<br><strong>import</strong> seaborn <strong>as</strong> sns<br><strong>from</strong> sktime.utils.plotting <strong>import</strong> plot_series<br><strong>from</strong> sklearn.feature_selection <strong>import</strong> RFE<br><strong>from</strong> sklearn.ensemble <strong>import</strong> RandomForestRegressor<br><strong>import</strong> numpy <strong>as</strong> np<br><strong>from</strong> statsmodels.tsa.stattools <strong>import</strong> adfuller<br><strong>from</strong> numpy <strong>import</strong> log<br><strong>from</strong> sklearn.metrics <strong>import</strong> mean_absolute_error<br><strong>from</strong> sklearn.metrics <strong>import</strong> mean_squared_error</pre><h3>Data Preprocessing</h3><p>This dataset was broken into several files, one for each year spanning from 2001 to 2018. Each file contained a date column along with a column for each of the 17 recorded pollutants. A column was also dedicated to station code.</p><p>The first thing to be done was to concatenate the files for all the years, to enable me to get a better clue of my next move. I used the glob package on Python for this, which is used to return all file paths that match a specific pattern in a specified directory. I then passed the “globbed” files into a single dataframe.</p><pre><em>#Using the glob package to read all files with datatype csv contained in the folder</em><br>files <strong>=</strong> glob<strong>.</strong>glob(&quot;C:/Users/uchei/OneDrive/Desktop/csvs_per_year/csvs_per_year**/*.csv&quot;)<br><br>df <strong>=</strong> pd<strong>.</strong>DataFrame()<br><strong>for</strong> f <strong>in</strong> files:<br>    csv <strong>=</strong> pd<strong>.</strong>read_csv(f)<br>    df <strong>=</strong> df<strong>.</strong>append(csv)</pre><p>Let’s see how the dataframe looked using a basic command.</p><figure><img alt="View of the first 5 observations of the concatenated dataset" src="https://cdn-images-1.medium.com/max/1019/1*ooke5j1SJIkSq9nQDcovFA.png" /></figure><p>Something to note would be that the same date and time appears multiple times in this output. This is due to different station codes as seen in the “Station” column. There are also quite a few null values contained in this short simple snippet, but we’ll deal with them in just a bit. Continuing with basic investigation, I noticed the data type of the “date” column was an object and needed to be converted to datetime, as shown below.</p><pre><em>#date shouldn&#39;t be an object dtype, change to datetime</em><br>df[&#39;date&#39;] <strong>=</strong> pd<strong>.</strong>to_datetime(df<strong>.</strong>date, infer_datetime_format <strong>=</strong> <strong>True</strong>)</pre><p>Next, I had to be sure there was uniformity in the measurements of levels of pollution. I did this on the Kaggle website, linked above, and saw that there were a few discrepancies which needed to be addressed.</p><pre><em>#Divide columns by 1000 to get desired unit (</em>μgm-3)<br>df[&#39;CO&#39;] <strong>=</strong> df[&#39;CO&#39;]<strong>/</strong>1000<br>df[&#39;TCH&#39;] <strong>=</strong> df[&#39;TCH&#39;]<strong>/</strong>1000<br>df[&#39;CH4&#39;] <strong>=</strong> df[&#39;CH4&#39;]<strong>/</strong>1000</pre><p>Done with that task, I needed to figure out how to address the issue of null values. After a bit of consideration and idle data manipulation, I found that after grouping the dataset by station, some stations did not contain any observations for certain gases. Hence, the only way to treat these values to gain proper insight would be after separation by station.</p><pre>df_stations <strong>=</strong> df<strong>.</strong>groupby(&#39;station&#39;)<br>casa_de_campo <strong>=</strong> df_stations<strong>.</strong>get_group(28079024)<br>casa_de_campo<strong>.</strong>index <strong>=</strong> casa_de_campo<strong>.</strong>date<br>casa_de_campo <strong>=</strong> casa_de_campo<strong>.</strong>drop([&#39;date&#39;], axis <strong>=</strong> 1)<br>casa_de_campo<strong>.</strong>head()</pre><figure><img alt="View of the dataset grouped by station code" src="https://cdn-images-1.medium.com/max/1007/1*gvsmVNpLI1425roPEPopdg.png" /></figure><p>I selected a station code at random of the 39 unique station codes contained in the entire dataset. Also, I passed the date column to become an index for ease of visualization to be handled later. This station, called Casa de Campo, contains about 150k observations and 17 columns, excluding the “Station” column, which was uniform throughout the grouping. The date index also makes sense sequentially now, as there are values being recorded every hour in each column.</p><h3>Missing Value Treatment</h3><p>I handled the null values using elimination by magnitude of missing value percentage and skewness. Some columns were simply lacking too many values to be considered useful, as I set my benchmark for retention of columns at 50% recorded data.</p><pre>casa_de_campo<strong>.</strong>isnull()<strong>.</strong>sum()<strong>/</strong>151416 <strong>*</strong> 100</pre><figure><img alt="Percentage of missing values per column in the grouped dataset" src="https://cdn-images-1.medium.com/max/285/1*ud0-jcxtxZPjdv9fy5rQ8w.png" /><figcaption>Percentage of missing values per column.</figcaption></figure><pre>casa_de_campo <strong>=</strong> casa_de_campo<strong>.</strong>drop([&#39;MXY&#39;, &#39;OXY&#39;, &#39;PXY&#39;, &#39;NO&#39;, &#39;CH4&#39;, &#39;station&#39;], axis <strong>=</strong> 1)</pre><p>The “Station” column also got dropped because it would be redundant in modeling as all the values are the same. In the case of skewness, columns with a skewed value of 0.5 or lower had their missing values imputed with their mean.</p><pre>casa_de_campo<strong>.</strong>skew()</pre><p>Those above that threshold of skewness were imputed with medians, depending on their total percentage of missing data.</p><pre>#Skewness under 0.5<br>casa_de_campo[&#39;O_3&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;O_3&#39;]<strong>.</strong>mean(), inplace <strong>=</strong> <strong>True</strong>)</pre><pre>#Skewness above 0.5<br>casa_de_campo[&#39;CO&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;CO&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)<br>casa_de_campo[&#39;NMHC&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;NMHC&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)<br>casa_de_campo[&#39;NO_2&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;NO_2&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)<br>casa_de_campo[&#39;PM10&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;PM10&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)<br>casa_de_campo[&#39;SO_2&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;SO_2&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)<br>casa_de_campo[&#39;TCH&#39;]<strong>.</strong>fillna(casa_de_campo[&#39;TCH&#39;]<strong>.</strong>median(), inplace <strong>=</strong> <strong>True</strong>)</pre><p>A third group, containing a higher percentage of missing values and skewness over 4, was imputed using the Bayesian Ridge linear estimator in the MICE Iterative Imputer in backward-fill.</p><pre><em># Define MICE Imputer and fill missing values for all other columns</em><br>mice_imputer <strong>=</strong> IterativeImputer(estimator<strong>=</strong>linear_model<strong>.</strong>BayesianRidge(), n_nearest_features<strong>=None</strong>, imputation_order<strong>=</strong>&#39;ascending&#39;)<br><br>campo_imputed <strong>=</strong> pd<strong>.</strong>DataFrame(mice_imputer<strong>.</strong>fit_transform(casa_de_campo), columns<strong>=</strong>casa_de_campo<strong>.</strong>columns, index <strong>=</strong> casa_de_campo<strong>.</strong>index)</pre><p>The next step was to determine stationarity of the new dataset which is now free from null values.</p><pre><em>#Checking stationarity of data</em><br>result <strong>=</strong> adfuller(campo_imputed[&#39;CO&#39;]<strong>.</strong>values)<br>print(&#39;p-value: %f&#39; <strong>%</strong> result[1])</pre><pre>p-value: 0.000000</pre><p>The p-value appears very minute. Since the value is below 0.05, we can assume the CO column is stationary, meaning the data does not need further transformation.</p><p>After this short test, for the purpose of maximizing accuracy, the data was resampled on a weekly basis. This was done using the average level of pollutants across a week of provided data.</p><pre><em>#Get the mean of values in the dataset on a weekly basis</em><br>weekly_campo <strong>=</strong> campo_imputed<strong>.</strong>resample(&quot;W&quot;)<strong>.</strong>mean()<br>weekly_campo<strong>.</strong>head()</pre><figure><img alt="View of the first five observations of the grouped data resampled on a weekly timeframe." src="https://cdn-images-1.medium.com/max/854/1*5A1rDaTBzvb6ewyQi39qgA.png" /></figure><p>We must remember that visualizations are an extremely important part of data science, to grant understanding at a single glance. Following this principle, I made use of the plot_series package contained in sktime.</p><pre>plot_series(weekly_campo[&#39;CO&#39;], x_label <strong>=</strong> &#39;Date&#39;, y_label <strong>=</strong> &#39;Levls of Carbon Monoxide (μgm-3) &#39;)</pre><figure><img alt="Plot of levels of carbon monoxide in weeks from 2001 to 2018" src="https://cdn-images-1.medium.com/max/995/1*QWsrqeubvnQjA6vUiqr8ZA.png" /></figure><p>We can see from this graph that the levels of Carbon Monoxide emissions from the Casa de Campo station have reduced significantly from the high levels seen in the early 2000s.</p><h3>Feature Selection</h3><p>As always, when selecting features to train a model, you want independent variables that actually have an impact on the dependent variable. A common way to determine the independent variables to select is the correlation heatmap. Let’s see one for this dataset.</p><pre>fig, ax <strong>=</strong> plt<strong>.</strong>subplots(figsize<strong>=</strong>(10,5)) <br>sns<strong>.</strong>heatmap(weekly_campo<strong>.</strong>corr(), annot <strong>=</strong> <strong>True</strong>, ax <strong>=</strong> ax)<br>plt<strong>.</strong>show()</pre><figure><img alt="Correlation heatmap of all columns in the weekly resampled group data" src="https://cdn-images-1.medium.com/max/586/1*r1XfLmnXObDDtmNCJlF0TA.png" /></figure><p>Our target variable is CO, so we can focus on that column alone and ignore the others. From this heatmap, we can tell that the pollutant NMHC has almost no correlation with CO. Also, PM10 (particulate matter) is on the low side of Pearson’s coefficient. O_3 (Ozone), despite being negatively correlated, is selected because its effect is significant on the levels of CO (Carbon Monoxide).</p><pre>corr_weekly <strong>=</strong> weekly_campo<strong>.</strong>drop([&#39;NMHC&#39;, &#39;PM10&#39;], axis <strong>=</strong> 1)<br>corr_weekly<strong>.</strong>head()</pre><figure><img alt="View of first five observations of well correlated columns of the resampled data" src="https://cdn-images-1.medium.com/max/745/1*pRhpXxjBzbgQztpfwo6LwA.png" /></figure><p>Subsequently, to obtain even higher model accuracy, I created a fresh dataframe to introduce lag variables. Since the previous dataframe was resampled on a weekly timeframe, I chose 54 lag variables on the target data, CO, to work with as this is the ideal number of lags to give weekly data. Lag variables are based on the past values of the dependent variable.</p><pre><em>#Creating lag variables</em><br>dataframe <strong>=</strong> pd<strong>.</strong>DataFrame()<br><em>#On a weekly basis, the ideal number of lags should be 54.</em><br><strong>for</strong> i <strong>in</strong> range(54, 0, <strong>-</strong>1):<br>    dataframe[&#39;t-&#39; <strong>+</strong> str(i)] <strong>=</strong> corr_weekly<strong>.</strong>CO<strong>.</strong>shift(i)<br><em>#Combine the lagged dataframe with the original</em><br>lagged_data <strong>=</strong> pd<strong>.</strong>concat([corr_weekly, dataframe], axis <strong>=</strong> 1)<br>lagged_data<strong>.</strong>dropna(inplace <strong>=</strong> <strong>True</strong>)</pre><h3>Modeling</h3><p>I manually sliced the data (Yes, this can be done using time split as well) into the training and testing sets, using the final 26 weeks of the dataset as the testing set.</p><pre><em>#Slice the data into train and test</em><br><em>#The testing slice is for the final 26 weeks of the dataset</em><br>train <strong>=</strong> lagged_data<strong>.</strong>loc[&#39;2002-01-20&#39; : &#39;2017-11-05&#39;]<br>test <strong>=</strong> lagged_data<strong>.</strong>loc[&#39;2017-11-12&#39; : &#39;2018-05-06&#39;]<br>x_test <strong>=</strong> test<strong>.</strong>loc[:, test<strong>.</strong>columns <strong>!=</strong> &#39;CO&#39;]<br>y_test <strong>=</strong> test[&#39;CO&#39;]<br>x_train <strong>=</strong> train<strong>.</strong>loc[:, train<strong>.</strong>columns <strong>!=</strong> &#39;CO&#39;]<br>y_train <strong>=</strong> train[&#39;CO&#39;]</pre><p>I chose Random Forest Regressor as my ensemble technique, while selecting only 13 of the most important features using Recursive Feature Elimination (RFE).</p><pre>print(&quot;Training model...&quot;)<br>reg_forest = RandomForestRegressor(n_estimators = 100, random_state = 42)<br>rfe_weekly = RFE(estimator = reg_forest, n_features_to_select = 13)<br>fit_weekly = rfe_weekly.fit(x_train, y_train)<br>predictions1 = fit_weekly.predict(x_test)</pre><h3>Model Evaluation</h3><p>To evaluate the performance of this model, I employed MAPE to get an accuracy percentage value. Bias was also utilized.</p><pre>correct <strong>=</strong> np<strong>.</strong>array(y_test)<br>sumvalue <strong>=</strong> np<strong>.</strong>sum(correct)<br>mape <strong>=</strong> np<strong>.</strong>sum(np<strong>.</strong>abs((correct <strong>-</strong> predictions1))) <strong>/</strong> sumvalue <strong>*</strong> 100<br>accuracy <strong>=</strong> 100 <strong>-</strong> mape<br>print(&#39;Accuracy:&#39;, round(accuracy, 2), &#39;%.&#39;)</pre><pre>Accuracy: 90.26 %.</pre><p>As calculated using MAPE, our model has an error of 9.74% in predicting actual values. In time series forecasting, according to <a href="https://www.researchgate.net/publication/257812432_Using_the_R-MAPE_index_as_a_resistant_measure_of_forecast_accuracy">Montano and Palmer et al</a>, this can be interpreted as a highly accurate forecast.</p><pre><em>#calculating bias</em><br>errors <strong>=</strong> [correct[i] <strong>-</strong> predictions1[i] <strong>for</strong> i <strong>in</strong> range(len(correct))]<br>bias <strong>=</strong> sum(errors) <strong>*</strong> 1.0<strong>/</strong>len(correct)<br>print(&#39;Bias: %f&#39; <strong>%</strong> bias)</pre><pre>Bias: -0.000008</pre><p>Our bias is very low, however, the negative sign preceding the bias value tells us our model has over forecast. This means our model is predicting values that are higher than the actual values.</p><p>In order to plot the sktime graph containing the predictions, some processing had to be done, as the plot_series function does not accept arrays and needs the index types of two dataframes to match.</p><pre><em>#As the predictions are in array,  we need to convert them to a DataFrame in order for the plot_series function to read it</em><br>predicted_df <strong>=</strong> pd<strong>.</strong>DataFrame(predictions1)<br>predicted_df[&#39;date&#39;] <strong>=</strong> y_test<strong>.</strong>index<br>predicted_df[&#39;date&#39;] <strong>=</strong> pd<strong>.</strong>to_datetime(predicted_df<strong>.</strong>date, infer_datetime_format <strong>=</strong> <strong>True</strong>)<br>predicted_df<strong>.</strong>index <strong>=</strong> predicted_df[&#39;date&#39;]<br>predicted_df <strong>=</strong> predicted_df<strong>.</strong>drop([&#39;date&#39;], axis <strong>=</strong> 1)<br>predicted_df<strong>.</strong>index <strong>=</strong> pd<strong>.</strong>to_datetime(predicted_df<strong>.</strong>index)</pre><p>Now, we could successfully employ the plot_series function.</p><pre>plot_series(y_train, y_test, predicted_df, x_label <strong>=</strong> &#39;Date&#39;, y_label <strong>=</strong> &#39;CO Levels (μgm-3)&#39;, labels <strong>=</strong> [&#39;Weeks&#39;, &#39;Actual CO Levels (μgm-3)&#39;, &#39;Predicted Levels (μgm-3)&#39;]</pre><figure><img alt="Plot of predicted values superimposed on actual values." src="https://cdn-images-1.medium.com/max/1000/1*gCf_EtZP79QU5Z9LcRBakg.png" /><figcaption>Predicted Values superimposed on graph of actual values</figcaption></figure><p>Although we have the accuracy figure, this graph looks a bit granular and is hard to make out due to the extensive number of data points contained. It can be hard to tell the story of our findings if the visualization does not communicate clearly.</p><p>To solve this problem, we can use a less granular resampling method. If the data is resampled on a monthly basis, the graph becomes clearer but at a cost of accuracy, which drops to 86.66% as shown below.</p><pre>monthly_campo <strong>=</strong> campo_imputed<strong>.</strong>resample(&#39;M&#39;)<strong>.</strong>mean()<br>relevant_monthly <strong>=</strong> monthly_campo<strong>.</strong>drop([&#39;NMHC&#39;, &#39;PM10&#39;], axis <strong>=</strong> 1)<br><em>#Creating lag variables</em><br>dataframe <strong>=</strong> pd<strong>.</strong>DataFrame()<br><strong>for</strong> i <strong>in</strong> range(12, 0, <strong>-</strong>1):<br>    dataframe[&#39;t-&#39; <strong>+</strong> str(i)] <strong>=</strong> relevant_monthly<strong>.</strong>CO<strong>.</strong>shift(i)<br>lagged_monthly <strong>=</strong> pd<strong>.</strong>concat([relevant_monthly, dataframe], axis <strong>=</strong> 1)<br>lagged_monthly<strong>.</strong>dropna(inplace <strong>=</strong> <strong>True</strong>)<br><em>#Adding seasonal variables</em><br>train1 <strong>=</strong> lagged_monthly<strong>.</strong>loc[&#39;2002-01-31&#39; : &#39;2017-11-30&#39;]<br>test1 <strong>=</strong> lagged_monthly<strong>.</strong>loc[&#39;2017-12-31&#39; : &#39;2018-05-31&#39;]<br>x_test1 <strong>=</strong> test1<strong>.</strong>loc[:, test1<strong>.</strong>columns <strong>!=</strong> &#39;CO&#39;]<br>y_test1 <strong>=</strong> test1[&#39;CO&#39;]<br>x_train1 <strong>=</strong> train1<strong>.</strong>loc[:, train1<strong>.</strong>columns <strong>!=</strong> &#39;CO&#39;]<br>y_train1 <strong>=</strong> train1[&#39;CO&#39;]<br>print(&quot;Training model...&quot;)<br>monthly_reg = RandomForestRegressor(n_estimators = 100, random_state = 42)<br>monthly_rfe = RFE(estimator = monthly_reg, n_features_to_select = 9)<br>monthly_fit = monthly_rfe.fit(x_train1, y_train1)<br>monthly_predictions = monthly_fit.predict(x_train1)</pre><p>Creating the recognized format for plot_series.</p><pre>df1 <strong>=</strong> pd<strong>.</strong>DataFrame(monthly_predictions)<br>df1[&#39;date&#39;] <strong>=</strong> y_test1<strong>.</strong>index<br>df1[&#39;date&#39;] <strong>=</strong> pd<strong>.</strong>to_datetime(df1<strong>.</strong>date, infer_datetime_format <strong>=</strong> <strong>True</strong>)<br>df1<strong>.</strong>index <strong>=</strong> df1[&#39;date&#39;]<br>df1 <strong>=</strong> df1<strong>.</strong>drop([&#39;date&#39;], axis <strong>=</strong> 1)<br>df1<strong>.</strong>index <strong>=</strong> pd<strong>.</strong>to_datetime(df1<strong>.</strong>index)<br>plot_series(y_train1, y_test1, df1, labels <strong>=</strong> [&#39;Months&#39;, &#39;Actual CO Levels (μgm-3)&#39;, &#39;Predicted Levels&#39;])</pre><figure><img alt="Plot of predicted values monthly resampled data superimposed on actual values" src="https://cdn-images-1.medium.com/max/986/1*8682ER3RBl5kD8iT84X7tg.png" /></figure><p>It is easier to see how the model fits on this graph than the weekly data, although it is less accurate. The predicted values drop almost in tandem with the actual levels. Really neat!</p><h3>Closing Thoughts</h3><p>Many times, I considered performing basic univariate time series analysis, especially when I ran into errors (there were so many along the way), but I challenged myself to push my limits and see how far I could go. You can find my GitHub repo <a href="https://github.com/Gozie02/Multivariate-Time-Series-Analysis">here</a>.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cac06610d93f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>