![]() |
The score( ) method and accuracy_score( ) function are both essential tools in evaluating machine learning models, especially in supervised learning tasks. While they both assess model performance in terms of accuracy, they differ in terms of usage, flexibility, and application. Understanding these differences is crucial for effectively evaluating and comparing machine learning models. Score Vs Accuracy Score
Implementation: score() method vs accuracy_score() function in pythonBelow is an example of implementation demonstrating the difference between two evaluation methods using a simple logistic regression model in scikit-learn – Step 1: Importing the necessary librariesWe will import necessary modules for logistic regression, accuracy evaluation, iris dataset loading, and train-test splitting, facilitating the development and assessment of a logistic regression model for classification tasks.
Step 2 : Loading the iris datasetOnce ,we have loaded the necessary libraries, we will be loading Iris Dataset into variables “X” and “y” representing the feature matrix and target labels, respectively.
Step 3 : Splitting the dataset into training and testing setsThis code splits the dataset into training and testing sets, with 80% of the data reserved for training (X_train, y_train) and 20% for testing (X_test, y_test), ensuring consistency in random splitting with a specified seed value (random_state=42).
Step 4 : Create and Train a logistic Regression model and train on training datasetWe create a logistic regression model instance and trains it on the training data (X_train, y_train) to learn the relationships between the features and target labels.
Step 5 : Model Evaluationscore() methodThen, we evaluate the trained logistic regression model’s performance on the test data (
Output : Score method - Accuracy: 1.0 accuracy_score()The provided code generates predictions (y_pred) from the trained logistic regression model on the test data (X_test). Then, it computes the accuracy of the model’s predictions compared to the actual target labels (y_test) using the accuracy_score function, which measures the proportion of correctly predicted labels. Finally, it prints out the accuracy score obtained from the accuracy_score function.
Output : accuracy_score function - Accuracy: 1.0 In this implementation, we train a logistic regression model on the Iris dataset and then evaluate its accuracy using both the score() method and accuracy_score() function. The output will demonstrate the same accuracy value obtained using both approaches, showcasing their equivalence in evaluating model accuracy. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |