<?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 Tia Plagata on Medium]]></title>
        <description><![CDATA[Stories by Tia Plagata on Medium]]></description>
        <link>https://medium.com/@tiaplagata?source=rss-28d188d40d54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*oe3TuAOq97MUVGB-.jpg</url>
            <title>Stories by Tia Plagata on Medium</title>
            <link>https://medium.com/@tiaplagata?source=rss-28d188d40d54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 21:10:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tiaplagata/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[How to Create Beautiful Word Clouds in Python]]></title>
            <link>https://medium.com/data-science/how-to-create-beautiful-word-clouds-in-python-cfcf85141214?source=rss-28d188d40d54------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfcf85141214</guid>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[natural-language-processi]]></category>
            <category><![CDATA[word-cloud]]></category>
            <dc:creator><![CDATA[Tia Plagata]]></dc:creator>
            <pubDate>Thu, 28 Jan 2021 15:08:44 GMT</pubDate>
            <atom:updated>2021-01-29T14:01:50.883Z</atom:updated>
            <content:encoded><![CDATA[<h4>A guide to creating stunning visualizations for your next NLP project</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MDtEd5lSA8xQX9ZKxv4YyQ.png" /><figcaption>Word Cloud by Author | <a href="https://unsplash.com/photos/VtRfEjFoybM">Trevi Fountain Image by James Lee via Unsplash</a></figcaption></figure><p>Natural Language Processing, or NLP, is a very popular subfield in Data Science at the moment because it allows computers to process and analyze human language. Siri and Alexa, spam filters, chatbots, auto-complete, and translate apps are all examples of everyday technology that use NLP.</p><p>As a Data Scientist, working with text data is a bit trickier than other types of data. Why? Because words are not numbers! This makes the Exploratory Data Analysis and the data cleaning and preprocessing steps a bit different in the Data Science workflow. Text data generally requires much more cleaning (removing stop words and punctuation, lowercasing, stemming or lemmatizing, etc). It also requires tokenizing or vectorizing the text (deriving meaningful numbers from words). As for exploring and analyzing the data, there are not as many ways to visualize text. However, text does open up one new kind of visualization technique that you have probably seen before — word clouds.</p><p>During my latest Data Science project, I got to delve into the world of NLP. Along the way, I learned all about creating word clouds in Python, and I wanted to write this piece to share my knowledge for anyone looking to create some beautiful visualizations for text data.</p><h3>Creating a Basic Word Cloud</h3><p>To create a basic word cloud (or any word cloud in Python), you will need the following libraries:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/796cf46f96816078e04a251eae7e9926/href">https://medium.com/media/796cf46f96816078e04a251eae7e9926/href</a></iframe><h4>Method 1: generate_from_text</h4><p>There are two main ways to build the word cloud. The first and simplest method is to create the word cloud from a corpus of text, such as an article, book, etc. This corpus should be in the form of a string.</p><p>In the example below, I have taken a list of attractions from <a href="https://www.tripadvisor.com/Attraction_Products-g187791-Rome_Lazio.html">TripAdvisor</a> in the city of Rome. I will group them all into one body of text (a corpus), and then create a basic word cloud.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*0pPetauRYeIzZagtxfRR3A.png" /><figcaption>Example attractions in Rome from TripAdvisor | Image by Author</figcaption></figure><p>Before I grouped the attraction texts, I did a little cleaning by lowercasing, removing basic stop words (i.e. “a”, “the”, “is”, etc), and lemmatizing. I did the same for a list of 12 cities (specifically, 12 of their <a href="https://www.tripadvisor.com/TravelersChoice-Destinations">Top World Destinations for 2021</a>). Rome’s grouped corpus is highlighted in the DataFrame below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*s3iTPuCNIRyiJW--B_xVJQ.png" /><figcaption>Grouped, cleaned and lemmatized attractions per city | Image by Author</figcaption></figure><p>As I mentioned before, we will create the word cloud from the text corpus for Rome. To do that, we will isolate that one corpus by taking a slice of this grouped DataFrame:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*njyOMIiQlObB9Kf_pbhAQA.png" /><figcaption>Rome text corpus | Image by Author</figcaption></figure><p>From here, we can create the basic word cloud. We will start by instantiating the WordCloud object from the wordcloud library, and use the generate_from_text method to feed it our text corpus. Finally, we will use plt.imshow() to display the WordCloud object. Be sure to use plt.axis(‘off’) to make sure it only displays the word cloud, not axes and their values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b83101209251a1183b5019106f5ab97/href">https://medium.com/media/8b83101209251a1183b5019106f5ab97/href</a></iframe><p>The resulting word cloud is below. We can see that by default, the word cloud uses bi-grams (pairs of words) instead of single words. If needed, we can turn this off when we instantiate the WordCloud object by changing the parameter ‘collocations=False’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*xvKq_o56aMZg8QwvQjnHOA.png" /><figcaption>Basic Rome Word Cloud (from text) | Image by Author</figcaption></figure><h4>Method 2: generate_from_frequencies</h4><p>The second method is to create a word cloud from a document term matrix. This is a commonly-used matrix for NLP, which has a separate column for each word in the corpus vocabulary, and the word frequency in each row. For example, below I have depicted the first columns of a document term matrix (dtm) for the 12 cities I showed before. Notice that the dimensions of the dtm 12 x 8676, indicating the 12 cities, and 8,676 words in the entire corpus vocabulary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9_kYScTr3KFE-fIiPFlJkg.png" /><figcaption>Document Term Matrix using Count Vectorization AKA dtm | Image by Author</figcaption></figure><p>If you have a document term matrix, we can easily feed this data into the word cloud object using the .generate_from_frequencies() method. First we will need to isolate the data we want to use for Rome. We will need to transpose this matrix for it to be in the correct format for the word cloud. Wea also want to get the most frequent words, so we will sort the values in descending order. You can see an example of the data we want to use in the photo below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q1gZf673KFaA7ws4KHPYkw.png" /><figcaption>Image by Author</figcaption></figure><p>To actually create the word cloud, we will use pretty much the same code as above, but use the generate_from_frequencies method instead.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1dfa8e5266c0ff383642ff735bbb73dc/href">https://medium.com/media/1dfa8e5266c0ff383642ff735bbb73dc/href</a></iframe><p>The resulting word cloud is below. Notice that because we isolated single words (not bi-grams) in the data, we did not need to tell the WordCloud object to turn off the ‘collocations’ parameter. It automatically had the vectorized words as single words instead of bi-grams.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*8J9syY3SLfE45OQvo1XyQw.png" /><figcaption>Basic Rome Word Cloud (from frequencies) | Image by Author</figcaption></figure><p>Finally, now that we understand how these word clouds are made, we can manipulate some of the parameters to create a nicer version of our basic word cloud. Let’s go back to our first example with the rome_corpus variable (generating a word cloud from text). Notice that words like ‘private tour’ and ‘skiptheline’ come up as some of the most frequent words. We can tell our word cloud a custom stop words list to get rid of these. I will also customize the dimensions of the word cloud, and make the entire figure bigger with a figsize parameter. We change the colormap, and add a title. See all these changes in the function below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e460d415a521a976f9ad720f9d30bf9/href">https://medium.com/media/3e460d415a521a976f9ad720f9d30bf9/href</a></iframe><p>Remember that you can always change the function above to use the generate_from_frequencies method instead! The resulting word cloud will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*mVqMihWpAd2pvEq8Uc_4rA.png" /><figcaption>Final Basic Word Cloud | Image by Author</figcaption></figure><p>Wow! Looks much better. It’s still very basic though. Let’s turn it up a notch by changing the word cloud shapes with masks.</p><h3>Change the Word Cloud Shape</h3><p>One way to make your word cloud visually stunning is to add a mask. A mask is an image you can use to change the shape of your word cloud. We can manipulate the mask very easily with the mask parameter when we instantiate the WordCloud object.</p><p>Once you choose the perfect image to use for your mask and save it (more on that in a minute), we can use the Image function from the PIL library and numpy to get it into the correct format for the WordCloud object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90812c9a571a4be81c616e79cf7da186/href">https://medium.com/media/90812c9a571a4be81c616e79cf7da186/href</a></iframe><p>Now that our mask is in the correct format, we can write a similar function to generate the nicer word cloud. The function below is very similar to our final basic word cloud function with a few exceptions:</p><ul><li>Using the mask parameter makes the width and height parameters void, because it will take on the size of the image itself</li><li>I am using a ‘scale’ of 3, which makes the computation faster for larger word cloud images</li><li>I changed the ‘colormap’ to ‘RdYlGn’ because it felt more Italian (think Italian flag, and homemade pasta!)</li><li>The ‘background_color’ is now ‘white’ for a nicer look against the colormap</li><li>The collocations (bi-grams) will show up this time since we are using the generate_from_text method, with the collocations parameter set to ‘True’</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16d13908071b389d37ad9d9ce3dc0576/href">https://medium.com/media/16d13908071b389d37ad9d9ce3dc0576/href</a></iframe><p>Our final word cloud is below… isn’t it <em>bellísima</em>?!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*E2_OLbcKhgKx96tKjZSh7g.png" /><figcaption>Final Rome Word Cloud | Image by Author</figcaption></figure><h4>Choosing the Perfect Mask</h4><p>The trickiest part of using a mask is that the image you choose must have the following requirements:</p><ul><li>a white background (it must be #ffffff, not off-white or transparent)</li><li>a definitive shape that is NOT white</li></ul><p>Here is an example of a mask that did not work so well. The issue here is that its shape is not definitive enough for you to tell what it is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cP6g30c6_GLFUKubvjLlWg.png" /><figcaption><a href="https://www.shutterstock.com/image-vector/colosseum-italy-icon-cartoon-style-isolated-557281153">Colosseum Image by Panda Vector via Shutterstock</a> | Word Cloud by Author</figcaption></figure><p>Therefore you want to choose something that has a very recognizable shape like the example below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4DUWqeqShaRma-tZHi8WgA.png" /><figcaption><a href="https://www.shutterstock.com/image-vector/simple-black-map-italy-isolated-on-519423460">Italy Map Image by David Petrik via Shutterstock</a> | Word Cloud by Author</figcaption></figure><p>There may even be times where you find the perfect photo for your mask, but it has a few marks in the background that are throwing off the word cloud shape. In order to fix this, you can always open the photo in Photoshop and edit the marks out. If you don’t have Photoshop, a method you can use to hack this (from my Marketing days) is <a href="https://www.canva.com/">Canva</a>. You can upload the mask image into Canva, place white squares on top of your marks, and then download the edited image. Voila! Your mask will be ready to go with that #ffffff background.</p><h3>Additional Finishing Touches</h3><p>The last step to making your word clouds beautiful is to use your own keen eye for those little aesthetic details. Changing the colors, using borders (AKA contour), and manipulating the font and plot size are just a few ways to customize your word clouds. Here are some ways to customize these parameters:</p><h4>Change the Colormap</h4><p>Changing the colors of the word is as easy as changing the ‘colormap’ parameter in the WordCloud object instantiation. See all the matplotlib colormap options <a href="https://matplotlib.org/tutorials/colors/colormaps.html">HERE</a>.</p><p>There is also a way to create a colormap of your own based on the colors in your mask image. To do this, use the ImageColorGenerator function in the wordcloud library, and use the colors generated in the color_func parameter when you instantiate the WordCloud object. The color_func parameter will overwrite the colormap parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a459cd0e23790e823d9f588ea92c991e/href">https://medium.com/media/a459cd0e23790e823d9f588ea92c991e/href</a></iframe><p>Here I used the colosseum again since the Italy map was all one color. Using this custom colormap is definitely a nice touch, even though we still can’t really tell its shape!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*o-S14IVZVMoT_DyjdzpWuQ.png" /><figcaption>Image by Author</figcaption></figure><h4>Use Contour</h4><p>Not like the Kardashians… but kinda. Contour gives your word cloud mask an outline. You can customize it when you instantiate the WordCloud object with the contour_color and contour_width parameters. Contour colors can be indicated with strings (use a simple color word, or a color code).</p><p>Below is an example of our same function to generate the word cloud, but with a thin black contour (width of 1).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7b2697956d0da591a7a81ca039ac85b/href">https://medium.com/media/c7b2697956d0da591a7a81ca039ac85b/href</a></iframe><p>As you can see, the resulting word cloud is a more distinguishable shape due to the contour.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*k-WkoGhaTdSqdyecQCSOog.png" /><figcaption>Image by Author</figcaption></figure><p>Looks pretty cool! The contour here is a bit squiggly because of the detailed shape of Italy. The contour line would be smoother with a more basic mask shape.</p><p>We can also try a colored contour to make this a bit nicer. Since there are dark red words in the word cloud, I found a nice wine red color code, #5d0f24, to replace the black contour, and increased the width to 3.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/858c0ab3b189730363ca652910e15398/href">https://medium.com/media/858c0ab3b189730363ca652910e15398/href</a></iframe><p>The resulting word cloud is below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*y43jGKO9VeAQ9MAVsJCwNw.png" /><figcaption>Image by Author</figcaption></figure><p>Even the colosseum mask from before looks better here with a little contour! Contour in this case helps us to see the actual shape of the mask.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*3BRTD6vse8L8kf4oOq1udA.png" /><figcaption>Image by Author</figcaption></figure><p>Whether or not to put contour, and deciding on the color and width of the contour is up to you based on the aesthetic detail you want to include in your word clouds.</p><h4>A Note on Word Cloud Size</h4><p>One thing you should always do is to change the size of the word cloud with plt.figure(figsize=(10,8)). You can change it to whatever size you need, but I have found that changing the size of the overall plot works better than trying to resize the WordCloud object itself. I have included this in all the word cloud functions, and you can notice the difference in the word cloud sizes when we jumped from basic word clouds to the word clouds created with functions.</p><h4>More Fun Customizations!</h4><p>The possibilities are endless when it comes to customizing your word clouds. Here are some examples:</p><ul><li>Manipulate the font of your word cloud text with the font_path parameter</li><li>Change the min and max font sizes (min_font_size, max_font_size parameters)</li><li>Change the background_color of the word cloud</li><li>Reduce the max_words parameter to show only the top 20 or 50 words, which sometimes makes the word cloud easier to read</li></ul><p>All of these small details can impact your word cloud visualizations. Since each mask and each text corpus will look different, it’s ultimately up to you to play around with them to truly make your word clouds look beautiful!</p><h3>Conclusion</h3><p>Thanks for following along with me on this little journey to Roma! I hope this story taught you a thing or two about word clouds and creating beautiful NLP visualizations. If you have any more tips, I’d love to hear about them in the comments below!</p><p><em>Note: This was only a small piece of my Data Science capstone project, where I used NLP to classify text for different cities. You can see the full project repo on my </em><a href="https://github.com/tiaplagata"><em>Github</em></a><em> account.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfcf85141214" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-create-beautiful-word-clouds-in-python-cfcf85141214">How to Create Beautiful Word Clouds in Python</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting Up Google Colab for CNN Modeling]]></title>
            <link>https://medium.com/swlh/setting-up-google-colab-for-cnn-modeling-55b5208599c4?source=rss-28d188d40d54------2</link>
            <guid isPermaLink="false">https://medium.com/p/55b5208599c4</guid>
            <category><![CDATA[colab]]></category>
            <category><![CDATA[google-colab]]></category>
            <category><![CDATA[cnn]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Tia Plagata]]></dc:creator>
            <pubDate>Tue, 15 Dec 2020 14:24:42 GMT</pubDate>
            <atom:updated>2020-12-17T20:35:48.898Z</atom:updated>
            <content:encoded><![CDATA[<p>During my journey as a Data Science student, I created a project using Convolutional Neural Networks (CNNs). CNNs are deep learning models that are becoming increasingly popular in the world of Data Science for image classification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*tAp7sVaLMb46X7ur2qv-_A.png" /><figcaption>Source: <a href="https://github.com/tiaplagata">https://github.com/tiaplagata</a></figcaption></figure><p>There is just one major issue when you’re building them at home — the training time for these models is very long!</p><p><em>Enter</em> <em>Google Colab.</em></p><p>Colab is great because it allows you to run your notebook on a hosted computer that is most-likely better/faster/stronger than your local machine. That means faster training for your CNN model.</p><h3>Why use Google Colab?</h3><p><strong>In Colab, you can make use of GPUS! </strong>Normally your computer is running on a CPU (Central Processing Unit). The CPU is the brain of your computer, which will run your programs and, in this case, train your model. Even though you can split the work between multiple cores of your CPU, it won’t be as fast as a GPU for machine learning. A GPU (Graphics Processing Unit) is a processor made up of many smaller, specialized cores, which means that it can split the work between even more cores and therefore get your CNN training done faster. The GPU is <em>designed</em> for parallel processing and it will be much faster than a CPU in this use-case.</p><p><strong>Make use of GPUs with High-Speed RAM with Colab Pro.</strong> For only $10 per month, Colab has an upgraded option that gives you access to even faster GPUs and longer runtimes (AKA your notebook won’t time-out/disconnect as often). It also gives you access to more RAM, thus you will have more memory for your data. I actually went with this option because I believe $10 is super cheap when you compare it to the time-savings you will experience (but I also know people who have had lots of success and speed just using normal Colab).</p><p><strong>Therefore, use Colab to save time! </strong>When I compared my model’s training time using Colab to another students’ training time on their local machine, Colab could train a model in 3–4 minutes (approx. 7 seconds per epoch) vs 6 hours or so on the local machine (approx. 5 minutes per epoch). Now I know that everyone’s computer will be different, but that is a HUGE time-savings even with slight variations in your local machine’s speed.</p><h3>How to Get Set Up on Colab</h3><p><strong>Prerequisites</strong>: A Google account</p><p><strong>First things first,</strong> you want to go to <a href="https://colab.research.google.com/">https://colab.research.google.com/</a>, sign in to your Google account, and open a new notebook. Alternatively, you can upload an existing jupyter notebook.</p><p><strong>Very important:</strong> You should set your runtime before you do anything! If you forget to do this before you run anything, you will need to restart your kernel when you finally set it to the high-speed option. To do this, go to the top toolbar and go to Runtime &gt; Change runtime type. You can then change your hardware accelerator to a GPU, and if you opted for the Colab Pro option, you can also set the runtime to High RAM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vOkqNhJNl1204kOhqq59zA.png" /></figure><p><strong>Next, we will set up a way to get ahold of your data.</strong> Here’s the thing about Colab… unlike your local machine, you need to add your data to the machine you’re working on. Anything you upload to Colab will go to the <em>content</em> folder. However, you can also mount your Google Drive and use that to pull data and files into your notebook. There is a toolbar on the side that you can use to see what’s happening and where things are going.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*PqwKmWOKLZsUpnSkiyVDcQ.png" /></figure><p>The toolbar will default to show you the <em>content</em> folder. To navigate up a level, click the folder that has the arrow. You will then see a bunch of folders that are available on the local machine that Google is hooking you up with. These folders allow you to have access to all the Python packages you need and pip-install more if you want. For this specific project, I was using CNNs to predict the presence of pneumonia in chest x-rays. I was using the <a href="https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia">Mooney Pneumonia</a> dataset available on Kaggle. Luckily this dataset is zipped and ready to go with all the train/test/validation data split into their own folders. I uploaded the zipped data to my Google Drive, but it can also be accessed with the Kaggle API.</p><p><strong>After this step, mount your Google Drive to this local machine.</strong> When you do, you’ll be able to pull those files out and/or save your models back into your Drive for later use. When you run this cell, it will ask you to go to a link, authorize Colab’s use of your files, and copy and paste that authorization link back in the notebook.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*LW2diU0Q9vuwFM4SiVMspQ.png" /></figure><p>To make sure this worked, check that folder toolbar again and look for a folder called <em>gdrive</em> on the same level as all the other folders. If you look in this folder, you should see everything that’s in your Google Drive. (the code for this is below).</p><p><strong>Now it’s time to get your data!</strong> Either upload a zipped file to Google Drive in a new tab or pull the data from an API in the notebook you just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*9y14OIsRfZHVZ8J-ixTP3w.png" /></figure><p>Find this zip file using the toolbar (you might need to press the little folder with the refresh button right under files if you can’t see it right away). Press the 3 dots to the right of the file, and copy the file path. This will go in the <em>drive_path</em> variable in the code snippet below (the <em>local_path</em> variable will stay the same). If you used the API, the file is probably under content &gt; drive.</p><p><strong>Finally, you’re going to unzip the file</strong>, but the trick to this is to copy it to the Google Colab machine before you unzip. The reason for this step is to avoid having to reach all the way into Google Drive every time you access this data because it slows down the process. In this way, everything will be unzipped and ready to go in the local machine that is using the GPU, high RAM, etc so you have the fastest setup available.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0f03a922ad0e83fbae4732d1a551d69/href">https://medium.com/media/c0f03a922ad0e83fbae4732d1a551d69/href</a></iframe><p><strong>And voila!</strong> Your file should now be unzipped and in the content folder. You can double-check to make sure it’s there by navigating the side toolbar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*A1jEPjVxAqMGScylP4DGaw.png" /></figure><p>If you’re lucky, your data will already be split into these nice folders of images for your train/test/validation split, but otherwise, you can create those folders manually, or get your data into the notebook first and then do a train/test/val split using sklearn’s <em>TrainTestSplit</em> method.</p><p>Now we can move on to preprocessing the images and loading them into the notebook from Colab’s local machine!</p><h3>Image Preprocessing in Colab</h3><p>With our data in these folders on the local machine, we can use the <em>ImageDataGenerator </em>from<em> </em>Tensorflow’s Keras package. The beauty of going through the above process and getting our data into the Colab machine’s folders is that we can also use the <em>flow_from_directory</em> method to grab these images right out of the folders.</p><p>In the code below, I preprocessed the images with the <em>ImageDataGenerator</em> and then iterated through the generators with the next function to get my data into the X, y formats needed for modeling. The process and code are below:</p><ul><li><strong>Scaling the data</strong>: <em>ImageDataGenerator</em> gives you the option to rescale the images with their <em>rescale</em> argument. Using 1./255 rescales the image pixels to a scale of 0–1 because the RGB pixel scale for images is from 1–255.</li><li><strong><em>flow_from_directory</em></strong>: the following arguments are used in the code below, but you can find more by referencing the <a href="https://keras.io/api/preprocessing/image/">full documentation</a></li></ul><ol><li>file_path — if you have train/test/val folders, you want to direct the file path there (use the 3 dots next to the folder in the side toolbar to copy the path if you’re not sure what it is)</li><li>target_size — how big you want the images to be (larger=longer train times, but that shouldn’t be a problem if you’re using Colab)</li><li>batch_size — the total number of images in the folder you’re directing to. Check the number of images with <em>len(os.listdir(file_path)</em></li><li>class_mode — defaults to “<em>categorical</em>” for multi-class, but you can set it to <em>“binary”</em> if you’re only predicting 2 classes. There are other options here so check out the <a href="https://keras.io/api/preprocessing/image/">documentation</a> if needed</li><li>seed — it is very important to note that when you’re using Colab it is very hard to snuff out randomization, and you will need to rerun the entire notebook every time you restart your runtime. Therefore, it is imperative to set random seeds whenever you can to best reproduce your results</li></ol><ul><li><strong>Iterate through to create X and y</strong>: Use the next function to iterate through the generators and put your data into the correct shape for modeling</li><li><strong>Check class indices</strong>: To ensure you’re referencing the correct classes, it is always a good idea to check the class indices to make sure the data is as you think. For example, make sure 0 refers to no pneumonia and 1 refers to pneumonia.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6319c74e294932527a2c4c6e0858813/href">https://medium.com/media/b6319c74e294932527a2c4c6e0858813/href</a></iframe><p>From here you can move on to building, training, and evaluating your Convolutional Neural Network! The speed from using the GPUs will allow you to experiment with building and tuning multiple models without having to wait for hours in between each one’s training.</p><h3>Pros and Cons of Using Colab</h3><p>Of course, Colab has both pros and cons, so you should consider both before getting set up.</p><h4>Pros:</h4><ul><li><strong>Faster runtimes:</strong> the GPUs and High RAM will train models in a fraction of the time, so you can continue to tune and iterate without having to wait for hours and hours while each model trains</li><li><strong>Accessible from any computer:</strong> that’s the beauty of the G-Suite… you can access everything through your Google Drive!</li><li><strong>Connect directly to Github:</strong> another cool feature is the ability to push directly to Github from your notebook, next to where you would normally save it. It will prompt you to sign into your Github account, then ask which repo to add it to. <em>Just a quick sidebar on this: you should probably push all your local changes </em><strong><em>before</em></strong><em> doing this, then pull down the change where you added the Colab notebook so you don’t run into any merge conflicts</em></li></ul><h4>Cons:</h4><ul><li><strong>Rerunning the notebook:</strong> one major annoyance with Google Colab is that you have to rerun the entire notebook every time you restart your runtime… that means doing the whole unzip, use data generators, preprocess, etc. However, in my opinion, this is a fair price to pay compared to waiting 6 hours for a model to train. <em>You may notice that sometimes the runtime times out and then re-initializes, but you shouldn’t have to rerun stuff unless you completely close out of the notebook and restart the runtime the next time you open it.</em></li><li><strong>More randomness:</strong> Because you have to constantly rerun things, the opportunity for randomness is more prevalent. Make sure you set random seeds in every method that takes a random seed argument (especially if you use validation splits in the model)! I found it very difficult to get the <em>exact</em> same model results each time I reran my notebook because, at the time, I missed a couple of opportunities to set those random seeds to get rid of all the randomness.</li><li><strong>Colab Pro availability:</strong> Colab Pro is only currently available in the US and Canada</li></ul><p><strong>In conclusion, I would use Colab and Colab Pro for any major machine learning project to save time! I think it’s a great tool for Data Scientists, despite a few minor annoyances. These are just some of the things I learned about Colab during my latest project, so if you have any more tips, pros, cons, personal experiences, etc, please leave a comment below!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55b5208599c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/setting-up-google-colab-for-cnn-modeling-55b5208599c4">Setting Up Google Colab for CNN Modeling</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Scikit-learn Pipelines Make Your Life So Much Easier]]></title>
            <link>https://tiaplagata.medium.com/how-scikit-learn-pipelines-make-your-life-so-much-easier-3cfbfa1d9da6?source=rss-28d188d40d54------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cfbfa1d9da6</guid>
            <category><![CDATA[imblearn]]></category>
            <category><![CDATA[data-preprocessing]]></category>
            <category><![CDATA[machine-learning-python]]></category>
            <category><![CDATA[sklearn-pipeline]]></category>
            <category><![CDATA[pipeline-oop]]></category>
            <dc:creator><![CDATA[Tia Plagata]]></dc:creator>
            <pubDate>Tue, 17 Nov 2020 15:37:27 GMT</pubDate>
            <atom:updated>2020-11-18T03:05:34.360Z</atom:updated>
            <content:encoded><![CDATA[<p>During the course of my latest machine learning project, I took some time to explore Scikit-learn pipelines. A pipeline is an object that allows you to preprocess/transform data, train a model, and use a model all in one easy tool. Below I will talk about some of the cool things you can do with pipelines, and how they can be a HUGE time-saver when building and validating models.</p><h4>Building a Basic Pipeline | Pipeline Basic Abilities</h4><p>Building a pipeline is simple. Let’s say you need to do the following steps in building your model:</p><ul><li>Scale data to a standardized scale</li><li>Fit a Random Forest Classifier</li></ul><p>We can take these same steps and throw them in a pipeline. Essentially, the initial steps in the pipeline should be preprocessing steps, thus having a fit/transform method. The final step should be an estimator with a scoring method. In this case, our scaler is our preprocessing step, and our Random Forest is the estimator. You can add any number of preprocessing steps into your pipeline before the estimator, but for this example, I will keep it simple. Check out the code below to define the pipeline:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3399311b0c0926e4ef16bcd45f7da05f/href">https://medium.com/media/3399311b0c0926e4ef16bcd45f7da05f/href</a></iframe><p>Notice that the steps in your pipeline should be defined as a list of tuples, with the name of the step as a string first, and the method second.</p><p>Now you can use this pipeline just like you would use an estimator object:</p><ul><li>First, you need to fit it to the training data</li><li>Call the .score( ) method, .predict( ), etc.</li><li>Access attributes and methods of the steps within your pipeline by indexing the step (this was something that I learned halfway through my project after lots of Googling… remember to go back to basics with indexing!)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4ae0d47223bb60da1e44598a77a1de3/href">https://medium.com/media/c4ae0d47223bb60da1e44598a77a1de3/href</a></iframe><h4>Using Pipeline and GridSearchCV</h4><p>Another amazing thing you can do with pipelines is to integrate a grid search! GridSearchCV is an exhaustive search of all combinations of parameters within a specific parameter grid for your model. It allows you to find the perfect tuning parameters in just a few lines of code. You can create pipelines that perform grid searches and even cross-validation as well.</p><p>In order to do this, you simply define the pipeline the same way as above, then define your parameter grid. The parameter grid is a dictionary with parameters as keys and lists of parameters to test as values. The names of the keys must have the following string format “PipelineStepName__ParameterName”. Finally, you run GridSearchCV with your pipeline as the estimator, and parameter grid as your parameter grid.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b101a3cbd3cd739626c5ecc867103469/href">https://medium.com/media/b101a3cbd3cd739626c5ecc867103469/href</a></iframe><p>In the example above, you can see how you can see the best parameters and best score for the best performing model. You can even store that best estimator in another variable to reference later on. In this way, you can add another step to your pipeline streamline — model tuning. It is important to remember that GridSearchCV is an *exhaustive* search, meaning that it will try every single combination of parameters. This can get cumbersome very quickly as you add more parameters, and/or if you add cross-validation as well. The example above would fit 90 total models (3*2*3*5), each containing 100–200 decision trees, so be very careful about which parameters to try and their effect on Big O Notation.</p><h4>Dealing with Class Imbalance in Your Pipeline</h4><p>If you’ve worked with pipelines before, you may have run into issues when trying to deal with class imbalance. Class imbalance is when the target you are predicting is not seen as frequently as other classes (i.e. a rare disease). To deal with this imbalance, you can use class weights, oversample the minority class, or undersample the majority class. One popular oversampling technique is SMOTE (Synthetic Minority Oversampling Technique) from Python’s imblearn library.</p><p>When trying to add SMOTE to my pipeline in my project, I hit an error. The issue is that sklearn’s pipeline will try to oversample the training and validation sets, which is not what you want to do with SMOTE. To fix this, imblearn has a pipeline that is built on top of sklearn’s pipeline, meaning it functions almost exactly the same way. However, when you call the predict( ) method, the imblearn pipeline will skip the sampling step, solving this issue.</p><p>In this way, dealing with class imbalance can be a breeze, because you can just add that step to your pipeline, like the example below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b49fc918ef73f5808b78ce498a183cdb/href">https://medium.com/media/b49fc918ef73f5808b78ce498a183cdb/href</a></iframe><h4>Customizing Classes to Add to Your Pipeline</h4><p>One of the things I explored in my most recent machine-learning project is how to build a custom class for my pipeline. Why would you want to do this? Sometimes OneHotEncoder, or StandardScaler is not enough preprocessing to transform your raw data to be ready to feed into your estimator. Essentially to make pipelines work most efficiently, you want them to include <strong>all</strong> of the data preprocessing so all you need to do to make predictions on new data is to feed it right into your pipeline. For example, what if you want to transform text data in a specific way, or engineer new features that are more complicated? There are 2 ways to do customize these classes:</p><p>The first way is to use Object-Oriented Programming (OOP). Sklearn has base estimators for all of their models that you can use to inherit qualities. Remember that in order to be used in the pipeline, the class must have both .fit( ) and .transform( ) methods, and the final class must have a .score( ) method. Therefore, we can build custom classes that have these methods and our pipeline will recognize them as valid. Check out the example below to see a very simple example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d443db8bd5d8bf163e816fe159ccbb7b/href">https://medium.com/media/d443db8bd5d8bf163e816fe159ccbb7b/href</a></iframe><p>The first class will allow me to choose which columns I want to use. In this way, I can try a variety of features in my grid search (see below). The second class will transform two of the features in my dataset from ‘yes’ and ‘no’ to 1 and 0, respectively (this could be done with OneHotEncoder too, but I just wanted to show an easy example). Now I can go through a similar grid search and scoring as the previous examples, while completely preprocessing my data all in my pipeline.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b8cf10a46c3f472cdf80ca0db84eb9f/href">https://medium.com/media/2b8cf10a46c3f472cdf80ca0db84eb9f/href</a></iframe><p>The second way to do this is arguably easier, but less customizable. We can use sklearn’s FunctionTransformer. This amazing tool will transform any function (even a lambda function) into a class that the pipeline can understand and use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5be70f7aebe64f048b2d29666afc72d5/href">https://medium.com/media/5be70f7aebe64f048b2d29666afc72d5/href</a></iframe><h4>In conclusion…</h4><p>Pipelines are your best friend when you’re building machine learning models! They make it super easy for you to put all your preprocessing steps, your model, and even model tuning into an organized and streamlined workflow. <strong>If your goal is to work smarter, not harder, then pipelines can help you achieve that goal.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cfbfa1d9da6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interpreting the Root Mean Squared Error of a Linear Regression Model]]></title>
            <link>https://tiaplagata.medium.com/interpreting-the-root-mean-squared-error-of-a-linear-regression-model-5166e6b10db8?source=rss-28d188d40d54------2</link>
            <guid isPermaLink="false">https://medium.com/p/5166e6b10db8</guid>
            <category><![CDATA[root-mean-squared-error]]></category>
            <category><![CDATA[model-interpretation]]></category>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[mean-squared-error]]></category>
            <category><![CDATA[linear-regression-python]]></category>
            <dc:creator><![CDATA[Tia Plagata]]></dc:creator>
            <pubDate>Fri, 06 Nov 2020 18:59:36 GMT</pubDate>
            <atom:updated>2020-11-06T19:10:57.959Z</atom:updated>
            <content:encoded><![CDATA[<p>The first time I ever built a Linear Regression model, I thought two things:</p><ul><li><em>Wow! I built something that can actually predict housing prices!</em></li><li><em>Ok, but how good are these predictions?</em></li></ul><p>I had learned to check all of the assumptions of a Linear Regression model (residuals should have a normal distribution, features are linearly correlated with the target, there’s no multi-collinearity, etc.). I learned to scale and sometimes even log-scale my features and target. I even learned about mean squared error (MSE) and root mean squared error (RMSE) to interpret the residuals of the model. The problem was, once I scaled my features and calculated the MSE, that number was scaled too! I wasn’t sure how to read it accurately.</p><p><em>As a refresher, Mean Squared Error is the average of the squared difference between each predicted point and the actual point. It is generally a very big number because we square each difference to get rid of negative numbers before taking the average. Therefore, we sometimes use Root Mean Squared Error to bring that number back down to scale (RMSE is the square root of MSE).</em></p><p>For example, if the target variable is in USD (i.e. Sale Price of a home), what does a root mean squared error of 0.3 really tell you? Is that big or small? Maybe if you scaled everything from 0 to 1 you could argue that it’s small, but what if you used a different scaling technique? Moreover, one of the most important skills as a Data Scientist is being able to connect these metrics back to real-world applications and interpret them through that lens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TlBV9UnaypKQRRnd8ODniw.jpeg" /><figcaption>Photo by Evelyn Paris via Unsplash</figcaption></figure><p>Let’s say I created an app that could predict your home price. As a homeowner, you might see this predicted home price and feel inclined to sell. Now, what if I predicted that your home was going to sell for $350,000. Maybe you get excited and decide to sell your home. When you sell your home, you then realized that it would actually only sell for $250,000, which is less than you paid for the home. My model’s prediction was $100,000 off. For home prices, that is <strong>a lot </strong>but how would you know that if the RMSE was 0.3?</p><p>This is why it is so important to interpret your Mean Squared Error and Root Mean Squared Error correctly. Therefore, in my first Linear Regression project, I reversed my log and normalization scaling so that my MSE and RMSE were back in USD. In this way, I was able to correctly interpret the RMSE. Below I will show you how I did this in Python.</p><p>Here are the libraries you will need (this does not include the libraries needed to actually build a linear regression model).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55d06f1e31b2b569934a5212e7d5ebbc/href">https://medium.com/media/55d06f1e31b2b569934a5212e7d5ebbc/href</a></iframe><h3>Log Transformation &amp; Normalization</h3><p>In this example, I am building a Linear Regression model to predict housing prices. The target feature here is housing prices, which are typically in USD (or whatever currency you’re working with). In the process of building this model, I decided to log transform price (the target).</p><p><strong>Why?</strong> In order to make the distribution of this feature more normal, and thus easier to work with. In this example, I will take the log base 10 of all of the continuous variables.</p><p><strong>How?</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ed33530c53a40585d1b4c23d36cc25a/href">https://medium.com/media/3ed33530c53a40585d1b4c23d36cc25a/href</a></iframe><p>Next, I will scale the target and the other continuous variables to a standard normal scale.</p><p><strong>Why?</strong> If we do this to the target and our other continuous features, it will allow the beta coefficients of our model to be weighted in a similar way so we can interpret which features are having a greater impact on our target. I will create a function to standard normalize so that we can apply it to our entire log data frame.</p><p><strong>How?</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99b9c9924055e146d8a411ec03d748b8/href">https://medium.com/media/99b9c9924055e146d8a411ec03d748b8/href</a></iframe><p>Awesome! Now we have a data frame of log-transformed, standard normalized continuous data. I can concatenate this with our categorical variables and run the model based on this. Next, I will fast-forward to the interpretation stage of the model to see how to change the target feature back to USD.</p><h3>Transforming Mean Squared Error Back to USD</h3><p>After building the model, I can validate it with a train-test split or k-fold cross-validation. Model validation is important to see if the model can predict a target using new data, instead of the just data it was trained on. We can analyze whether the model is overfitting (i.e. it predicts training data super well, but cannot generalize to new data) or underfitting (i.e. it is too generalized and thus produces predictions that are too far off). Many times during model validation, we analyze Mean Squared Error (MSE) or Root Mean Squared Error (RMSE) — AKA the average distance (squared to get rid of negative numbers) between the model’s predicted target value and the actual target value. In this example, I can use RMSE to see how far off the model’s predicted price generally is from the actual home price.</p><p>Remember that the RMSE will still reflect a price that has been log-transformed and standard-normal scaled. Here’s how I changed it back to USD.</p><p>Step 1: Build a function to undo the scale.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bb80ca48e5e67548f6afbcb1f516e63/href">https://medium.com/media/0bb80ca48e5e67548f6afbcb1f516e63/href</a></iframe><p>Step 2: Inverse the scale using this function.</p><p>Step 3: Inverse the log.</p><p>Step 4: Find the mean squared error and root mean squared error.</p><p>(I did steps 2–4 all in a few lines of code, but to break it down, you would inverse the normalization scale first, then to inverse the log, you would do 10 to the power of that inversed array, then find the mse)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd5fdf1d8a5e82b475204e6b2a737241/href">https://medium.com/media/fd5fdf1d8a5e82b475204e6b2a737241/href</a></iframe><p>Step 5: Interpret.</p><p>As you can see, the final RMSE for the training set was $130,614. For the testing set, the RMSE was $131, 683. This means that in general, the model’s predicted sale price is generally about $130,000 off. If I hadn’t changed that RMSE back to USD, it would have been 0.38 and 0.39 for the training and test RMSEs respectively, which is much harder to interpret. One thing to note is that since the training and test RMSE are very close, we did not overfit this model.</p><p>Now, is this a good model? Probably not… If I told you your house was going to sell for $330,000 and then it actually only sold for $200,000, you might be pretty mad. $130,000 is a big difference when you’re buying/selling a home. But what if I told you that I could predict GDP (Gross Domestic Product) of the US with only an average error of $130,000? Well, since the GDP is over $20 trillion, that difference wouldn’t be so big.</p><h3>In conclusion…</h3><p>If I could give you one takeaway from this article, it’s this:</p><p><strong>Understanding the context of the real-world application of the data you’re working with is essential in order to properly interpret the model’s metrics (like MSE and RMSE).</strong></p><p>Without knowing about housing prices, how would you know if your error is big or small? It is so important to think about how your model will be used in practice. Once you understand that, you can transform the target back to its original unit, and truly understand your model’s errors/residuals.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5166e6b10db8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>