Skip to content

Commit

Permalink
added sections to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
VisheshSaluja committed Aug 15, 2024
1 parent c78a610 commit ce0d960
Showing 1 changed file with 41 additions and 73 deletions.
114 changes: 41 additions & 73 deletions book/chapters/validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,20 @@
"The goal of predicting Snow Water Equivalent exemplifies the integration of Machine learning with environmental science. This chapter delved into the testing and Evaluation part of this project."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IFvX25Khr2dW"
},
"source": [
"To begin with, it is essential to grasp the function of the BaseHole class. This class represents the complete lifecycle of the project, guiding it from initial development through to its final deployment."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rkO0EdSFr4q1"
},
"source": [
"BaseHole class is a meticulously crafted blueprint for constructing models capable of predicting SWE. It offers a structured approach to handling data, training models, and making predictions with unparalleled precision."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Y-PZlyQKr48X"
},
"source": [
"Let’s briefly discuss what is happening in this BaseHole class--\n",
"\n",
"\n",
"\n",
"* Preprocessing: The model begins with preprocessing, a critical phase where raw data is transformed into a refined form suitable for training. The BaseHole class adeptly navigates this phase, loading data, cleaning it, and splitting it into training and testing sets. This preparatory step ensures that the models are fed data that is both digestible and informative, setting the stage for accurate predictions.\n",
"* Training: This is the center of Learning, with the data primed, the BaseHole class now moves on to the training phase. This is where the coalition of machine learning takes place as the class utilizes the power of its classifiers to learn from the training data. The model, through this process, uncovers patterns and insights hidden within the data, providing itself with the knowledge needed to predict SWE with confidence.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2B9qmiO7r5N9"
},
"source": [
"Now comes the part that is one of the main focus of this chapter—**Testing**\n",
"\n",
"Within the extensive array of functionalities provided by the BaseHole class, the testing process is akin to a rigorous examination.\n",
"Unveiling the test function:\n",
"\n",
"So, what is a test?\n",
"## 8.1 Testing\n",
"\n",
"The test function operates on a simple yet profound principle: it utilizes the model to predict outcomes based on the test dataset. By invoking the classifier's prediction method, the BaseHole class utilizes the trained model on the test data to forecast SWE values with precision.\n"
"### 8.1.1 So, what is a test?\n",
"\n",
"The test function operates on a simple yet profound principle: it utilizes the model to predict outcomes based on the test dataset which the model has not seen earlier during the training. This method is fundamental to understanding how well the model generalizes to new, unseen data."
]
},
{
Expand All @@ -82,9 +50,9 @@
"id": "qpzGEyDQsFaZ"
},
"source": [
"**The Mechanics of Testing**\n",
"### 8.1.2. The Mechanic of Testing\n",
"\n",
"At its core, the test function embodies the essence of machine learning validation. It executes the trained model's prediction method on the test_x dataset—a collection of features that the model has not encountered during its training phase. The function then returns the predicted SWE values, encapsulated within test_y_results, offering a glimpse into the model's predictive accuracy and reliability."
"The `test()` method uses the trained model to make predictions on test_x, a dataset that was not part of the training process. The output, test_y_results, provides a preview of the model’s performance, offering insights into its predictive capabilities."
]
},
{
Expand All @@ -93,7 +61,7 @@
"id": "-0nRqBFQsICP"
},
"source": [
"# Validation/Evaluation"
"## 8.2 Validation/Evaluation"
]
},
{
Expand All @@ -102,7 +70,9 @@
"id": "toh-cEggsKZ0"
},
"source": [
"So now we have made a model, trained the model, and made predictions on a test dataset, but how to evaluate all of this? For this, we use multiple Evaluation metrics. A model needs to go through a rigorous validation process that assesses its effectiveness and accuracy. Evaluation is a testament to the model’s commitment to precision, ensuring that the predictions made are not only reliable but also meaningful."
"### 8.2.1 Importance of Evaluation\n",
"So now we have made a model, trained the model, and made predictions on a test dataset, but how to evaluate all of this? <br />\n",
"For this, we use multiple Evaluation metrics. A model needs to go through a rigorous validation process that assesses its effectiveness and accuracy. Evaluation is a testament to the model’s commitment to precision, ensuring that the predictions made are not only reliable but also meaningful."
]
},
{
Expand All @@ -120,7 +90,7 @@
"id": "dpbM36bgsRCB"
},
"source": [
"**Insights**\n",
"### 8.2.2 Insights\n",
"\n",
"Upon invoking the evaluation method, the class starts a detailed analysis of the model's predictions. By comparing these predictions against actual values from the test dataset, the method illuminates the model's strengths and areas for improvement.\n",
"\n",
Expand Down Expand Up @@ -151,7 +121,7 @@
"id": "PdYjCmmbsVzW"
},
"source": [
"# The Evaluation Process"
"### 8.2.3 The Evaluation Process"
]
},
{
Expand All @@ -160,7 +130,7 @@
"id": "IlRUU0lzsW4r"
},
"source": [
"Upon invocation, the evaluate method undertakes the task of computing these metrics, using the predictions generated by the RandomForestHole model (self.test_y_results) and comparing them against the actual values (self.test_y) from the test dataset. This comparison is the crux of the evaluation, offering a window into the model's predictive capabilities."
"The `evaluate()` method in the model classes is responsible for computing the above metrics, using the predictions generated by the model and comparing them against actual values from the test dataset.\n"
]
},
{
Expand Down Expand Up @@ -230,11 +200,11 @@
"id": "RjgBbKVVsbKF"
},
"source": [
"**Computing the Metrics:** Leveraging the metrics module from scikit-learn, the function calculates MAE, MSE, R2, and RMSE. Each of these calculations provides a different lens through which to view the model's performance, from average error rates (MAE, RMSE) to the model's explanatory power (R2) and the variance of its predictions (MSE).\n",
"- **Computing the Metrics:** Leveraging the metrics module from scikit-learn, the function calculates MAE, MSE, R2, and RMSE. Each of these calculations provides a different lens through which to view the model's performance, from average error rates (MAE, RMSE) to the model's explanatory power (R2) and the variance of its predictions (MSE).\n",
"\n",
"**Interpreting the Results:** The function not only computes these metrics but also prints them out, offering immediate insight into the model's efficacy. This step is vital for iterative model improvement, allowing data scientists to diagnose and address specific areas where the model may fall short.\n",
"- **Interpreting the Results:** The function not only computes these metrics but also prints them out, offering immediate insight into the model's efficacy. This step is vital for iterative model improvement, allowing data scientists to diagnose and address specific areas where the model may fall short.\n",
"\n",
"**Returning the Metrics:** Finally, the function encapsulates these metrics in a dictionary and returns it. This encapsulation allows for the metrics to be easily accessed, shared, and utilized in further analyses or reports, facilitating a deeper understanding of the model's impact and areas for enhancement.\n"
"- **Returning the Metrics:** Finally, the function encapsulates these metrics in a dictionary and returns it. This encapsulation allows for the metrics to be easily accessed, shared, and utilized in further analyses or reports, facilitating a deeper understanding of the model's impact and areas for enhancement.\n"
]
},
{
Expand All @@ -248,39 +218,36 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "C1pxcIYgsvKD"
},
"metadata": {},
"source": [
"### 8.2.4 Practical Example of Evaluation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All the necessary functions are called in the model_train_validate process"
"The script provides practical examples by loading pre-trained models and evaluating them using the test dataset:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "D2hKsntLszCu"
},
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def main():\n",
" print(\"Train Models\")\n",
" # Choose the machine learning models to train (e.g., RandomForestHole, XGBoostHole, ETHole)\n",
" worm_holes = [ETHole()]\n",
" for hole in worm_holes:\n",
" # Perform preprocessing for the selected model\n",
" hole.preprocessing()\n",
" print(hole.train_x.shape)\n",
" print(hole.train_y.shape)\n",
" # Train the machine learning model\n",
" hole.train()\n",
" # Test the trained model\n",
" hole.test()\n",
" # Evaluate the model's performance\n",
" hole.evaluate()\n",
" # Save the trained model\n",
" hole.save()\n",
" print(\"Finished training and validating all the models.\")"
"base_model = joblib.load(f\"{homedir}/Documents/GitHub/snowcast_trained_model/model/wormhole_random_forest_basic.joblib\")\n",
"basic_predicted_values = evaluate(base_model, all_features, all_labels, \"Base Model\")\n",
"\n",
"best_random = joblib.load(f\"{homedir}/Documents/GitHub/snowcast_trained_model/model/wormhole_random_forest.joblib\")\n",
"random_predicted_values = evaluate(best_random, all_features, all_labels, \"Optimized\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, the script loads two models—a base model and an optimized model—and evaluates their performance on the same test dataset. This side-by-side comparison allows for an assessment of how model optimization impacts predictive accuracy and overall performance."
]
},
{
Expand All @@ -289,7 +256,8 @@
"id": "JJDaq-uls1AR"
},
"source": [
"In conclusion, testing and validation form the bedrock of predictive excellence in the SnowCast project. They are not merely steps in the machine learning workflow but are the very processes that ensure the models we build are not just algorithms but are reliable interpreters of the natural world."
"Testing and validation form the bedrock of predictive excellence in the SnowCast project. They are not merely steps in the machine learning workflow but are essential processes that ensure the models we build are reliable interpreters of environmental data. By rigorously testing and evaluating models, we can trust that their predictions will be both accurate and meaningful in real-world applications.\n",
"\n"
]
}
],
Expand Down

0 comments on commit ce0d960

Please sign in to comment.