Lab 0 - Getting Started with Google Colab
Lab 0 - Getting Started with Google Colab
1. Introduction
This lab sheet will guide you through setting up and using Google Colab for your
machine learning projects. Google Colab (short for "Colaboratory") is a free cloud
service that provides you with a Jupyter Notebook environment that runs entirely in
the cloud. This allows you to write and execute Python code directly in your browser,
making it an excellent platform for machine learning experiments.
2. Objectives
4. Basic Usage
• Code Cells:
o Colab notebooks consist of code cells where you write and execute
Python code.
o To execute a code cell, press Shift+Enter or click the "Play" button.
o Output from the code will be displayed below the cell.
• Markdown Cells:
o Colab also supports Markdown cells for adding text, formatting, and
visualizations to your notebook.
o To change a cell to Markdown, click the dropdown menu and select
"Markdown".
o Use Markdown syntax to format your text (e.g., headings, bold, italics,
lists, links).
o
• Uploading Files:
o You can upload files to your Colab environment using the "Files" tab on
the left-hand side of the notebook.
Prepared by Simon Lau Boung Yew Page 2 of 5
COMP2024 Spring 2025
o Click on the "Upload" button and select the file you want to upload.
• Mounting Google Drive:
o To access files from your Google Drive, you can mount your drive to the
Colab environment:
o This will prompt you to authorize Colab to access your Google Drive.
Follow the on-screen instructions to complete the authentication process.
o This will allow you to access and read/write files from your Google Drive
within your Colab notebook.
o Once mounted, you can access your Google Drive files using the
following path: /content/drive/MyDrive/
o For example, to list the files in your Drive:
!ls /content/drive/MyDrive/
o You can now work with your files as if they were on your local machine.
For instance, to read a CSV file:
import pandas as pd
file_path = '/content/drive/MyDrive/my_data.csv'
df = pd.read_csv(file_path)
• Importing Libraries:
o Import the necessary libraries for your machine learning project:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
6. Runtime
• Changing Runtime Type:
o Colab provides different runtime environments (e.g., CPU, GPU, TPU)
with varying computational power.
o To change the runtime type:
Go to "Runtime" -> "Change runtime type"
Select the desired hardware accelerator (e.g., "None" for CPU,
"GPU", "TPU").
This is crucial for performance-intensive tasks like deep learning.
• Restarting Runtime:
o Restarting the runtime clears the memory and reloads all libraries.
Go to "Runtime" -> "Restart runtime"
Choose "Restart and clear output" to also clear the output of all
cells.
• Interrupting Execution:
o To stop the execution of a currently running cell:
Go to "Runtime" -> "Interrupt execution"
Figure 5: Runtime
Additional Resources
• Colab Documentation: https://colab.research.google.com/
• Jupyter Notebook Documentation: https://docs.jupyter.org/
Note: This is a basic introduction. Explore the Colab interface and experiment with
different features to gain a deeper understanding of its capabilities.