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.

In this notebook we classify seismic recordings into four source types β€” earthquake, explosion, surface event, and noise β€” from 61 physical waveform features (spectral shape, envelope statistics, kurtosis, band energies). The dataset is a curated set of Pacific Northwest seismic events, 1000 per class, archived on Zenodo: DOI 10.5281/zenodo.14025693.

We compare three classic classifiers: Support Vector Machine, k-nearest neighbors, and Random Forest, and we evaluate them per class with classification reports, confusion matrices, and one-vs-rest ROC curves.

This dataset returns in notebook 3.9, and it anchors the class leaderboard defined at the end of this notebook.

πŸ–₯️ Lecture slides β€” Session 15 (Mon Nov 2)

1. Load the dataΒΆ

The loader below downloads and caches the four class files, concatenates them into one table, and drops the one feature column with missing values.

2. ExploreΒΆ

Check the class balance first. The four classes have 1000 events each, so this curated set is balanced. Keep in mind that real catalogs are not.

<Figure size 640x480 with 1 Axes>

Build the feature matrix X (61 numeric waveform features) and the label vector y (four source-type strings).

X: (4000, 61)  y: (4000,)
A few feature names: ['Window_Length', 'RappMaxMean', 'RappMaxMedian', 'AsDec', 'KurtoSig', 'KurtoEnv', 'SkewSig', 'SkewEnv']

3. Train/test splitΒΆ

The cell below is the canonical split for this chapter: this exact line defines the leaderboard split at the end of the notebook. Everyone trains on the same X_train and predicts on the same X_test.

Two details matter:

  • An earlier version of this lesson used shuffle=False. With the table ordered by class, that put whole classes in the test set and none in training β€” the classifier never saw the classes it was scored on.
  • stratify=y preserves the class proportions in both halves. That matters most when classes are imbalanced, and real seismic catalogs are: noise windows outnumber earthquakes by orders of magnitude.

4. Scale after the splitΒΆ

We fit the scaler on the training set only, then transform both sets. Fitting the scaler on all the data would let the test set’s statistics leak into training.

5. BaselineΒΆ

Before any model, score a trivial baseline. DummyClassifier(strategy="most_frequent") always predicts the majority class; with four balanced classes it scores 0.25. Every model below must beat this number.

Baseline accuracy: 0.25

6. Three classifiersΒΆ

The SVM and k-nearest neighbors both rely on distances or margins in feature space, so they use the scaled features.

SVC test accuracy: 0.877
K-nearest Neighbors test accuracy: 0.83

Random Forest works on the unscaled features: trees split on thresholds, and a monotonic rescaling does not change which side of a threshold a point falls on.

Random Forest test accuracy: 0.882

7. Per-class evaluationΒΆ

Accuracy is one number. The classification report gives precision, recall, and F1 per class, and the confusion matrix shows which classes get mistaken for each other.

Support Vector Machine
Classification report for classifier SVC():
               precision    recall  f1-score   support

   earthquake       0.82      0.87      0.84       250
    explosion       0.88      0.79      0.83       250
        noise       0.90      0.93      0.92       250
surface event       0.92      0.92      0.92       250

     accuracy                           0.88      1000
    macro avg       0.88      0.88      0.88      1000
 weighted avg       0.88      0.88      0.88      1000


<Figure size 640x480 with 2 Axes>
K-nearest neighbors
Classification report for classifier KNeighborsClassifier():
               precision    recall  f1-score   support

   earthquake       0.78      0.84      0.81       250
    explosion       0.79      0.68      0.73       250
        noise       0.92      0.91      0.92       250
surface event       0.83      0.89      0.86       250

     accuracy                           0.83      1000
    macro avg       0.83      0.83      0.83      1000
 weighted avg       0.83      0.83      0.83      1000


<Figure size 640x480 with 2 Axes>
Random Forest
Classification report for classifier RandomForestClassifier(random_state=42):
               precision    recall  f1-score   support

   earthquake       0.83      0.86      0.85       250
    explosion       0.87      0.80      0.83       250
        noise       0.93      0.94      0.93       250
surface event       0.90      0.94      0.92       250

     accuracy                           0.88      1000
    macro avg       0.88      0.88      0.88      1000
 weighted avg       0.88      0.88      0.88      1000


<Figure size 640x480 with 2 Axes>

Which class pairs get confused the most? Compare the off-diagonal terms across the three confusion matrices. A cross-tabulation of true labels against Random Forest predictions makes the counts easy to read.

Loading...

The dominant confusion is between explosions and earthquakes. That is physically plausible: quarry blasts and shallow earthquakes excite similar frequency content at regional distances, so their waveform features overlap.

8. One-vs-rest ROC curvesΒΆ

ROC curves are defined for binary problems. For a multiclass problem we use the one-vs-rest strategy: binarize the labels (one column per class) and fit one binary classifier per class.

To keep the split identical to the canonical one, we binarize y_train and y_test obtained above rather than re-splitting the data.

<Figure size 700x600 with 1 Axes>

Class leaderboardΒΆ

Train any classifier you like on X_train. Feature engineering is allowed. Do not peek at y_test while developing: select your model and hyperparameters with cross-validation on the training set only β€” scoring candidates on rotating validation subsets carved from the training data (lesson 3.8).

When you are done:

  1. Predict on the canonical X_test (the split cell in section 3 defines it).
  2. Save your predictions to results/predictions_<uwnetid>.csv with the format below.
  3. Submit the file by pull request to the course repository. CI scores macro-F1 β€” the per-class F1 scores averaged with equal weight on every class β€” against the held-out labels and posts a leaderboard.

The row_id column is the row position in the canonical concatenated table; the split preserves it, so it identifies each test sample. The demo below writes a submission file from the Random Forest predictions.

Loading...
ReferencesΒΆ
  1. Kharita, A. (2024). Physical features for small sample of data (1000 events per class). Zenodo. 10.5281/ZENODO.14025693