Using Google Colab
Abstract:
What is Google Colab?
Google Colaboratory, or Google Colab , is a free, cloud-based Jupyter notebook environment provided by Google. It allows you to write and execute Python code in your browser —with no setup required and free access to GPUs and TPUs for computation-heavy tasks like deep learning. Think of it as Google Docs, but for Python code.
Colab is widely used in data science, machine learning, and education because it provides an accessible, flexible, and powerful coding environment.
Why Use Google Colab for Data Science?
- No Installation Needed: Everything runs in the browser. You don't need to install Python, Jupyter, or any packages locally.
- Free GPUs and TPUs: Great for machine learning experiments and prototyping.
- Seamless Integration with Google Drive: Your notebooks are stored in the cloud and synced across devices.
- Built-in Collaboration: Share notebooks and work with your team like you would with Google Docs.
- Pre-installed Libraries: Colab comes with many popular Python packages (like NumPy, pandas, matplotlib, TensorFlow, etc.) ready to go.
Getting Started with Google Colab
Step 1: Access Colab
You can open Colab in two main ways:
- Direct link: https://colab.research.google.com
- Or open a new notebook directly from Google Drive:
- Go to Google Drive
-
Click
+ New
→More
→Google Colaboratory
Tip: If you don't see "Google Colaboratory" in the menu, click
+ Connect more apps
and search for it to install.
Step 2: Create a New Notebook
Once you're in Colab:
-
Click
File
→New notebook
to create a fresh notebook. - You'll see a familiar notebook interface with code cells and text (Markdown) cells , just like in Jupyter Notebooks.
Understanding the Colab Interface
- Code Cells: Where you write and execute Python code.
- Text Cells: Written in Markdown; great for explanations, notes, and formatting.
- Runtime Menu: Manage your execution environment.
-
Runtime → Change runtime type
allows you to select:- Python version
- Hardware accelerator (None, GPU, TPU)
Working with Files and Google Drive
Colab can mount your Google Drive , so you can access and save datasets, models, and scripts directly.
Mounting Drive:
from google.colab import drive
drive.mount('/content/drive')
After running, you'll be prompted to authorize access. Once mounted, your Drive contents will be available under
/content/drive/My Drive/
.
Importing and Using Libraries
Many libraries come pre-installed. You can import and use them as usual:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
If a library isn’t available, you can install it with a
!pip
command:
!pip install seaborn
Saving Your Work
By default, your notebook is saved in your Google Drive under
Colab Notebooks
. You can also download it in multiple formats:
-
File → Download
as.ipynb
(Jupyter),.py
, or.pdf
Collaboration & Sharing
-
Use the
Share
button (top right) to invite collaborators. - You can set access permissions (view/comment/edit), just like a Google Doc.
Advanced Tips
-
Use GitHub Repositories:
Open a notebook from GitHub via
File → Open notebook → GitHub
. -
Terminal-like Commands:
Use
!
before terminal commands:
!ls
!pwd
!pip install xgboost
- Keyboard Shortcuts:
-
Shift + Enter
→ Run cell -
Ctrl + M + B
→ Add code cell -
Ctrl + M + M
→ Add text cell
GPU and TPU Support
To use a GPU/TPU:
-
Go to
Runtime → Change runtime type
-
Select
GPU
orTPU
under "Hardware accelerator" - Check if it's active:
import tensorflow as tf
print("GPU:", tf.config.list_physical_devices('GPU'))
Common Pitfalls & How to Avoid Them
Problem | Solution |
---|---|
Notebook disconnects after inactivity | Regularly interact with it, or consider using a local runtime |
Session resets and data is lost | Always save important data to Drive |
Can't install some libraries | Make sure the package is pip-compatible or use a local runtime |
Learn More:
https://research.google.com/colaboratory/faq.html
https://www.youtube.com/watch?v=inN8seMm7UI
Leave a Comment