0% found this document useful (0 votes)
4 views

Lab 0 - Getting Started with Google Colab

This document is a lab guide for setting up and using Google Colab for machine learning projects. It covers objectives such as familiarizing with the Colab environment, executing Python code, and using libraries for data processing and model training. Additionally, it provides instructions on file management, runtime settings, and sharing notebooks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 0 - Getting Started with Google Colab

This document is a lab guide for setting up and using Google Colab for machine learning projects. It covers objectives such as familiarizing with the Colab environment, executing Python code, and using libraries for data processing and model training. Additionally, it provides instructions on file management, runtime settings, and sharing notebooks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMP2024 Spring 2025

Lab 00: Setting Up and Using 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.

Figure 1: Google Colab Interface

2. Objectives

The lab aims to provide students with practical experience to


1. Familiarize with the Google Colab environment.
2. Create and execute Python code in Colab.
3. Install and import necessary libraries for machine learning.
4. Load and preprocess data within the Colab environment.
5. Train and evaluate AI algorithms / machine learning models using Colab.
6. Understand how to save and share Colab notebooks.

3. Getting Started with Google Colab


• Access Google Colab:
o Open your web browser and navigate to
https://colab.research.google.com/
o You may be prompted to sign in with your Google account.
• Create a New Notebook:
o Click on "File" -> "New notebook"
Prepared by Simon Lau Boung Yew Page 1 of 5
COMP2024 Spring 2025

o Choose the desired language (Python 3)

Figure 2: Create New / Open / Upload / Save / Download Notebook

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

Figure 3: Code and Markdown Cells

• 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:

from google.colab import drive #import the drive library


drive.mount('/content/drive') #mount the drive

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)

Figure 4: Google Drive mounted in Colab

Prepared by Simon Lau Boung Yew Page 3 of 5


COMP2024 Spring 2025

For more information: https://miteshparmar1.medium.com/mounting-


google-drive-in-google-colab-with-3-mouse-clicks-8d38d681aad5

5. Coding with Colab


• Installing Libraries:
o You can install necessary libraries using the !pip install command:

!pip install scikit-learn


!pip install tensorflow
!pip install matplotlib

• 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"

Prepared by Simon Lau Boung Yew Page 4 of 5


COMP2024 Spring 2025

Figure 5: Runtime

7. Saving and Sharing Notebooks


• Saving:
o Colab automatically saves your work periodically.
o You can also manually save your notebook by clicking "File" -> "Save".
• Sharing:
o Share your notebook with others by clicking "Share" and entering the
email addresses of the recipients.

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.

Prepared by Simon Lau Boung Yew Page 5 of 5

You might also like