Run in Google Colab
|
View on GitHub
|
Google Colab Basics¶
Welcome to the Usable AI course! This notebook will help you get started with Google Colab, an interactive, cloud-based Jupyter environment. If you already have access to some notebooks, simply click on the Open in Colab links provided in your course materials. This is one of the options for running the course notebooks.
Using Google Colab¶
One option for this course is to use Google Colab.
Google Colab allows you to write and execute Python code on remote servers. This means:
No need to install Python or any libraries on your local machine.
Access to powerful computing resources directly from the browser.
Easy collaboration by sharing notebooks with peers.
While Google Colab offers a convenient cloud-based environment, keep in mind the following limitations:
Sessions are temporary and may disconnect after a period of inactivity.
There are restrictions on GPU/TPU usage and RAM.
The runtime environment resets after disconnecting, which means unsaved work is lost.
Dependency management can be challenging if specific package versions are required.
You always need to install and import the necessary libraries at the beginning of each notebook.
For more details, check out the Google Colab Documentation.
1. Setting Up the Colab Environment¶
To begin:
Open your Colab notebook from Google Colab.
You can also use the Open in Colab button provided by your course materials.
Once your notebook opens, you'll see this guide and can start running cells immediately.
2. Mounting Google Drive¶
Many datasets in this course are stored on Google Drive. The cell below shows how to mount your drive:
Run the cell.
Click on the link that appears.
Log into your Google account and authorize the access.
Copy and paste the provided authorization code back into the notebook.
from google.colab import drive
import pandas as pd
drive.mount('/content/drive')
3. Importing Data from Google Drive¶
After mounting your drive, you can access your data files. For example, to load a CSV file called dataset.csv located in MyDrive/data/, update the file path accordingly and run the cell below.
This example includes a simple error handling block in case the file isn't found.
# Update the file path as necessary
file_path = '/content/drive/MyDrive/data/dataset.csv'
try:
df = pd.read_csv(file_path)
print("Data loaded successfully. Here are the first few rows:")
print(df.head())
except FileNotFoundError:
print(f"Error: The file at {file_path} was not found. Please verify the path.")
except Exception as e:
print("An error occurred while loading the file:", str(e))
4. Installing Libraries¶
Google Colab comes with many popular libraries pre-installed. However, if you need to install additional packages, you can do so using !pip install.
For example, to install the seaborn library, run the cell below:
!pip install seaborn
Importing Installed Libraries¶
After installing a library, you can import it in your notebook as usual:
import seaborn as sns
# example usage:
sns.lineplot(x=[1, 2, 3, 4], y=[1, 4, 9, 16])
5. Next Steps¶
Now that you've set up your environment:
Explore the notebooks provided in the course.
Try running additional code cells and modifying examples.
Consider experimenting with different datasets and visualizations.
If you encounter issues, refer to the course documentation or ask for help.
Enjoy your journey with Usable AI and happy coding!
Run in Google Colab
View on GitHub