Write a Machine Learning program to check Model Accuracy

Model accuracy is a fundamental metric in machine learning that measures how often a model makes correct predictions. Understanding accuracy helps evaluate whether your model is performing well enough for real-world applications.

What is a Machine Learning Model?

In machine learning, a model is a mathematical representation that learns patterns from data to make predictions or classifications. Common types include:

  • Linear Regression - for predicting continuous values
  • Decision Trees - for classification and regression
  • Neural Networks - for complex pattern recognition
  • Support Vector Machines - for classification tasks

The quality of a model depends on how well it performs on new, unseen data that wasn't used during training.

Understanding Model Accuracy

Accuracy is defined as the ratio of correctly predicted instances to the total number of instances in the dataset:

# Accuracy formula
accuracy = (correct_predictions / total_predictions) * 100

For example, if a model makes 90 correct predictions out of 100 test cases, its accuracy is 90%.

Python Program to Check Model Accuracy

Let's create a complete program that trains a model and evaluates its accuracy ?

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.datasets import make_classification

# Generate synthetic data for binary classification
num_samples = 1000
X, y = make_classification(n_samples=num_samples, n_features=4, 
                          n_informative=3, n_redundant=1, 
                          n_clusters_per_class=1, random_state=42)

# Split data into training and testing sets (70-30 split)
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.3, 
                                                    random_state=42)

# Create and train the logistic regression model
model = LogisticRegression(random_state=42)
model.fit(X_train, y_train)

# Make predictions on the test set
y_pred = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.4f}")
print(f"Accuracy Percentage: {accuracy * 100:.2f}%")

# Additional metrics for better evaluation
from sklearn.metrics import classification_report
print("\nDetailed Classification Report:")
print(classification_report(y_test, y_pred))
Model Accuracy: 0.8867
Accuracy Percentage: 88.67%

Detailed Classification Report:
              precision    recall  f1-score   support

           0       0.89      0.88      0.89       154
           1       0.88      0.89      0.89       146

    accuracy                           0.89       300
   macro avg       0.89      0.89      0.89       300
weighted avg       0.89      0.89      0.89       300

How the Program Works

  1. Data Generation: Creates synthetic classification data using make_classification()
  2. Data Splitting: Divides data into training (70%) and testing (30%) sets
  3. Model Training: Trains a logistic regression model on training data
  4. Prediction: Makes predictions on the test set
  5. Accuracy Calculation: Compares predictions with actual labels using accuracy_score()

Limitations of Accuracy

While accuracy is useful, it has important limitations:

Scenario Problem Better Metric
Imbalanced Classes High accuracy despite poor minority class prediction Precision, Recall, F1-score
Different Misclassification Costs Treats all errors equally Cost-sensitive metrics
Multi-class Problems Doesn't show per-class performance Confusion Matrix, Per-class metrics

Alternative Evaluation Metrics

from sklearn.metrics import precision_score, recall_score, f1_score

# Calculate additional metrics
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average='weighted')

print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1-Score: {f1:.4f}")
Precision: 0.8868
Recall: 0.8867
F1-Score: 0.8867

Conclusion

Model accuracy provides a quick assessment of overall performance, but should be used alongside other metrics like precision, recall, and F1-score for a complete evaluation. Always consider your specific problem context when choosing evaluation metrics.

Updated on: 2026-03-27T09:52:39+05:30

657 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements