![]() |
In scikit learn, we can demonstrate multi-metric evaluation with the help of two functions cross_val_score and GridSearchCV. They help you check the performance of the model based on multiple metrics with a single click rather than writing repetitive code. In this article, we will first discuss the implementation of cross_val_score and then GridSearchCV. Then finally, we will see how they can work together. What is cross_val_score?cross_val_score is a function in Scikit Learn that will help you to perform cross-validation scoring of an estimator. Generally, cross-validation helps us to understand how well a model has generalized to an independent dataset. You need to provide the following parameters as an input:
An estimator is a machine learning model on which you train your dataset. Input features are the independent variables and target value is a dependent variable that we have to determine. There are other optional parameters like cv, scoring, n_jobs which you can check in scikit learn documentation. When we pass all these parameters to the function, it will perform k-fold cross-validation. Here, your dataset is split into k subsets (folds), and the model is trained and evaluated k times. Each time a different fold is chosen as test set and remaining are chosen as train set. As a result, you get an array of k values, where each value determines how the model performed on that fold based on the scoring metric. What is GridSearch CV?GridSearch CV function of scikit learn library allows you to perform an exhaustive search over a specified parameter grid. And as a result you get the best hyperparameters for your model. This function will help you to combine cross-validation with grid search algorithm. Therefore, you can easily evaluate a model’s performance by different combinations of hyperparameter values. Implementation of multi-metric evaluation on cross_val_score and GridSearchCVImport LibrariesWe have imported numpy, matplotlib, sklearn. Python3
Loading DatasetLoading iris dataset. Python3
Setting Evaluation MetricsSetting evaluation metrics to AUC and accuracy score. Python3
Setting Classifier and Grid SearchWe have created a Random Forest Classifier and set up GridSearchCV to search for the optimal value of the “min_samples_split” hyperparameter. This is a common approach to tune hyperparameters and find the best combination for your model. However, I notice that you’ve used the variable Python3
Visualization
Python3
Output: This visualization provides a comprehensive view of the model performance across different values of the “min_samples_split” hyperparameter for multiple scoring metrics. It helps in identifying the optimal hyperparameter value based on different evaluation criteria. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |