ML for Climate Data
โจ PAN'S RULE: The data has a story to tell, you just have to learn how to listen! โจ
What if you could be a weather detective ๐ต๏ธโโ๏ธ? Or a superhero who tells a whole city when to save energy? Today, we're teaching a computer to look at clues about our planet and maybe even predict the future!
The Big Picture: Becoming a Weather Detective
So what does a weather detectiveโor a "meteorologist"โactually do? They use data (clues!) from the past to understand what might happen next. This video from SciShow Kids gives you a peek into how they work. We're about to do the same thing, but with code!
๐ง How Does a Computer Learn? Let's Play!
We showed our computer this dog: ๐ฆฎ and told it "This is a dog." It learned to look for things with four legs, fur, and a tail. Now, which of these new pictures will it most likely call a 'dog'?
Your First Mission: Treasure Hunt! ๐ฏ
Before we write code, let's practice being data detectives. Your mission: explore the interactive chart below to find the answers to the treasure hunt questions. You can click and drag to zoom in, and hover your mouse to see the exact numbers!
- What was the temperature on Jan 1, 1981?
- What was the temperature on July 1, 1985?
- Can you find the hottest day on the whole chart? What temperature was it?
Pre-Code Challenge: Draw the Trend!
Look at the bumpy line in the chart above. It goes up and down every single day! But is there a hidden, long-term trend? Before we use code, use your eyes. Can you guess where the "average" line would go? Try tracing the general direction with your finger on the screen! This is exactly what we're about to ask the computer to do.
Your Code Lab: Predicting with Python
Ready to use the same tools as real data scientists? We're going to use a tool called Google Colab. It's like a Google Doc, but for writing and running Python code right in your browser. No installation needed! Let's go!
Here's the secret plan:
- STEP 1 ๐งฐ: We import our "toolboxes" (special code libraries).
- STEP 2 โ๏ธ: We load a real weather dataset from the internet.
- STEP 3 ๐ค: We ask how many clues (days of data) we have.
- STEP 4 ๐: We take a quick peek at the first few rows of data.
- STEP 5 ๐: We tell the computer to turn all that data into a cool-looking chart!
Now you're ready! Open a new Colab notebook and copy-paste this code into a cell. Press the play button (โถ๏ธ) to run it!
# 1. Get our super-toolboxes (called libraries)
import pandas as pd # Pandas helps us work with data tables
# 2. Load a real dataset from the internet
# This is a history of minimum daily temperatures in Melbourne, Australia
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv'
df = pd.read_csv(url)
# 3. How many clues do we have?
print(f"This dataset has {len(df)} days of weather clues!")
# 4. Be a detective: let's peek at the first 5 clues!
print("\nHere's a peek at the data:")
print(df.head())
# 5. The magic moment: turn the data into a picture!
print("\nCreating your temperature plot... check it out!")
df.plot(title='Daily Minimum Temperatures in Melbourne')
When you run this, you'll see how many clues our AI detective has to work with (that's almost 10 years of data!), a table of data, AND a chart that visualizes it all. You just used Python to analyze real-world climate data.
๐งช Tinker Time: Your Turn to Experiment!
Real scientists ask "what if?" Try these challenges in your Colab notebook:
- Change
df.head()todf.tail(). What does it show you now? (Hint: "tail" is the opposite of "head"!) - Can you change the title of the plot? Find the text
'Daily Minimum Temperatures in Melbourne'and change it to something fun, like'My Awesome Weather Chart'. - In the second code block (the Level Up one below), change
window=365towindow=30. Run it again. Does the line get smoother or bumpier? Why do you think that is?
๐ค What's a "Rolling Average"?
Imagine you wanted to know your average video game score, but you only looked at the last 10 games you played. That's a rolling average! It smooths out the wild up-and-down scores from single games to show your *real* skill trend over time. We're doing the same thing with 365 days of weather to find the planet's real trend!
Level Up: Find the Hidden Trend
Okay, you've seen the data. But a true data scientist looks for the hidden story. Is the temperature *generally* going up or down? It's hard to tell with all those daily spikes! Let's use that "rolling average" trick to find the trend line. Add this new code to a new cell in your Colab notebook and run it!
# This calculates the average temperature over a full year (365 days)
# and plots that trend! It smooths out the daily bumps.
# You need to run this in a NEW cell after the first one.
# It also needs the 'Temp' column to be a number, let's fix that first!
df['Temp'] = pd.to_numeric(df['Temp'], errors='coerce')
print("Calculating the 365-day average trend...")
df['Temp'].rolling(window=365).mean().plot(title='Smoothed Temperature Trend in Melbourne')
WHOA! See that new line? It's much smoother. It shows the real, long-term pattern hidden in the data. You just used your first data analysis model to find a hidden signal.
Your Final Mission: Predict Tomorrow!
Finding a trend is cool, but what about predicting the future? This is where true Machine Learning comes in. We'll use a famous toolbox called `scikit-learn` to build a simple "model" that learns from all the past data and makes a guess about the next day. This is itโthe big moment! Add this to a new code cell.
# This is real Machine Learning!
# We'll use a simple model to predict the next value.
from sklearn.linear_model import LinearRegression
import numpy as np
# We need to reshape our data for the model
X = np.array(range(len(df))).reshape(-1, 1)
y = df['Temp'].dropna() # Drop any missing values
X = X[:len(y)] # Make sure X and y are the same size
model = LinearRegression()
model.fit(X, y)
# Let's predict the temperature for the very next day!
next_day_index = len(df)
prediction = model.predict([[next_day_index]])
print(f"\n๐ค AI Prediction for the next day's temp: {prediction[0]:.2f} degrees")
You did it! That number is your AI's best guess for the future based on all the data from the past. You just built and ran a real predictive model!
๐ Pro Challenge: Become a Data Explorer
You just analyzed weather in Australia. What about Tokyo? Or your own city? Find a simple, public CSV dataset from a trusted source like Kaggle Datasets or data.gov. Try to load it into your Colab notebook by changing the `url` variable. You might need to change the column names in the code, too. This is exactly what real data scientists do every day!
๐จโ๐ฉโ๐ง Parent Corner
Your young scientist is using Python and Google Colabโthe exact same tools used by data scientists at NASA, Google, and Pixar to analyze everything from climate change to movie animations. This isn't just a toy, it's the real deal!
๐ Talk About It: Ask them to show you the "smoothed-out" trend line. Does it generally go up or down over the 10 years in the data? What do you think that means for the city of Melbourne?
๐ Learn More
- See real temperature data from NASA - Explore the real-time data!
- Our World in Data - See how scientists visualize climate information.