1. Data Preparation
Data integrity was a top priority. My goal was to prepare this longitudinal dataset for a full scale analysis:
- Utilizing Time: Extracted "Season" and "Time of Day" from timestamps to get a full understanding of each incident.
- Categorical Pruning: Only injuries considered to be minor, major, or fatal were included in this analysis.
- Missing Values: Discarded "Unknown" entries for "Species" and filtered out "Incomplete" reports.
2. Machine Learning Pipeline
Building an accurate model required more than selecting an algorithm. This project followed a structured machine learning workflow, beginning with feature engineering and progressing through model comparison, validation, and interpretation. Rather than optimizing solely for training performance, each stage emphasized generalization to unseen data.
3. Model Selection
Several supervised learning approaches were evaluated, including Random Forest, XGBoost, feature reduction techniques, and oversampling methods to address class imbalance. Although the more sophisticated approaches improved training performance, they consistently reduced accuracy on unseen test data, indicating overfitting. Random Forest produced the strongest generalization and ultimately had the best fit.
| Model | Accuracy | Observation |
|---|---|---|
| Random Forest | 64% | Best predictive ability |
| XGBoost (XGB) | 60% | Overfit with small dataset |
| XGB Oversampling | Below baseline | Higher variance |
| XGB Feature Reduction | Below baseline | Removed useful information |
The final Random Forest classifier achieved 64% accuracy on the held-out testing data. Although this may appear modest compared to many benchmark datasets, it represents the strongest-performing model after systematically evaluating multiple algorithms. More complex methods, including XGBoost and oversampling techniques, consistently produced poorer generalization despite higher training performance.
3. Why Random Forest Fits Best
Random Forest proved particularly well suited for this project because the dataset contains relatively few observations, several categorical variables, and inherently noisy historical records. Unlike boosting algorithms, which sequentially learn from previous errors and often overfit small datasets, Random Forest combines the predictions of hundreds of independently trained trees. This ensemble approach produced more stable predictions and ultimately delivered the highest performance on unseen incidents.
4. Feature Analysis
By looking into the most prominent features present in this analysis it comes to light that shark attacks are not purely random and even follow certain trends.
- Year: The strongest single predictor is "Year," but this is likely a side-effect of there being an overall decrease in deaths from shark attacks. Medical advancements and more granular reporting of minor incidents are likely to have made "Year" as strong of a factor as it is.
- Activity: There is a clear "severity gap" between Surfing and Freediving/Scuba. So, while surfers represent the highest volume of incidents, they more frequently sustain Minor injuries. In contrast, Freediving/Scuba incidents skew heavily toward Major or Fatal outcomes, likely due to the lack of a board as a physical buffer and the obvious constraint of being confined to the depths of the ocean (i.e., without immediate oxygen, the ability to speak, or seek aid).
- Species: The data exhibits a near-total dominance of the Great White in high-severity incidents. Meaning that if you had a choice to meet one shark, and would prefer to live on to tell the tale, it ought not to be the Great White.
- Geography & Biology: A striking finding is that near the major breeding grounds of seals and sea lions (Farallon Islands, Año Nuevo, Point Conception, Morro Bay area, San Miguel Island), 71.6% of attacks resulted in "Major" or "Fatal" injuries. Outside of these areas, severe attacks drop to 42.9%. This indicates that when Great Whites are in these distinct areas, they are in a heighted predatory state and thus hunting to kill.
5. Dataset Limitations
- Subjective Labels: Severity classifications such as Minor, Major, and Fatal are not based on standardized measurements. Historical records often rely on medical judgement, which introduces inconsistency when comparing incidents across decades.
- Historical Reporting: The dataset spans more than seventy years of recorded incidents. Reporting practices, available technology, and the level of detail collected have changed significantly over time, creating potential bias in older observations.
- Unobserved Variables: Important contextual factors such as water conditions, shark behavior, victim response, environmental conditions, and emergency response time are not consistently documented, limiting the model's ability to capture the full complexity of each event.
Full Python Notebook available here.
One of the most important findings of this project was not simply the prediction accuracy, but the realization that model complexity cannot overcome limitations in the underlying data. After comparing multiple machine learning approaches, Random Forest proved to be the most reliable model because it generalized better to new observations than more sophisticated alternatives. This project demonstrates an essential principle of applied machine learning: selecting the model that best generalizes is often more valuable than pursuing increasingly complex algorithms that merely memorize the training data.