Google Colab 101 Tutorial with Python — Tips, Tricks, and FAQ
By: Towards Ai Team
Tags: Artificial Intelligence, Innovation, Technology, Education, Science

Introduction

Google Colab is a project from Google Research, a free, Jupyter based environment that allows us to create Jupyter [programming] notebooks to write and execute Python [1](and other Python-based third-party tools and machine learning frameworks such as Pandas, PyTorch, Tensorflow, Keras, Monk, OpenCV, and others) in a web browser.

📚 Check out our editorial recommendations on the best machine learning books. 📚

A programming notebook is a type of shell or kernel in the form of a word processor, where we can write and execute code. The data required for processing in Google Colab can be mounted into Google Drive or imported from any source on the internet. Project Jupyter is an open-source software organization that develops and supports Jupyter notebooks for interactive computing [4].

Google Colab requires no configuration to get started and provides free access to GPUs. One of the main functionalities of Google Colab is that it allows anyone to share live code, mathematical equations, data visualizations, data processing (cleaning and transformation), numerical simulations, machine learning models, and many other projects with others.

Why We Use Google Colab?

Google Colab has unique and critical features:

It provides a free Jupyter notebook environment.

It comes with pre-installed packages.

It hosts entirely on Google Cloud.

Users do not need to set up on servers or workstations.

Notebooks save automatically on a user’s Google Drive.

It provides browser-based Jupyter notebooks.

It is completely free of cost and offers GPU and TPU power (unless you need more resources and decide to go pro with Colab Pro ).

). It supports Python versions 2 and 3 (however, Google Suggests migrating important notebooks to Python 3 [2] [5]).

It provides two hardware accelerators:

1. GPU (Graphical Processing Unit).

2. TPU (Tensor Processing Unit).

Start Google Colab

Python code can be executed directly on the web browser by using Colab. We can launch it with the URL below:

The launch window opens with a popup offering many features:

Figure 1: Screenshot of Google Colab’s start page.

It provides options to create a notebook as well as to upload and select from different sources such as:

GitHub

Google Drive

Local computer

Uploading a Notebook from GitHub

Python code can be directly uploaded from Github by using its project’s URL or by searching the organization or user. The steps below highlight how to upload a project using a Github URL:

Launch Google Colab.

Select the GitHub tab from the popup box.

Figure 2: Screenshot of Google Colab’s upload code using a Github URL.

Enter the GitHub’s project URL and search it to fetch the code

Figure 3: Screenshot showing how to upload a Github repository with Google Colab.

It will upload the complete code with one click to the Google Colab notebook.

Figure 4: Screenshot showcasing the uploaded Github repository using a URL.

Similarly, the code can be uploaded directly from Google Drive by filtering saved notebooks by name, date, owner, or modified date.

Figure 5: Screenshot showing how to upload a notebook directly from Google Drive to Google Colab.

Uploading Data from Kaggle

Data from Kaggle can be uploaded directly into Colab for processing. An API token from Kaggle is required to accomplish the data import.

Steps to generate API token from Kaggle

Open Kaggle

Go to “My Account”

Scroll down to the “API” section

Figure 6: Screenshot of Kaggle’s website showing the API section.

Click on “Expire API Token” to remove the previous token if required.

Click on “Create New API Token.” It will generate a new token and download a JSON file named “ kaggle.json ”

” The “kaggle.json” file contains the username and key like:

Figure 7: Screenshot of IDE showing test API key for Kaggle.

Steps to upload data from Kaggle

Save the “kaggle.json” file on your local computer.

Install the Kaggle package

!pip install -q kaggle

Import packages:

from google.colab import files

Upload the local file “kaggle.json”

files.upload()

Figure 8: Screenshot of the output of the uploaded file “kaggle.json”

Check if the Colab notebook connects with Kaggle correctly.

!kaggle datasets list

Figure 9: Screenshot showing a dataset list from Kaggle.

Download any competition data from Kaggle (i.e., competition name — predict future sales)

!kaggle competitions download -c competitive-data-science-predict-future-sales

Figure 10: Screenshot showing the output of data downloads from Kaggle.

Data from Kaggle will be downloaded and uploaded in Colab, like:

Figure 11: Screenshot of Google Colab showing the uploaded data from Kaggle.

Read Files from Google Drive

Google Colab provides functionality to read data from google drive too.

Import packages

import globimport pandas as pdfrom google.colab import drive

Mount Google Drive

drive.mount('/gdrive')

This will ask Google to drive authorization code.

Figure 12: Screenshot showing Google Colab asking you to insert Google Drive’s authorization code.

Input box for the authorization code

Click on the link and generate the authorization code.

Read a CSV file from the drive.

file_path = glob.glob("/gdrive/My Drive/***.csv")for file in file_path:

df = pd.read_csv(file)

print(df)

Figure 13: Output from the test CSV file on Google Drive.

Setting up Hardware Accelerator GPU for Runtime

Google Colab provides a free cloud service with a GPU hardware accelerator. High configurations GPU machines are very costly and required in machine learning and deep learning to simultaneously process multiple computations.