Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Open In Colab Open In Kaggle

This notebook will be used in the lab session for week 1 of the course and provides some hands-on experience applying the lessons to environmental science datasets. We will be using data from Wilks’ book on Statistical Methods for the Atmospheric Sciences.

Notebook Setup

Let’s begin by loading relevant data from the cloud.

Here's a data sample. You can copy the row header text from here if you need it later 😉
Loading...

Linear Regression

The goal for this exercise is to train a linear regression model and a logistic regression model to forecast atmospheric temperature using atmospheric pressure. 🌡

For the first case, we want to train linear regression to calculate June temperatures (the predictand) from June pressures (as the predictor) in Guayaquil, Ecuador.

Guayacil.jpg

Caption A beautiful day in Guayacil, Ecuador. Can you predict how hot it will be? 🌞

We can try addressing this question using a linear regression model from scikit.

Q1) Import the LinearRegression model. Instantiate it and fit it using the A3 dataframes’ pressure and temperature.

We now have a linear regression model for the temperature and pressure. Let’s make some plots to visualize our data and get a qualitative sense of our model.

Q2) Generate a scatter plot with the linear regression plot for our data.

We now have a qualitative verification of our model! Your figure should look similar to this one:

Qualitative verifications are nice - but this is not enough! Let’s do some quantitative analyses:

Q3) Print the slope of our model. Find the F-score, p-value, and R2R^2 statistics for our model.

If your code works just the way ours does, you’ll get:

The slope of the line is: -0.92
The f score is: 40
The R² value is: 0.69

Classification

Let’s use the same dataset to train a classifier for El Niño years. \

We will use the June temperature and pressure in Guayaquil as the predictors for El Niño. \

Let’s begin by setting up a training and testing dataset. Since the dataset is so small, we’ll set aside one each of a random El Niño year and a non-El Niño year for our test dataset, and the remaining points as our training dataset.

ENSO.png

Source: NOAA “What is Enso?”

Caption: Can we predict whether we are in an El Niño phase based on June temperatures and pressures in Guayaquil, Ecuador?

Test years: [1951 1958]
Train years: [1969 1957 1965 1953 1970 1963 1968 1961 1967 1964 1960 1962 1956 1952
 1954 1959 1966 1955]

We’re going to train a logistic regression classifier on the dataset, but in this exercise we’ll rely on the scikit learn implementation!

Q4) Make the training and test datasets from the source data.

Hint 1: Scikit-learn’s LogisticRegression classifier is documented at this link.

Hint 2: Before training, use the dataframes’ .loc method with the test/train list, then convert the values to numpy (e.g., using this method) and .ravel() the truth if necessary. You can also do what was done in the previous section when handling single columns (i.e., using .values.reshape() on single column data).

Loading...

Q5) Import and instantiate the logistic regression classifier from scikit. Fit it using the training dataset.

That should hopefully have felt much simpler than our previous exercise. Now that you have a trained model, let’s see if our model is able to predict whether the test years are El Niño years!

Q6) Predict whether each of the two test years was an El Niño year using the logistic regression model, and print out the prediction alongside the truth.

Hint: To find which method of your LogisticRegression classifier to use to make predictions, don’t hesitate to consult its documentation at this link.

If your code reproduces our exact results, you should get the following as a printout: \

Was 1951 an El Niño year? True. We predicted True
Was 1958 an El Niño year? False. We predicted False

And with that, we’re done for the week! Take a moment to relax, or if you’re feeling up to a challenge go back and do the challenge questions for the other notebooks 😀