{"id":9290,"date":"2021-05-11T18:47:56","date_gmt":"2021-05-11T18:47:56","guid":{"rendered":"http:\/\/algotrading101.com\/learn\/?p=9290"},"modified":"2023-04-03T21:08:36","modified_gmt":"2023-04-03T21:08:36","slug":"google-colab-guide","status":"publish","type":"post","link":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/","title":{"rendered":"Google Colab &#8211; A Step-by-step Guide"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_9290\" class=\"pvc_stats total_only  \" data-element-id=\"9290\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>\n<figure class=\"wp-block-image size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/05\/Google-Colab-Guide-1024x683.jpg\" alt=\"Google Colab Logo\" class=\"wp-image-9296\" width=\"512\" height=\"342\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Table of contents:<\/strong><\/h3>\n\n\n\n<ol id=\"block-44b84697-5880-4a95-b333-5a34a05db56d\"><li><a href=\"#what-is-google-colab\">What is Google Colab?<\/a><\/li><li><a href=\"#free\">Is Google Colab free?<\/a><\/li><li><a href=\"#pros\">Why should I use Google Colab?<\/a><\/li><li><a href=\"#cons\">Why shouldn&#8217;t I use Google Colab?<\/a><\/li><li><a href=\"#alternatives\">What are the alternatives to Google Colab?<\/a><\/li><li><a href=\"#python\">Does Google Colab support Python?<\/a><\/li><li><a href=\"#started\">How do I get started with Google Colab?<\/a><\/li><li><a href=\"#libraries\">How do I import libraries\/install dependencies in Google Colab?<\/a><\/li><li><a href=\"#gpu\">How do I enable GPU\/TPU usage in Google Colab?<\/a><\/li><li><a href=\"#import\">How do I import data in Google Colab?<\/a><\/li><li><a href=\"#machine-learning\">Using Machine Learning in Google Colab to predict house prices<\/a><\/li><li><a href=\"#load-kaggle\">How can I load Kaggle datasets directly into Google Colab?<\/a><\/li><li><a href=\"#eda\">How can I visualize data\/produce charts using Google Colab?<\/a><\/li><li><a href=\"#deployment\">How can I deploy ML algorithms in Google Colab?<\/a><ol><li><a href=\"#prepare\">Prepare the Data<\/a><\/li><li><a href=\"#algorithm\">Pick an Algorithm and evaluate it<\/a><\/li><li><a href=\"#optimize\">Optimize the Algorithm<\/a><\/li><\/ol><\/li><li><a href=\"#save\">How can I save my Google Colab notebook directly to GitHub?<\/a><\/li><li><a href=\"#mount\">How can I mount external Python files in Google Colab?<\/a><\/li><li><a href=\"#magic\">What are Google Colab Magics?<\/a><\/li><li><a href=\"#features\">What are some other interesting Google Colab features?<\/a><\/li><li><a href=\"#common-machine-learning-testing-mistakes\">What are the 3 Common Machine Learning Analysis\/Testing Mistakes?<\/a><\/li><li><a href=\"#full-code\">Full code<\/a><\/li><\/ol>\n\n\n\n<a name=\"what-is-google-colab\">\n\n\n\n<h2 class=\"wp-block-heading\">What is Google Colab?<\/h2>\n\n\n\n<p>Google Colab is an online notebook-like coding environment that is well-suited for machine learning and data analysis.<\/p>\n\n\n\n<p>It comes equipped with many Machine Learning libraries and offers GPU usage. It is mainly used by data scientists and ML engineers.<\/p>\n\n\n\n<p>Link: <a href=\"https:\/\/colab.research.google.com\">https:\/\/colab.research.google.com<\/a><\/p>\n\n\n\n<a name=\"free\">\n\n\n\n<h2 class=\"wp-block-heading\">Is Google Colab free?<\/h2>\n\n\n\n<p>Yes, Google Colab is free to use and you can access all of its features to a certain degree. There is a subscription plan called <a href=\"https:\/\/colab.research.google.com\/signup\">Google Colab Pro<\/a> that offers access to upgraded features.<\/p>\n\n\n\n<p>These upgraded features allow the use of more processing power, RAM, and memory. You can access this plan for $9.99 per month if you come from one of the following countries:<\/p>\n\n\n\n<ul><li>US<\/li><li>Canada<\/li><li>UK<\/li><li>Germany<\/li><li>France<\/li><li>India<\/li><li>Japan<\/li><li>Thailand<\/li><li>Brazil<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"757\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/2-11-1024x757.jpg\" alt=\"\" class=\"wp-image-16060\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/2-11-1024x757.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/2-11-300x222.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/2-11-768x568.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/2-11.jpg 1098w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<a name=\"pros\">\n\n\n\n<h2 class=\"wp-block-heading\">Why should I use Google Colab?<\/h2>\n\n\n\n<ul id=\"block-d85790f0-ca53-4f0e-983a-b95d2ba0408b\"><li>Google Colab is Free <\/li><li>Easy to get started<\/li><li>Allows access to GPUs\/TPUs<\/li><li>Easy to share code with others<\/li><li>Easy graphical visualizations in Notebooks<\/li><\/ul>\n\n\n\n<p>Let&#8217;s go over each of the pros of Google Colab.<\/p>\n\n\n\n<p>Firstly, it is <strong>free to use<\/strong> and everyone can access it. There are also some premium features if you want to utilize GPUs\/TPUs with more power and fewer limitations.<\/p>\n\n\n\n<p><strong>Getting started<\/strong> with Google Colab is easy. You don&#8217;t need to install any prerequisites or have a decent PC or laptop. All you need is a browser where you&#8217;ll get a Jupyter Notebook-like environment.<\/p>\n\n\n\n<p>Google Colab comes ready with <strong>GPUs <\/strong>and <strong>TPUs <\/strong>which can be utilized with a click of a single button. This makes Google Colab a great coding environment for machine learning practitioners.<\/p>\n\n\n\n<p><strong>Sharing code<\/strong> with Google Colab can be done through Google Drive or directly to GitHub with an in-built feature.<\/p>\n\n\n\n<p>Being like a Jupyter Notebook, a Google Colab document allows you to run code in blocks, and intersperse these blocks with Markdown cells. It can also easily display multiple graphical outputs.<\/p>\n\n\n\n<p>All of these features make Google Colab a phenomenal asset when it comes to collaborative, data science, machine learning, and data analysis projects.<\/p>\n\n\n\n<a name=\"cons\">\n\n\n\n<h2 class=\"wp-block-heading\">Why shouldn&#8217;t I use Google Colab?<\/h2>\n\n\n\n<ul id=\"block-5a4c6fa6-222a-4dda-af90-7fdbbe677a01\"><li>GPU\/TPU usage is limited<\/li><li>Not the most powerful GPU\/TPU setups available<\/li><li>Not the best de-bugging environment<\/li><li>It is hard to work with big data<\/li><li>Have to re-install extra dependencies every new runtime<\/li><\/ul>\n\n\n\n<p>GPU\/TPU usage is not endless with Google Colab as resources aren&#8217;t infinite. The <strong>free version<\/strong> lasts for <strong>12 hours of continuous usage<\/strong> and is not very tolerant with inactivity, whilst the <strong>pro version allows 24 hours<\/strong> of continuous usage with greater tolerance.<\/p>\n\n\n\n<p>The free version of Google Colab allows the usage of a <strong>K80 GPU<\/strong> while the Pro version allows a <strong>T4<\/strong> or <strong>P100 GPU<\/strong>. For most of you, these GPUs are way more powerful than the ones you have, but for more money we can get even better ones (e.g. AWS).<\/p>\n\n\n\n<p>Being a Notebook environment, it will be harder to catch <strong>bugs<\/strong> in your code before running it.<\/p>\n\n\n\n<p>As big datasets<strong> need to fit in a Google drive<\/strong>, it can be difficult to deal with them because you are limited to 15 GB of free space with a Gmail id.<\/p>\n\n\n\n<p>Lastly, you&#8217;ll have to <strong>(re)install any additional libraries<\/strong> you want to use <strong>every time you (re)connect to a Google Colab notebook<\/strong>. A good thing is that it comes equipped with pre-installed libraries that are often used.<\/p>\n\n\n\n<a name=\"alternatives\">\n\n\n\n<h2 class=\"wp-block-heading\">What are the alternatives to Google Colab?<\/h2>\n\n\n\n<p>Google Colab can be replaced with other platforms that can be more suitable for your needs. Here are some of them:<\/p>\n\n\n\n<ul><li>Jupyter Notebook<\/li><li>Kaggle<\/li><li>Azure Notebooks<\/li><li>Amazon SageMaker<\/li><li>Paperspace Gradient<\/li><li>FloydHub<\/li><\/ul>\n\n\n\n<a name=\"python\">\n\n\n\n<h2 class=\"wp-block-heading\">Does Google Colab support Python?<\/h2>\n\n\n\n<p>Yes, Google Colab supports Python (and as of October 2019 only allows the creation of Python 3 notebooks), though in some cases with further tinkering it might be possible to get R, Swift, or Julia to work.<\/p>\n\n\n\n<p>Have in mind that since the 1st January 2020, Python 2 is no longer supported.<\/p>\n\n\n\n<a name=\"started\">\n\n\n\n<h2 class=\"wp-block-heading\">How do I get started with Google Colab?<\/h2>\n\n\n\n<p>There are several ways to get started with Google Colab and we will go over each of them. All approaches are quite easy and it depends on what you want to start working on (i.e. fresh notebook or GitHub repository).<\/p>\n\n\n\n<p>The first way is to go over to your Google Drive account. In the top left corner select &#8220;New&#8221;, then &#8220;More&#8221; in the drop-down panel, and then &#8220;Google Collaboratory&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"651\" height=\"620\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/3-9.jpg\" alt=\"\" class=\"wp-image-16061\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/3-9.jpg 651w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/3-9-300x286.jpg 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/figure>\n\n\n\n<p>To open an existing Google Colab document simply right click on it &#8211;&gt; Open With &#8211;&gt; Google Collaboratory.  You can also load other people&#8217;s Google Colab documents if you share a google drive with them.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"771\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/4-10.jpg\" alt=\"\" class=\"wp-image-16062\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/4-10.jpg 681w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/4-10-265x300.jpg 265w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/figure>\n\n\n\n<p>To import\/open files directly from GitHub you will need the <a href=\"https:\/\/chrome.google.com\/webstore\/detail\/open-in-colab\/iogfkhleblhcpcekbiedikdehleodpjo\">Open in Colab<\/a> chrome extension. Add it to your chrome, then navigate over to the notebook you want to open in Github, click on your browser&#8217;s <strong>Extensions <\/strong>tab, then click <strong>Open in Colab<\/strong>.<\/p>\n\n\n\n<p>As the extension is new and is still being worked on I&#8217;d advise waiting a bit for it to get polished.<\/p>\n\n\n\n<p>Another way is to go to <a href=\"https:\/\/colab.research.google.com\/notebooks\/intro.ipynb#recent=true\">this link<\/a> and click the &#8220;New Notebook&#8221; button. You can also see that you have access to Google Colab examples, Recent notebooks, Google Drive, GitHub, and you can upload your own notebook.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"705\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/5-9-1024x705.jpg\" alt=\"\" class=\"wp-image-16063\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/5-9-1024x705.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/5-9-300x207.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/5-9-768x529.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/5-9.jpg 1051w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After opening up a new Notebook you can do a couple of things with it. The first thing is to give it a name in the upper left corner. In the upper right corner, you can click on the Settings icon.<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"265\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10-1024x265.jpg\" alt=\"\" class=\"wp-image-16064\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10-1024x265.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10-300x78.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10-768x199.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10-1536x397.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/6-10.jpg 1729w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When in settings you can change your theme to dark, set your editor key bindings and colors, change the font size, and more. Be sure to customize these features so they suit your preferences.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"727\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/7-7-1024x727.jpg\" alt=\"\" class=\"wp-image-16065\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/7-7-1024x727.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/7-7-300x213.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/7-7-768x545.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/7-7.jpg 1061w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, let us get acquainted with some of the most used shortcuts so we can save ourselves some time (for Mac users CTRL == Command):<\/p>\n\n\n\n<ul><li>Command Pallete &#8211; Ctrl+Shift+P<\/li><li>Add a comment &#8211; Ctrl+Alt+M<\/li><li>Convert to text cell &#8211; Ctrl+M M<\/li><li>Add a new cell below &#8211; Ctrl+B B<\/li><li>Run all cells &#8211; Ctrl+F9<\/li><li>Run the current cell &#8211; Ctrl+Enter<\/li><li>Save Notebook &#8211; Ctrl+S<\/li><li>Show keyboard shortcuts &#8211; Ctrl+M H<\/li><\/ul>\n\n\n\n<p>All shortcuts can be edited to suit your needs.<\/p>\n\n\n\n<p>On the left taskbar, you can view your Notebook&#8217;s table of contents that shows all the Markdown headers in a structured way, useful code snippets, files, and a search and replace tool.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"332\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/8-9.jpg\" alt=\"\" class=\"wp-image-16066\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/8-9.jpg 564w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/8-9-300x177.jpg 300w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/figure>\n\n\n\n<p>To start coding, in the upper right side you may see the connect button so be sure to click it. When connected you will see something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"253\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/9-8.jpg\" alt=\"\" class=\"wp-image-16068\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/9-8.jpg 606w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/9-8-300x125.jpg 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure>\n\n\n\n<p>Now that we know how to use some of the main features of Google Colab, we are ready to start working on a problem. I believe that this is the best way to get acquainted with new environments and learn some new things as a bonus.<\/p>\n\n\n\n<a name=\"libraries\">\n\n\n\n<h2 class=\"wp-block-heading\">How do I import libraries\/install dependencies in Google Colab?<\/h2>\n\n\n\n<p>Importing libraries and installing dependencies in Google Colab is quite easy. You need to use your usual <code>!pip install<\/code> and <code>import<\/code> commands followed by the libraries\/dependencies name.<\/p>\n\n\n\n<p>A great thing about Google Colab is that it comes with many preinstalled dependencies that are often used.<\/p>\n\n\n\n<p>Any installations only remain for the duration of your session, so if you close the session\/notebook, you&#8217;ll have to run inline installations whenever you open your project again.<\/p>\n\n\n\n<p>You can check which version of a library you&#8217;re using with<code> !pip show<\/code>. For example, to check which version of TensorFlow you are using you would use <code>!pip show tensorflow<\/code><\/p>\n\n\n\n<p>To upgrade an already installed library to the latest version, use <code>!pip install --upgrade tensorflow<\/code><\/p>\n\n\n\n<p>And finally to install a specific version, use <code>!pip install tensorflow==1.2<\/code><\/p>\n\n\n\n<a name=\"gpu\">\n\n\n\n<h2 class=\"wp-block-heading\">How do I enable GPU\/TPU usage in Google Colab?<\/h2>\n\n\n\n<p>All you have to do in Google Colab to enable a GPU or TPU is head over to the &#8220;Runtime&#8221; section, select &#8220;Change runtime type&#8221; and select either GPU or TPU.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"457\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/25.png\" alt=\"\" class=\"wp-image-8858\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/25.png 654w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/25-300x210.png 300w\" sizes=\"(max-width: 654px) 100vw, 654px\" \/><\/figure>\n\n\n\n<p>Due to TPU&#8217;s specialist nature, there are some best practices you can use to help optimize your data flow to utilize them to their fullest potential. The <a rel=\"noreferrer noopener\" href=\"https:\/\/colab.research.google.com\/notebooks\/tpu.ipynb#scrollTo=FpvUOuC3j27n\" target=\"_blank\">&#8220;TPUs in Colab&#8221;<\/a> section of the Google Colab docs highlights some of these.<\/p>\n\n\n\n<a name=\"import\">\n\n\n\n<h2 class=\"wp-block-heading\">How do I import data in Google Colab?<\/h2>\n\n\n\n<p>There are several ways to import data with Google Colab from a Google Drive, including mounting your Google Drive in the Colab notebook&#8217;s runtime&#8217;s virtual machine, using PyDrive, and using a native REST API.<\/p>\n\n\n\n<p>We&#8217;ll go over how to mount your Google Drive quickly here, but you can learn about how to use the other methods (and other data loading\/saving options) <a rel=\"noreferrer noopener\" href=\"https:\/\/colab.research.google.com\/notebooks\/io.ipynb\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>To mount you drive, simply run the following code:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from google.colab import drive\ndrive.mount(&#39;\/content\/drive&#39;)<\/code><\/pre><\/div>\n\n\n\n<p>You&#8217;ll be given a link to authorize this action via a code output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"223\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/10-1-1024x223.png\" alt=\"\" class=\"wp-image-8817\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/10-1-1024x223.png 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/10-1-300x65.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/10-1-768x167.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/10-1.png 1057w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After clicking the link, it will take you to the following screen where you will click allow.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"439\" height=\"851\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/11-11.jpg\" alt=\"\" class=\"wp-image-16069\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/11-11.jpg 439w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/11-11-155x300.jpg 155w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/figure>\n\n\n\n<p>After that, a authorization code will appear that you will copy and paste it in the cell. Press enter and that&#8217;s it.<\/p>\n\n\n\n<p>Now for instance you could open a text document in your drive, write some text in it and save it:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>with open(&#39;\/content\/drive\/My Drive\/foo.txt&#39;, &#39;w&#39;) as f:\n  f.write(&#39;Hello Google Drive!&#39;)\n!cat \/content\/drive\/My\\ Drive\/foo.txt\n\ndrive.flush_and_unmount()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"288\" height=\"304\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/12-8.jpg\" alt=\"\" class=\"wp-image-16070\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/12-8.jpg 288w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/12-8-284x300.jpg 284w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/figure>\n\n\n\n<p>For machine learning work, we will usually be loading tabular data from csv or xlsx files into pandas data frames or loading image data into arrays, so let&#8217;s quickly cover how to do exactly those things with Google Colab!<\/p>\n\n\n\n<p>Let&#8217;s assume we&#8217;ve already mounted our drive like shown just above. Upload the csv\/xlsx file you want to use onto Google Drive, then browse for its location. <\/p>\n\n\n\n<p>The default path to your drive is <code>'\/content\/drive\/My&nbsp;Drive\/'<\/code>, and our file that we used in another article is directly in the files section- not in any further folders- so its path is <code>'\/content\/drive\/My&nbsp;Drive\/Name'<\/code>.<\/p>\n\n\n\n<p>We can now simply use the pandas <code>read_csv <\/code>function to load the file directly into a pandas data frame:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import pandas as pd\n\npath = &quot;\/content\/drive\/My Drive\/Name&quot;\n\ndf = pd.read_csv(path)\n\ndf #displays dataframe<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"392\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10-1024x392.jpg\" alt=\"\" class=\"wp-image-16071\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10-1024x392.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10-300x115.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10-768x294.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10-1536x589.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/13-10.jpg 1824w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can also upload a file directly from your computer using the following code:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from google.colab import files\nuploaded = files.upload()<\/code><\/pre><\/div>\n\n\n\n<p>Click on <em>Choose Files<\/em>, browse to your desired file and open it.<\/p>\n\n\n\n<p>Finally, we can use the BytesIO function from the io module to stream the data into a pandas data frame:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import io\n\ndf2 = pd.read_csv(io.BytesIO(uploaded[&#39;reddit_wsb.csv&#39;]))<\/code><\/pre><\/div>\n\n\n\n<p>As the dataset that we&#8217;ll use comes from Kaggle we shall open it directly into Google Colab without needing to download it manually or use one of the above-mentioned ways.<\/p>\n\n\n\n<div class=\"wp-block-group is-layout-flow\"><div class=\"wp-block-group__inner-container\">\n<a name=\"machine-learning\">\n\n\n\n<h2 class=\"wp-block-heading\">Using Machine Learning in Google Colab to predict house prices<\/h2>\n\n\n\n<p>Our main problem will be to create a machine learning algorithm in Google Colab that will be able to predict future house prices. For this, we will use the good old California House dataset that can be found <a href=\"https:\/\/www.kaggle.com\/camnugent\/california-housing-prices\">here<\/a>.<\/p>\n\n\n\n<a name=\"load-kaggle\">\n\n\n\n<h2 class=\"wp-block-heading\">How can I load Kaggle datasets directly into Google Colab?<\/h2>\n\n\n\n<p>Kaggle has its own API client which allows users to obtain the datasets directly into their notebooks and I&#8217;ll show you how to do it for Google Colab.<\/p>\n<\/div><\/div>\n\n\n\n<p>For this, we will need to create a Kaggle API token. Go to your Kaggle account details and scroll down to the API section. When there click the &#8220;Create New API Token&#8221; button and a Kaggle JSON file will be downloaded.<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"165\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/14-10-1024x165.jpg\" alt=\"\" class=\"wp-image-16072\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/14-10-1024x165.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/14-10-300x48.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/14-10-768x124.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/14-10.jpg 1171w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now we go back to our notebooks and import the required libraries to load the dataset:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>#pip install kaggle - Should come preinstalled\nimport pandas as pd\nfrom google.colab import files<\/code><\/pre><\/div>\n\n\n\n<p>Now let&#8217;s upload our downloaded Kaggle.json file and check if it is in the right place:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>files.upload()\nls -lha kaggle.json<\/code><\/pre><\/div>\n\n\n\n<p>The next thing that we need to do is to set the file configuration:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># The Kaggle API client expects this file to be in ~\/.kaggle,\n# and we will move it there\n!mkdir -p ~\/.kaggle\n!cp kaggle.json ~\/.kaggle\/\n\n# This permissions change avoids a warning on Kaggle tool startup.\n!chmod 600 ~\/.kaggle\/kaggle.json<\/code><\/pre><\/div>\n\n\n\n<p>Now we are ready to download our dataset directly from Kaggle. Go to our dataset on Kaggle and Copy the API command as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"542\" height=\"216\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/15-8.jpg\" alt=\"\" class=\"wp-image-16073\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/15-8.jpg 542w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/15-8-300x120.jpg 300w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/figure>\n\n\n\n<p>Then paste it as a new command and Google Colab will download it:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!kaggle datasets download -d camnugent\/california-housing-prices<\/code><\/pre><\/div>\n\n\n\n<p>Now we unzip the file and remove the zip:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Unzip the data and delete the zip\n!unzip california-housing-prices.zip  && rm california-housing-prices.zip<\/code><\/pre><\/div>\n\n\n\n<p>If you access the Files icon on your toolbar that is on the left, you will se our obtained housing dataset.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"337\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/16-6.jpg\" alt=\"\" class=\"wp-image-16074\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/16-6.jpg 499w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/16-6-300x203.jpg 300w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s see the dataset:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df = pd.read_csv(&#39;\/content\/housing.csv&#39;)\ndf.head()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"170\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7-1024x170.jpg\" alt=\"\" class=\"wp-image-16075\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7-1024x170.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7-300x50.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7-768x128.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7-1536x256.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/17-7.jpg 1737w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<a name=\"eda\">\n\n\n\n<h2 class=\"wp-block-heading\">How can I visualize data\/produce charts using Google Colab?<\/h2>\n\n\n\n<p>Google Colab is similar to Jupyter Notebooks so you can instantly see your graphs after running the graphing command. The most used graphing libraries are matplotlib, seaborn, ggplot, plotly, and more.<\/p>\n\n\n\n<p>Now that we have our dataset we want to explore it by conducting an Explanatory Data Analysis (EDA). Let&#8217;s give our dataset a quick glance at the values it has.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.info()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"454\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/18-5.jpg\" alt=\"\" class=\"wp-image-16076\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/18-5.jpg 584w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/18-5-300x233.jpg 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<p>We can already see that the variable <code>total_bedrooms <\/code>has some missing values. We also see that all variables are numerical except for the ocean_proximity one. We will take care of it later but let&#8217;s see what it has:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df[&#39;ocean_proximity&#39;].value_counts()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"194\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/19-3.jpg\" alt=\"\" class=\"wp-image-16077\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/19-3.jpg 512w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/19-3-300x114.jpg 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure>\n\n\n\n<p>For a quick glance at the numerical variables, we can use the pandas <code>describe <\/code>function as shown:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.describe()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"252\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2-1024x252.jpg\" alt=\"\" class=\"wp-image-16078\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2-1024x252.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2-300x74.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2-768x189.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2-1536x377.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/20-2.jpg 1685w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now let&#8217;s graph these variables with matplotlib. We will first check out the histogram:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>df.hist(bins=50, figsize=(15,10))\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image alignwide size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"891\" height=\"590\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download.jpg\" alt=\"\" class=\"wp-image-16079\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download.jpg 891w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-300x199.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-768x509.jpg 768w\" sizes=\"(max-width: 891px) 100vw, 891px\" \/><\/figure>\n\n\n\n<p>The first thing that we can see from the <code>median_income<\/code> histogram is that the values were preprocessed, in this case they were scaled. After checking out the information behind the data I&#8217;ve uncovered that each number is expressed in tens of thousands of dollars (e.g. 5 \u2248 50000$).<\/p>\n\n\n\n<p>We also see that most of our distributions have quite a skew i.e. they lean more towards the left side. Also, our variables have different scales and we will deal with this issue later on.<\/p>\n\n\n\n<p>Here is a tip: Before performing an EDA be sure to split the data to train and test sets to avoid the Data Snooping Bias. Data snooping&nbsp;is the inappropriate use of data mining to uncover misleading relationships in data.<\/p>\n\n\n\n<p>So let&#8217;s split our data into train and test sets but with a twist. As median income is a good predictor of the house value, we want to split our data in a way that will be representative of the median income stratums.<\/p>\n\n\n\n<p> If you check out the median income histogram you will see that most of the data is between 1.5 and 6 but it goes beyond 6 too. If the sets don&#8217;t contain enough instances of each stratum the model might be biased towards one.<\/p>\n\n\n\n<p>In order to combat this, we will use the <code>pd.cut<\/code> function to stratify the data by 1.5 increments.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import numpy as np\nfrom sklearn.model_selection import StratifiedShuffleSplit\n\n# As we want the stratums of data income to be representative we will split the data by them\n# But first, we need to create these stratums\ndf[&#39;income_stratums&#39;] = pd.cut(df[&#39;median_income&#39;],\n                               bins=[0., 1.5, 3.0, 4.5, 6., np.inf],\n                               labels=[1, 2, 3, 4, 5])\n\ndf[&#39;income_stratums&#39;].hist()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"248\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-1.jpg\" alt=\"\" class=\"wp-image-16080\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-1.jpg 381w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-1-300x195.jpg 300w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><\/figure>\n\n\n\n<p>Now we split the data by the stratums and delete them:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Split the data by stratums\nsplit = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)\nfor train_index, test_index in split.split(df, df[&#39;income_stratums&#39;]):\n  train_set = df.loc[train_index]\n  test_set = df.loc[test_index]\n\n# Delete the income_stratums column\nfor stratum in (train_set, test_set):\n  stratum.drop(&#39;income_stratums&#39;, axis=1, inplace=True)<\/code><\/pre><\/div>\n\n\n\n<p>The next thing is to change this graph to show us the housing prices. The radius around each circle will show the population of a district and the color will represent the price.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>train_set.plot(kind=&#39;scatter&#39;, x=&#39;longitude&#39;, y=&#39;latitude&#39;, figsize=(10,10),\n               s=train_set[&#39;population&#39;]\/100, label=&#39;population&#39;,\n               c=&#39;median_house_value&#39;, cmap=&#39;rainbow&#39;,\n               colorbar=True)\nplt.legend()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"609\" height=\"565\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-3.jpg\" alt=\"\" class=\"wp-image-16081\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-3.jpg 609w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/download-3-300x278.jpg 300w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure>\n\n\n\n<p>As we can see, the high-density areas are the Bay Area and around San Diego and Los Angeles. Also, there is density in the Central Valley around Fresno and Sacramento.<\/p>\n\n\n\n<p>The housing prices are also correlated with the density area as one could expect. Also, houses closer to the sea tend to be more expensive. When speaking of correlations, we should check them out.<\/p>\n\n\n\n<p>But before we go to create a correlation matrix we should see if our features make sense aka we could make them more informative.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Before looking at correlations we might want to create new features \n# that make more sense\n# Let&#39;s look at the variables that we have\ntest_set<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"348\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4-1024x348.jpg\" alt=\"\" class=\"wp-image-16082\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4-1024x348.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4-300x102.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4-768x261.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4-1536x522.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/21-4.jpg 1658w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For example, three features can be combined to be more informative. Try to find them. If you&#8217;re thinking of creating bedrooms per room, population per household, and rooms per household you were correct.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Create new features\ndf[&#39;bedrooms_per_room&#39;] = df[&#39;total_bedrooms&#39;]\/df[&#39;total_rooms&#39;]\ndf[&#39;population_per_household&#39;] = df[&#39;population&#39;]\/df[&#39;households&#39;]\ndf[&#39;rooms_per_household&#39;] = df[&#39;total_rooms&#39;]\/df[&#39;households&#39;]\n\n# Check for correlations\ncorrelations = df.corr()\ncorrelations[&#39;median_house_value&#39;].sort_values(ascending=False)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"569\" height=\"409\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/22-2.jpg\" alt=\"\" class=\"wp-image-16083\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/22-2.jpg 569w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/22-2-300x216.jpg 300w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/figure>\n\n\n\n<p>We can see that our <code>room_per_household <\/code>is more correlated with the label than the <code>total_rooms <\/code>or households. Also, the <code>bedrooms_per_room <\/code>is more correlated with the label than its parent variables.<\/p>\n\n\n\n<p>To interpret some of them, we see that the higher the median income is &#8211; the higher are the house prices. The lower the bedrooms per room ratio is &#8211; the higher the prices get.<\/p>\n\n\n\n<p>Let&#8217;s see how the top 4 variables by correlation look when correlated to each other:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>variables =[&#39;median_house_value&#39;, &#39;median_income&#39;,\n            &#39;bedrooms_per_room&#39;, &#39;rooms_per_household&#39;]\npd.plotting.scatter_matrix(df[variables], figsize=(12, 10))<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image alignwide size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"610\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/downloafd.jpg\" alt=\"\" class=\"wp-image-16084\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/downloafd.jpg 736w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/downloafd-300x249.jpg 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/figure>\n\n\n\n<p>Okay, here we can see how variables behave to each other. If you look at median_income and median_house_value you might notice that our prices are capped at $500k.<\/p>\n\n\n\n<p>You can also notice that data tends to group in a few horizontal lines around $450k, $350k, and $280k. These are the things one should take care of before passing the data to the algorithm as the algorithm might learn these occurrences.<\/p>\n\n\n\n<a name=\"deployment\">\n\n\n\n<h2 class=\"wp-block-heading\">How can I deploy ML algorithms in Google Colab?<\/h2>\n\n\n\n<p>Machine Learning algorithms can be used in Google Colab the same way you use them in any other coding environment. Google Colab also comes with preinstalled ML libraries like TensorFlow and scikit-learn.<\/p>\n\n\n\n<p>Have in mind that we will go fast through the following sub-sections and that you should check out our Sklearn Introduction article if you get stuck on a certain point.<\/p>\n\n\n\n<a name=\"prepare\">\n\n\n\n<h3 class=\"wp-block-heading\">Prepare the Data<\/h3>\n\n\n\n<p>Before we pick a few ML models and deploy them, we want to prepare (preprocess) our data to be ready for the algorithms. So let&#8217;s do that. <\/p>\n\n\n\n<p>We already know that we have one categorical feature (<code>ocean_proximity<\/code>) and the rest are numerical. As they are different from each other, they will be preprocessed in different ways. <\/p>\n\n\n\n<p>Firstly, let&#8217;s split our train set into two parts where one will contain the label.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Split Train Set\nhousing_features = train_set.drop(&#39;median_house_value&#39;, axis=1)\nhousing_label = train_set[&#39;median_house_value&#39;].copy()<\/code><\/pre><\/div>\n\n\n\n<p>Now as we want to automate the process of data preparation, we will split the <code>housing_features<\/code> into numerical and categorical. After that, we will create our own functions and sklearn pipelines to process them.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Split housing_features to categorical and numerical sets\nnumerical = housing_features.drop(&#39;ocean_proximity&#39;, axis=1)\ncategorical = housing_features[&#39;ocean_proximity&#39;].copy()<\/code><\/pre><\/div>\n\n\n\n<p>Now we will create a sklearn numerical pipeline that:<\/p>\n\n\n\n<ul id=\"block-12f764bd-e42a-4b7d-beae-210876ac89f2\"><li>Imputes missing data by the median value<\/li><li>Creates new features<\/li><li>Standardizes the numerical features<\/li><\/ul>\n\n\n\n<p>The first thing we want to do is to import our dependencies and create a custom function that will create new features (the ones we created before). The function should also allow us to choose which features to include so we can test them.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn import pipeline\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.impute import SimpleImputer\n\n# Create a function that creates new features (Inspired by Aurelien Geron)\nrooms_ix, bedrooms_ix, population_ix, households_ix = 3, 4, 5, 6\nclass FeatureGenerator(BaseEstimator, TransformerMixin):\n  def __init__(self, add_bedrooms_per_room=True):\n    self.add_bedrooms_per_room = add_bedrooms_per_room\n  def fit(self, X, y=None)\n    return self\n  def transform(self, X, y=None)\n    rooms_per_household = X[:, rooms_ix] \/ X[:, households_ix]\n    population_per_household = X[:, population_ix] \/ X[:, households_ix]\n    if self.add_bedrooms_per_room:\n      bedrooms_per_room = X[:, bedrooms_ix] \/ X[:, rooms_ix]\n      return np.c_[X, rooms_per_household, population_per_household, \n                   bedrooms_per_room]\n    else:\n      return np.c_[X, rooms_per_household, population_per_household]<\/code><\/pre><\/div>\n\n\n\n<p>Now we can create our full numerical pipeline:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># Numerical pipeline\nnumerical_pipe = Pipeline([\n            (&#39;imputer&#39;, SimpleImputer(strategy=&#39;median&#39;)),\n            (&#39;feature_generator&#39;, FeatureGenerator()),\n            (&#39;standardizer&#39;, StandardScaler())\n            ])<\/code><\/pre><\/div>\n\n\n\n<p>The next step is to create a categorical pipeline that will perform One Hot Encoding on the <code>ocean_proximity<\/code> feature. We will combine the two pipelines into a single one and run all features through it.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn.preprocessing import OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\n\nnum_features = list(numerical)\ncat_features = [&quot;ocean_proximity&quot;]\n\nfull_pipeline = ColumnTransformer([\n        (&quot;num&quot;, numerical_pipe, num_features),\n        (&quot;cat&quot;, OneHotEncoder(), cat_features),\n    ])\n\n# Run all features through the pipeline\nhousing = full_pipeline.fit_transform(housing_features)\nhousing.shape<\/code><\/pre><\/div>\n\n\n\n<p>(16512, 16)<\/p>\n\n\n\n<p>It comes quite in handy to preprocess your data all at once. And you can easily save your pipelines and functions to use for later. When doing ML you will see that you&#8217;ll soon start creating a list of your custom functions.<\/p>\n\n\n\n<a name=\"algorithm\">\n\n\n\n<h3 class=\"wp-block-heading\">Pick an Algorithm and evaluate it<\/h3>\n\n\n\n<p>As this is a supervised regression task, we will pick a regression model. When doing ML it is advised to pick multiple algorithms and compare them to pick the best one.<\/p>\n\n\n\n<p>For the brevity of the article, we will go for a single one which is the Random Forest Regressor. For practice, you can try to build a multiple algorithm pipeline that runs them and prints the comparisons.<\/p>\n\n\n\n<p>We will also check for the RMSE which basically shows us the discrepancy between the predicted and observed values.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn.model_selection import RandomizedSearchCV\nfrom scipy.stats import randint\n\nparam_distribs = {\n        &#39;n_estimators&#39;: randint(low=1, high=200),\n        &#39;max_features&#39;: randint(low=1, high=8),\n    }\n\nforest_reg = RandomForestRegressor(random_state=42)\nrnd_search = RandomizedSearchCV(forest_reg, param_distributions=param_distribs,\n                                n_iter=10, cv=5, scoring=&#39;neg_mean_squared_error&#39;, random_state=42)\nrnd_search.fit(housing_prepared, housing_labels)<\/code><\/pre><\/div>\n\n\n\n<p>18728.778<\/p>\n\n\n\n<p>Not bad. But we are likely overfitting our data. The thing that we want to do next is to optimize the algorithm.<\/p>\n\n\n\n<a name=\"optimize\">\n\n\n\n<h3 class=\"wp-block-heading\">Optimize the Algorithm<\/h3>\n\n\n\n<p>In order to optimize the machine learning algorithm, we will perform a randomized search of specified hyperparameters. The search will look for optimal ones that we should use for our model.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sklearn.model_selection import RandomizedSearchCV\nfrom scipy.stats import randint\n\nhyperparameters = {\n        &#39;n_estimators&#39;: randint(low=1, high=250),\n        &#39;max_features&#39;: randint(low=1, high=10),\n    }\n\nrnd_search = RandomizedSearchCV(clf, param_distributions=hyperparameters,\n                                n_iter=15, cv=5, scoring=&#39;neg_mean_squared_error&#39;, random_state=42)\nrnd_search.fit(housing, housing_label)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"396\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2-1024x396.jpg\" alt=\"\" class=\"wp-image-16085\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2-1024x396.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2-300x116.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2-768x297.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2-1536x594.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/23-2.jpg 1727w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Let&#8217;s see our errors and pick the hyperparameters that give us the lowest value:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>cv = rnd_search.cv_results_\nfor mean_score, params in zip(cv[&quot;mean_test_score&quot;], cv[&quot;params&quot;]):\n    print(np.sqrt(-mean_score), params)<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"459\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/24.jpg\" alt=\"\" class=\"wp-image-16086\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/24.jpg 813w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/24-300x169.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/24-768x434.jpg 768w\" sizes=\"(max-width: 813px) 100vw, 813px\" \/><\/figure>\n\n\n\n<p>Seems like it is 8 max features and 189 estimators. There were other things to do like doing a grid search to see which features are the best and similar. But it won&#8217;t be our focus for this article. <\/p>\n\n\n\n<p>Let&#8217;s see how the model performs on the test set:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model = RandomForestRegressor(n_estimators=189, max_features=8, random_state=42)\n\nX_test = test_set.drop(&quot;median_house_value&quot;, axis=1)\ny_test = test_set[&quot;median_house_value&quot;].copy()\n\nX_test = full_pipeline.transform(X_test)\nmodel.fit(housing, housing_label)\npredictions = model.predict(X_test)\n\nmse = mean_squared_error(y_test, predictions)\nrmse = np.sqrt(mse)\nrmse<\/code><\/pre><\/div>\n\n\n\n<p>46879.44<\/p>\n\n\n\n<p>That&#8217;s it. Feel free to play with other regression models and see how they behave while we move on to our next header.<\/p>\n\n\n\n<a name=\"save\">\n\n\n\n<h2 class=\"wp-block-heading\">How can I save my Google Colab notebook directly to GitHub?<\/h2>\n\n\n\n<p>To save your Google Colab notebook to GitHub you will go to the &#8220;File&#8221; section and select &#8220;Save a copy in GitHub&#8221;. After that, a pop-up screen will ask for your authorization. And you will then do the usual repository stuff.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"511\" height=\"612\" data-id=\"16087\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/26-1.jpg\" alt=\"\" class=\"wp-image-16087\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/26-1.jpg 511w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/26-1-250x300.jpg 250w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"634\" data-id=\"16088\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/27.jpg\" alt=\"\" class=\"wp-image-16088\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/27.jpg 834w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/27-300x228.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/27-768x584.jpg 768w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/figure>\n<\/figure>\n\n\n\n<a name=\"mount\">\n\n\n\n<h2 class=\"wp-block-heading\">How can I mount external Python files in Google Colab?<\/h2>\n\n\n\n<p>Suppose you have some Python code stored in your Google Drive and you want to run it in Google Colab with their GPU\/TPU. To mount the external file write the following command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from google.colab import drive\ndrive.mount(&#39;\/content\/drive&#39;)<\/code><\/pre><\/div>\n\n\n\n<p>You will be provided with an URL that will take you to a new tab to give permission to Google Drive. After you allow access to Google Drive you will be given an authorization code to enter in your code cell.<\/p>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"81\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28-1024x81.jpg\" alt=\"\" class=\"wp-image-16089\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28-1024x81.jpg 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28-300x24.jpg 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28-768x61.jpg 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28-1536x122.jpg 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/28.jpg 1730w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"429\" height=\"854\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/30.jpg\" alt=\"\" class=\"wp-image-16090\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/30.jpg 429w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2022\/07\/30-151x300.jpg 151w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><\/figure>\n\n\n\n<p>To list the contents of your drive run the following command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!ls &quot;\/content\/drive\/My Drive\/Colab Notebooks&quot;<\/code><\/pre><\/div>\n\n\n\n<p>To run a specific content, for example hello.py, write the following:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>!python3 &quot;\/content\/drive\/My Drive\/Colab Notebooks\/hello.py&quot;<\/code><\/pre><\/div>\n\n\n\n<a name=\"magic\">\n\n\n\n<h2 class=\"wp-block-heading\">What are Google Colab Magics?<\/h2>\n\n\n\n<p>Google Colab Magics are a set of system commands that can be seen as a mini extensive command language. There are two types of magics which are Line and Cell magics.<\/p>\n\n\n\n<p>The line magics start with %, while the cell magics start with %%. To see a full list of available magics run the following command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>%lsmagic<\/code><\/pre><\/div>\n\n\n\n<p>Now let&#8217;s run a line magic that will show you your local directory:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>%ldir<\/code><\/pre><\/div>\n\n\n\n<p> And a cell magic:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>%%html\n&lt;marquee style=&#39;width: 50%; color: Red;&#39;&gt;Welcome to Algotrading101!&lt;\/marquee&gt;<\/code><\/pre><\/div>\n\n\n\n<a name=\"features\">\n\n\n\n<h2 class=\"wp-block-heading\">What are some other interesting Google Colab features?<\/h2>\n\n\n\n<p>Google Colab has other interesting features like markdown that shows nice mathematical equations, custom widgets, forms, and more. To take a look at these features go <a href=\"https:\/\/colab.research.google.com\/notebooks\/intro.ipynb\">here<\/a>.<\/p>\n\n\n\n<a name=\"common-machine-learning-testing-mistakes\">\n\n\n\n<h2 class=\"wp-block-heading\">What are the 3 Common Machine Learning Analysis\/Testing Mistakes?<\/h2>\n\n\n\n<p>When you run your analysis, there are 3 common mistakes to take note:<\/p>\n\n\n\n<ul><li>Overfitting<\/li><li>Look-ahead Bias<\/li><li>P-hacking<\/li><\/ul>\n\n\n\n<p>Do check out this lecture PDF to learn more:&nbsp;<a href=\"https:\/\/course.algotrading101.com\/courses\/pt101-practical-python-for-finance-trading-masterclass\/lectures\/27360454\">3 Big Mistakes of Backtesting \u2013 1) Overfitting 2) Look-Ahead Bias 3) P-Hacking<\/a><\/p>\n\n\n\n<a name=\"full-code\">\n\n\n\n<h2 class=\"wp-block-heading\">Full Code<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/IgorWounds\/Google-Colab-Guide\">GitHub Link<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9290\" class=\"pvc_stats total_only  \" data-element-id=\"9290\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Table of contents: What is Google Colab? Is Google Colab free? Why should I use Google Colab? Why shouldn&#8217;t I use Google Colab? What are the alternatives to Google Colab? Does Google Colab support Python? How do I get started with Google Colab? How do I import libraries\/install dependencies in Google Colab? How do I [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":9296,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"no","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[3],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Google Colab - A Step-by-step Guide - AlgoTrading101 Blog<\/title>\n<meta name=\"description\" content=\"Google Colab is a online notebook-like coding environment that is well-suited for machine learning and data analysis.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/algotrading101.com\/learn\/google-colab-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Google Colab - A Step-by-step Guide - AlgoTrading101 Blog\" \/>\n<meta property=\"og:description\" content=\"Google Colab is a online notebook-like coding environment that is well-suited for machine learning and data analysis.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/algotrading101.com\/learn\/google-colab-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-11T18:47:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-03T21:08:36+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/05\/Google-Colab-Guide-e1620759490851.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Igor Radovanovic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Igor Radovanovic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"26 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Google Colab - A Step-by-step Guide - AlgoTrading101 Blog","description":"Google Colab is a online notebook-like coding environment that is well-suited for machine learning and data analysis.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/","og_locale":"en_US","og_type":"article","og_title":"Google Colab - A Step-by-step Guide - AlgoTrading101 Blog","og_description":"Google Colab is a online notebook-like coding environment that is well-suited for machine learning and data analysis.","og_url":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/","og_site_name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","article_published_time":"2021-05-11T18:47:56+00:00","article_modified_time":"2023-04-03T21:08:36+00:00","og_image":[{"width":1000,"height":667,"url":"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/05\/Google-Colab-Guide-e1620759490851.jpg","type":"image\/jpeg"}],"author":"Igor Radovanovic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Igor Radovanovic","Est. reading time":"26 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/#article","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/"},"author":{"name":"Igor Radovanovic","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/a7ae60c112a73b7c3fe14ac56726a0ae"},"headline":"Google Colab &#8211; A Step-by-step Guide","datePublished":"2021-05-11T18:47:56+00:00","dateModified":"2023-04-03T21:08:36+00:00","mainEntityOfPage":{"@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/"},"wordCount":3654,"publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"articleSection":["Programming"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/","url":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/","name":"Google Colab - A Step-by-step Guide - AlgoTrading101 Blog","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/#website"},"datePublished":"2021-05-11T18:47:56+00:00","dateModified":"2023-04-03T21:08:36+00:00","description":"Google Colab is a online notebook-like coding environment that is well-suited for machine learning and data analysis.","breadcrumb":{"@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/algotrading101.com\/learn\/google-colab-guide\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/algotrading101.com\/learn\/google-colab-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/algotrading101.com\/learn\/"},{"@type":"ListItem","position":2,"name":"Google Colab &#8211; A Step-by-step Guide"}]},{"@type":"WebSite","@id":"https:\/\/algotrading101.com\/learn\/#website","url":"https:\/\/algotrading101.com\/learn\/","name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","description":"Authentic Stories about Algorithmic trading, coding and life.","publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/algotrading101.com\/learn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/algotrading101.com\/learn\/#organization","name":"AlgoTrading101","url":"https:\/\/algotrading101.com\/learn\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/","url":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","contentUrl":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","width":1200,"height":627,"caption":"AlgoTrading101"},"image":{"@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/a7ae60c112a73b7c3fe14ac56726a0ae","name":"Igor Radovanovic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d46175c509b3ee240a1e2bbe735a4d1e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d46175c509b3ee240a1e2bbe735a4d1e?s=96&d=mm&r=g","caption":"Igor Radovanovic"},"sameAs":["https:\/\/igorradovanovic.com","https:\/\/www.linkedin.com\/in\/igor-radovanovic-profile"],"url":"https:\/\/algotrading101.com\/learn\/author\/igor\/"}]}},"modified_by":"Igor Radovanovic","_links":{"self":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/9290"}],"collection":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/comments?post=9290"}],"version-history":[{"count":12,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/9290\/revisions"}],"predecessor-version":[{"id":21154,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/9290\/revisions\/21154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media\/9296"}],"wp:attachment":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media?parent=9290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/categories?post=9290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/tags?post=9290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}