Q- this my notes from the previous work done so far oky :
Q- we have create the First Ml model here :
Using logistic regression using this code :
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
from tabulate import tabulate
def train_and_validate_model(X_train, X_test, y_train, y_test, model=LogisticRegression()):
"""
Train a model and validate its performance using various metrics.
Input:
- X_train: Training features
- X_test: Testing features
- y_train: Training labels
- y_test: Testing labels
- model: Machine learning model (default: LogisticRegression)
Output:
- accuracy: Accuracy score
- precision: Precision score
- recall: Recall score
- f1: F1 score
- roc_auc: ROC AUC score
"""
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba)
return accuracy, precision, recall, f1, roc_auc
"""
Q- now just print the result after calling the method
Q- give the code to it
"""
result = train_and_validate_model(X_train, X_test, y_train, y_test)
metrics = ['Accuracy', 'Precision', 'Recall', 'F1 Score', 'ROC AUC']
for metric, value in zip(metrics, result):
print(f"{metric}: {value:.4f}")
Q- then i want to introduce MLflow to the picture like this
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import mlflow
import mlflow.sklearn
def train_and_validate_model(X_train, X_test, y_train, y_test, model=LogisticRegression()):
with mlflow.start_run():
# Log model parameters
mlflow.log_param("model", "LogisticRegression")
mlflow.log_param("test_size", 0.2)
mlflow.log_param("random_state", 42)
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba)
# Log metrics
metrics = ['accuracy', 'precision', 'recall', 'f1_score', 'roc_auc']
for metric, value in zip(metrics, [accuracy, precision, recall, f1, roc_auc]):
mlflow.log_metric(metric, value)
print(f"{metric.capitalize()}: {value:.4f}")
# Log the model
mlflow.sklearn.log_model(model, "logistic_regression_model")
return accuracy, precision, recall, f1, roc_auc
Assistant
Call the function
result = train_and_validate_model(X_train, X_test, y_train, y_test)
Q- now i want to use another model and test using principle like code reuse or “DRY” principle where i do not need to repeat the same model training pipeline if we have a function that encpsate the logic oky
Q- so first question what about knn ? is a good choice after logistic regression yes or non?
Choosing the right machine learning model depends on various factors, including the nature of your data, the problem you're trying to solve, and the specific requirements of your application. Let's delve into whether k-Nearest Neighbors (KNN) is a good choice to use alongside or after Logistic Regression in your modeling pipeline.
Understanding Logistic Regression and KNN
Logistic Regression
Type: Parametric model.
Assumptions:
The relationship between the independent variables and the log-odds of the dependent variable is linear.
Features are independent of each other.
Pros:
Interpretability: Coefficients provide insights into feature importance.
Efficiency: Computationally less intensive, especially with large datasets.
Probabilistic Outputs: Directly provides probabilities, which are useful for threshold-based decisions.
Cons:
Linearity: Struggles with non-linear relationships unless you include polynomial or interaction terms.
Feature Scaling: Less sensitive compared to KNN but can still benefit from scaling.
k-Nearest Neighbors (KNN)
Type: Non-parametric (instance-based) model.
Assumptions:
Similar instances exist in close proximity in feature space.
Pros:
Flexibility: Can model complex, non-linear decision boundaries.
No Training Phase: Essentially, training is just storing the dataset.
Cons:
Computationally Intensive: Especially with large datasets, as it requires distance calculations at prediction time.
Sensitive to Feature Scaling: Features need to be on the same scale to ensure that distance calculations are meaningful.
Curse of Dimensionality: Performance can degrade with high-dimensional data.
No Probabilistic Interpretation: Unlike Logistic Regression, KNN doesn't naturally provide probability estimates.
When to Use KNN After Logistic Regression
Exploratory Analysis: After establishing a baseline with Logistic Regression, using KNN can help identify whether non-linear patterns exist in the data that Logistic Regression couldn't capture.
Model Comparison: Employing multiple models allows you to compare performance metrics and choose the best-performing one. Since Logistic Regression and KNN have different strengths, this comparison can provide a more comprehensive understanding of your data.
Ensemble Methods: Combining predictions from Logistic Regression and KNN (e.g., via voting or stacking) can sometimes yield better performance than either model alone.
Feature Importance and Selection: Insights from Logistic Regression about feature importance can inform which features might be more relevant for distance-based models like KNN.
Considerations Before Choosing KNN
Dataset Size: KNN can be slow with large datasets. If your dataset is sizable, you might need to consider dimensionality reduction or approximate nearest neighbor techniques.
Feature Scaling: Ensure that your features are properly scaled (e.g., using StandardScaler or MinMaxScaler) to improve KNN performance.
Choosing 'k': Selecting the right number of neighbors is crucial. Too small a 'k' can lead to overfitting, while too large a 'k' can smooth out the decision boundaries excessively.
Distance Metric: The choice of distance metric (e.g., Euclidean, Manhattan) can significantly impact KNN performance. Experimenting with different metrics based on your data characteristics is advisable.
Implementation Example
Given your existing pipeline with Logistic Regression and MLflow integration, here's how you can incorporate KNN following the DRY (Don't Repeat Yourself) principle:
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import mlflow
import mlflow.sklearn
def train_and_validate_model(X_train, X_test, y_train, y_test, model):
with mlflow.start_run():
# Log model parameters
mlflow.log_param("model", model.class.name)
# Add other relevant parameters if necessary
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
if hasattr(model, "predict_proba"):
y_pred_proba = model.predict_proba(X_test)[:, 1]
else:
# For models without predict_proba (e.g., some KNN configurations)
y_pred_proba = y_pred
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba) if hasattr(model, "predict_proba") else None
# Log metrics
mlflow.log_metric("accuracy", accuracy)
mlflow.log_metric("precision", precision)
mlflow.log_metric("recall", recall)
mlflow.log_metric("f1_score", f1)
if roc_auc is not None:
mlflow.log_metric("roc_auc", roc_auc)
# Print metrics
print(f"Model: {model.__class__.__name__}")
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
if roc_auc is not None:
print(f"ROC AUC: {roc_auc:.4f}")
# Log the model
mlflow.sklearn.log_model(model, model.__class__.__name__)
return accuracy, precision, recall, f1, roc_auc
knn = KNeighborsClassifier(n_neighbors=5) # You can tune 'n_neighbors' as needed
result_knn = train_and_validate_model(X_train, X_test, y_train, y_test, knn)
Explanation of the Implementation:
Unified Function: The train_and_validate_model function now accepts any model, promoting code reuse and adhering to the DRY principle.
Model Parameters Logging: It logs the model's class name. You can extend this to log other hyperparameters as needed.
Handling predict_proba: Not all models implement predict_proba. The function checks if the model has this method to compute ROC AUC. If not, it skips this metric.
MLflow Integration: Both models' parameters, metrics, and the models themselves are logged to MLflow, enabling easy tracking and comparison.
Examples with Logistic Regression and KNN: Demonstrates how to train and evaluate both models without duplicating code.
Conclusion
KNN can be a valuable addition to your modeling toolkit, especially when you suspect non-linear relationships in your data that Logistic Regression might not capture. By leveraging a unified function for training and evaluation, you can seamlessly integrate multiple models into your pipeline, making experimentation and comparison straightforward.
However, it's essential to consider the characteristics of your dataset and the specific demands of your application when choosing between Logistic Regression, KNN, or any other model. Often, ensemble methods or model stacking can harness the strengths of multiple algorithms to achieve superior performance.
Q- now i want to you just to give the full code for knn part without the actual comments oky
Here is the full code for the k-Nearest Neighbors (KNN) part without comments:
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (accuracy_score, precision_score, recall_score,
f1_score, roc_auc_score)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import mlflow
import mlflow.sklearn
def train_and_validate_model(X_train, X_test, y_train, y_test, model):
with mlflow.start_run():
mlflow.log_param("model", model.__class__.__name__)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
if hasattr(model, "predict_proba"):
y_pred_proba = model.predict_proba(X_test)[:, 1]
else:
y_pred_proba = y_pred
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba) if hasattr(model, "predict_proba") else None
mlflow.log_metric("accuracy", accuracy)
mlflow.log_metric("precision", precision)
mlflow.log_metric("recall", recall)
mlflow.log_metric("f1_score", f1)
if roc_auc is not None:
mlflow.log_metric("roc_auc", roc_auc)
print(f"Model: {model.__class__.__name__}")
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
if roc_auc is not None:
print(f"ROC AUC: {roc_auc:.4f}")
mlflow.sklearn.log_model(model, model.__class__.__name__)
return accuracy, precision, recall, f1, roc_auc
knn = KNeighborsClassifier(n_neighbors=5)
result_knn = train_and_validate_model(X_train, X_test, y_train, y_test, knn)
Q- running this previous code i come across this result ;
Model: KNeighborsClassifier
Accuracy: 0.9339
Precision: 0.9095
Recall: 0.9551
F1 Score: 0.9318
ROC AUC: 0.9787
2024/10/16 16:33:13 WARNING mlflow.models.model: Model logged without a signature and input example. Please set input_example parameter when logging the model to auto infer the model signature.
Q- and these are the ui image from mflow ui 7
Q- give a discussion between two exeter about the result and especially the ui part of ml flow oky
Let's say Alice and Bob are reviewing the results.
Alice: Wow, Bob, look at these KNN results! The accuracy is up to 93%, significantly better than our initial Logistic Regression model.
Bob: That's a substantial improvement, Alice. The precision, recall, and F1-score are also much higher. And check out that ROC AUC – almost 98%! It seems KNN is capturing the non-linear relationships in our data much better than logistic regression.
Alice: Let's take a closer look at the MLflow UI. (Points to the UI) See, we now have two runs listed under the "Default" experiment: one for Logistic Regression and one for KNN, named "loud-skink-715." The UI makes it so easy to compare these runs side-by-side.
Bob: Exactly. Notice how the "Parameters" tab clearly shows which model was used for each run. We logged "LogisticRegression" for the first run and "KNeighborsClassifier" for this KNN run. This is essential for traceability and reproducibility.
Alice: And the "Metrics" tab is where the performance improvement really stands out. The values for accuracy, precision, recall, F1-score, and ROC AUC are all clearly displayed for both runs. It's visually very clear that KNN outperformed Logistic Regression.
Bob: You're right. This visual comparison is incredibly useful, especially when trying different models or hyperparameter settings. We could easily add more runs to this experiment as we continue to refine our model.
Alice: We should also address that warning about logging the model without a signature and input example. We saw that with Logistic Regression too. Remember, we need to add an input_example when logging the model so MLflow can automatically infer the model signature. This is helpful for downstream tasks like model serving and makes it clear what type of input the model expects.
Bob: Good point, Alice. Let's make a note to fix that. It'll improve the usability of our logged models.
Alice: Okay. I'm impressed by how straightforward it is to compare different models and track their performance using MLflow. It really simplifies the experimentation process.
Bob: Absolutely. And the centralized tracking of parameters, metrics, and artifacts makes collaboration and reproducibility so much easier. It’s a valuable addition to our workflow. Now that we have a promising KNN model, we can think about registering it in the Model Registry and moving it towards deployment.
This conversation highlights the following aspects of the results and the MLflow UI:
Performance Improvement: The KNN model demonstrates a clear performance gain over the Logistic Regression model.
MLflow UI for Comparison: The UI facilitates easy comparison of different runs within the same experiment, highlighting the performance differences between models.
Parameter and Metric Tracking: MLflow's tracking of parameters and metrics ensures clarity and reproducibility of experiments.
Model Signature Warning: The importance of addressing the model signature warning is emphasized for improved model management and deployment.
This discussion and interpretation of the UI emphasize the value of MLflow in comparing model performance and maintaining a well-organized ML workflow. It also reinforces the importance of addressing the model signature warning for better model management.
Q- now i want to add a thid model to the list here :
Q- i come across what know as decision tree model : wich i think can work for our case right ?
Q- as you are my assistance give your feabckack about the is third model
Q- and give a good robust framework to reason about it about this choice oky
Decision Tree Model Analysis
Overview of Decision Trees
Decision trees are a popular machine learning algorithm used for both classification and regression tasks. They work by creating a tree-like model of decisions based on features in the dataset.
Pros:
Interpretability: Easy to understand and explain
Non-linear relationships: Can capture complex patterns
Feature importance: Provides insight into which features are most influential
No need for feature scaling: Insensitive to the scale of features
Cons:
Overfitting: Prone to creating overly complex trees that don't generalize well
Instability: Small changes in data can result in very different trees
Biased towards dominant classes in imbalanced datasets
Framework for Evaluating Decision Trees
To determine if decision trees are suitable for your case, consider the following factors:
Data Characteristics
Is your data numerical, categorical, or mixed?
Are there complex interactions between features?
Is your dataset balanced or imbalanced?
Model Interpretability Requirements
Do you need to explain the model's decisions to stakeholders?
Is transparency in decision-making crucial for your application?
Performance Metrics
How does the decision tree perform compared to logistic regression and KNN?
Are you prioritizing accuracy, precision, recall, or a balance of these?
Computational Resources
How large is your dataset?
Do you have constraints on training or prediction time?
Overfitting Concerns
Do you have enough data to prevent overfitting?
Are you prepared to use techniques like pruning or ensemble methods?
Feature Importance
Do you need insights into which features are driving the predictions?
Deployment Considerations
How will the model be deployed and updated?
Is the simplicity of decision trees beneficial for your deployment strategy?
Implementation Suggestion
To incorporate decision trees into your existing framework:
from sklearn.tree import DecisionTreeClassifier
# ... (previous imports and function definitions)
# Create and train the decision tree model
dt_model = DecisionTreeClassifier(random_state=42)
result_dt = train_and_validate_model(X_train, X_test, y_train, y_test, dt_model)
# Compare results
print("\nComparison of Models:")
print("Logistic Regression:", result_log_reg)
print("KNN:", result_knn)
print("Decision Tree:", result_dt)
This approach allows you to easily compare the performance of the decision tree model against your existing models using the same evaluation framework.
Q- oky now i come accors the ideas of choosing the DecisionTreeCalssifer here oky :
→ but i have one question oky : as you know in software engineer we have a principle called “DRY” which promote code resume and modularity :
And we have a quote in tunisie “ne change pas equipe qui gagne “ oky
Q- levraging the style of code : here
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import mlflow
import mlflow.sklearn
def train_and_validate_model(X_train, X_test, y_train, y_test, model=LogisticRegression()):
with mlflow.start_run():
# Log model parameters
mlflow.log_param("model", "LogisticRegression")
mlflow.log_param("test_size", 0.2)
mlflow.log_param("random_state", 42)
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba)
# Log metrics
metrics = ['accuracy', 'precision', 'recall', 'f1_score', 'roc_auc']
for metric, value in zip(metrics, [accuracy, precision, recall, f1, roc_auc]):
mlflow.log_metric(metric, value)
print(f"{metric.capitalize()}: {value:.4f}")
# Log the model
mlflow.sklearn.log_model(model, "logistic_regression_model")
return accuracy, precision, recall, f1, roc_auc
Call the function
result = train_and_validate_model(X_train, X_test, y_train, y_test)
Q- just refactor it to be suibtible for decision Tree models oky :
Q- maintain of course the “mlflow” part of the project here :
Q- and just change the model to decssiion tree as we dicssus so far and of course make it first a “working code “ without bugs , clean code (name conviison , comments to input & output and one hightline describe the purpose of the function oky )
Q- just do for me
Sure, I can help you refactor your existing train_and_validate_model function to accommodate the Decision Tree Classifier while adhering to the DRY (Don't Repeat Yourself) principle. Below is the clean, modular, and bug-free implementation that maintains the MLflow integration. The function is designed to work with any scikit-learn model, including the Decision Tree Classifier.
Refactored Code for Decision Tree Classifier
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_auc_score
)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import mlflow
import mlflow.sklearn
def train_and_validate_model(X_train, X_test, y_train, y_test, model):
"""
Train a machine learning model, validate its performance, and log results with MLflow.
Parameters:
- X_train (pd.DataFrame or np.ndarray): Training features.
- X_test (pd.DataFrame or np.ndarray): Testing features.
- y_train (pd.Series or np.ndarray): Training labels.
- y_test (pd.Series or np.ndarray): Testing labels.
- model (sklearn estimator): The machine learning model to train and evaluate.
Returns:
- tuple: (accuracy, precision, recall, f1_score, roc_auc)
"""
with mlflow.start_run():
# Log model parameters
mlflow.log_param("model", model.__class__.__name__)
# Train the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
if hasattr(model, "predict_proba"):
y_pred_proba = model.predict_proba(X_test)[:, 1]
else:
y_pred_proba = y_pred # For models without predict_proba
# Calculate performance metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred_proba) if hasattr(model, "predict_proba") else None
# Log metrics
mlflow.log_metric("accuracy", accuracy)
mlflow.log_metric("precision", precision)
mlflow.log_metric("recall", recall)
mlflow.log_metric("f1_score", f1)
if roc_auc is not None:
mlflow.log_metric("roc_auc", roc_auc)
# Print metrics
print(f"Model: {model.__class__.__name__}")
print(f"Accuracy: {accuracy:.4f}")
print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
if roc_auc is not None:
print(f"ROC AUC: {roc_auc:.4f}")
# Log the model with an input example to infer the signature
input_example = X_test[:1] # Example input for the model
mlflow.sklearn.log_model(model, model.__class__.__name__, input_example=input_example)
return accuracy, precision, recall, f1, roc_auc
# Example Usage with Decision Tree Classifier
if __name__ == "__main__":
# Sample data loading (replace with your actual data)
# For demonstration, using a synthetic dataset
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
# Split the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Feature scaling (if necessary)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Initialize the Decision Tree model
dt_model = DecisionTreeClassifier(random_state=42)
# Train and validate the Decision Tree model
result_dt = train_and_validate_model(X_train, X_test, y_train, y_test, dt_model)
# Optionally, compare with other models
# Initialize other models
log_reg = LogisticRegression(random_state=42, max_iter=1000)
knn = KNeighborsClassifier(n_neighbors=5)
# Train and validate Logistic Regression
result_log_reg = train_and_validate_model(X_train, X_test, y_train, y_test, log_reg)
# Train and validate KNN
result_knn = train_and_validate_model(X_train, X_test, y_train, y_test, knn)
# Print comparison
print("\nComparison of Models:")
print(f"Logistic Regression: Accuracy={result_log_reg[0]:.4f}, Precision={result_log_reg[1]:.4f}, Recall={result_log_reg[2]:.4f}, F1 Score={result_log_reg[3]:.4f}, ROC AUC={result_log_reg[4]:.4f}")
print(f"KNN: Accuracy={result_knn[0]:.4f}, Precision={result_knn[1]:.4f}, Recall={result_knn[2]:.4f}, F1 Score={result_knn[3]:.4f}, ROC AUC={result_knn[4]:.4f}")
print(f"Decision Tree: Accuracy={result_dt[0]:.4f}, Precision={result_dt[1]:.4f}, Recall={result_dt[2]:.4f}, F1 Score={result_dt[3]:.4f}, ROC AUC={result_dt[4] if result_dt[4] is not None else 'N/A'}")
Explanation of the Refactored Code
Function train_and_validate_model:
Purpose: This function trains any scikit-learn model, evaluates its performance on the test set, logs the parameters and metrics to MLflow, and returns the performance metrics.
Parameters:
X_train, X_test: Features for training and testing.
y_train, y_test: Labels for training and testing.
model: An instance of a scikit-learn estimator (e.g., DecisionTreeClassifier).
Functionality:
Starts an MLflow run.
Logs the model's class name as a parameter.
Trains the model using the training data.
Makes predictions on the test set.
Calculates performance metrics: Accuracy, Precision, Recall, F1 Score, and ROC AUC (if applicable).
Logs these metrics to MLflow.
Prints the metrics for quick reference.
Logs the trained model to MLflow with an input_example to infer the model signature, addressing the earlier warning.
Returns the calculated metrics.
Example Usage:
Data Loading: Uses the Breast Cancer dataset from scikit-learn for demonstration purposes. Replace this with your actual dataset.
Data Splitting: Splits the dataset into training and testing sets with stratification to maintain class balance.
Feature Scaling: Applies StandardScaler to normalize the feature values, which is particularly useful for models like KNN.
Model Initialization and Training:
Initializes the DecisionTreeClassifier with a fixed random_state for reproducibility.
Trains and validates the Decision Tree model using the train_and_validate_model function.
Optionally, trains and validates other models like Logistic Regression and KNN for comparison.
Comparison: Prints a comparison of the performance metrics across different models.
Addressing the MLflow UI Warning
You previously encountered the following warning:
WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.
In the refactored code, this warning is addressed by providing an input_example when logging the model:
input_example = X_test[:1] # Example input for the model
mlflow.sklearn.log_model(model, model.__class__.__name__, input_example=input_example)
Providing an input_example helps MLflow automatically infer the model's signature, enhancing model management and deployment capabilities.
Benefits of This Refactored Approach
Code Reusability: The train_and_validate_model function is generic and can handle any scikit-learn model, promoting the DRY principle.
Modularity: Models can be easily swapped, added, or removed without altering the core training and evaluation logic.
MLflow Integration: Consistent logging of parameters, metrics, and models facilitates easy tracking and comparison of different experiments.
Clean and Readable Code: Well-structured with clear naming conventions and minimal comments for better maintainability.
Extensibility: Easily extend the function to include more metrics or models as needed.
Sample Output
When you run the refactored code, you should receive an output similar to the following: