Get Started:
hw1_exercises.pyBy completing this assignment, you will:
uvuv is a fast Python package manager that we’ll use throughout the course. Install it by following the instructions for your operating system:
macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
Windows (PowerShell):
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Verify the installation by running:
uv --version
Deliverable: Screenshot showing uv --version output.
Create a dedicated virtual environment for this course:
# Create a new directory for the course
mkdir mphy6120
cd mphy6120
# Initialize a new project with uv
uv init
# Create virtual environment with Python 3.11
uv venv --python 3.11
Activate the virtual environment:
macOS/Linux:
source .venv/bin/activate
Windows:
.venv\Scripts\activate
Deliverable: Screenshot showing your activated virtual environment (your prompt should show (.venv) or similar).
Install the core packages we’ll use throughout the course:
uv pip install torch numpy pandas matplotlib jupyter ipykernel
Note: If you have an NVIDIA GPU and want CUDA support, follow the PyTorch installation guide for your specific setup.
Verify your installation by starting Python and running:
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
print(f"PyTorch version: {torch.__version__}")
print(f"NumPy version: {np.__version__}")
print(f"Pandas version: {pd.__version__}")
print("All packages imported successfully!")
Deliverable: Screenshot showing successful package imports with version numbers.
Complete the following exercises in the starter notebook (or create your own notebook named hw1_python_skills.ipynb).
Array Creation (3 pts): Create a 1D NumPy array containing the integers 1 through 10.
Random Matrix (3 pts): Create a 3x3 matrix of random values between 0 and 1 using np.random.rand().
Statistics (3 pts): For your random matrix, compute and print the mean, standard deviation, min, and max.
Reshaping (3 pts): Create a 1D array of 12 elements and reshape it into a 3x4 matrix.
Slicing (3 pts): From your 3x3 random matrix, extract a 2x2 submatrix from the top-left corner.
Create DataFrame (3 pts): Create a DataFrame with columns patient_id, age, heart_rate, and diagnosis with at least 10 rows of sample data.
Add Column (3 pts): Add a new column age_group that categorizes patients as “young” (< 40), “middle” (40-65), or “senior” (> 65).
Filter (3 pts): Filter the DataFrame to show only patients with heart rate > 80.
GroupBy (3 pts): Group by diagnosis and compute the mean age and heart rate for each group.
Missing Data (3 pts): Introduce some NaN values into your DataFrame, then demonstrate using fillna() or dropna().
Create the following visualizations with proper labels, titles, and legends:
Line Plot (2.5 pts): Plot $y = x^2$ for $x \in [0, 10]$.
Scatter Plot (2.5 pts): Create a scatter plot of random (x, y) data with at least 50 points.
Histogram (2.5 pts): Generate 1000 samples from a normal distribution and plot a histogram.
Bar Chart (2.5 pts): Create a bar chart showing the count of patients per diagnosis from your DataFrame.
Complete these exercises in the same notebook or a new one named hw1_pytorch_basics.ipynb.
From List (2 pts): Create a 2D tensor from a nested Python list.
torch.zeros(3, 4)torch.ones(2, 3)torch.rand(3, 3)torch.arange(0, 10, 2)dtype, shape, and device.Element-wise (3 pts): Create two 3x3 random tensors and perform element-wise addition, multiplication, and exponentiation.
Matrix Operations (4 pts): Perform matrix multiplication (@ or torch.matmul) and transpose.
NumPy Conversion (3 pts): Convert a PyTorch tensor to a NumPy array and back. Demonstrate that changes to one affect the other (shared memory).
Write code that:
Check Availability (4 pts): Check if CUDA is available and print the result.
GPU Info (3 pts): If CUDA is available, print the GPU device name. If not, print a message saying CPU will be used.
Device Transfer (3 pts): Create a tensor and move it to the available device (GPU if available, otherwise CPU). Print the tensor’s device.
# Example structure
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
if torch.cuda.is_available():
print(f"GPU: {torch.cuda.get_device_name(0)}")
x = torch.rand(3, 3).to(device)
print(f"Tensor device: {x.device}")
All submissions for this course are done through GitHub. This mirrors real-world software development workflows and helps you build essential version control skills.
hw1_exercises.py (replace None values with your code)git add hw1_exercises.py
git commit -m "Complete NumPy exercises 2.1.1-2.1.5"
git push
Good commit habits are part of your grade. We expect:
a]b3f2d1 Complete Part 3: PyTorch CUDA check
8c4e5f6 Add tensor operations and NumPy conversion
2d1a9b7 Complete Part 2: matplotlib visualizations
f7e8c3a Add pandas DataFrame exercises
9a2b4c5 Complete NumPy array operations
1e6d8f2 Verify environment setup - all packages installed
In the era of AI-assisted coding, your commit history tells the story of your work:
| Component | Points |
|---|---|
| Part 1: Environment Setup | 25 |
| 1.1 uv installation verified | 8 |
| 1.2 Virtual environment created | 8 |
| 1.3 Packages installed correctly | 9 |
| Part 2: Python Skills | 40 |
| 2.1 NumPy exercises | 15 |
| 2.2 Pandas exercises | 15 |
| 2.3 Matplotlib visualizations | 10 |
| Part 3: PyTorch Basics | 25 |
| 3.1 Tensor creation | 8 |
| 3.2 Tensor operations | 9 |
| 3.3 GPU/CUDA check | 8 |
| Git Workflow | 10 |
| Multiple meaningful commits | 5 |
| Clear commit messages | 5 |
| Total | 100 |