Classification
Anonymous contributor
Published Mar 1, 2025
Contribute to Docs
Classification is a supervised technique in machine learning used to categorize data into predefined classes or labels. It involves training a model using labeled data and then using the model to predict labels for new data. Common applications include spam detection, sentiment analysis, and medical diagnosis.
Classification Process
The general process for performing classification involves the following steps:
1. Import necessary libraries
2. Load and preprocess the dataset
3. Split the dataset into training and testing sets
4. Initialize the classifier (e.g., Logistic Regression, Decision Tree, SVM).
5. Fit the model on the training set
6. Make predictions on the test set
7. Evaluate the model using metrics such as accuracy, precision, recall, and F1-score
Example
Python provides several libraries for performing classification, such as Scikit-learn.
Here is an example that demonstrates how to perform classification using Logistic Regression in Scikit-learn:
from sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import accuracy_score# Load the Iris datasetiris = load_iris()X = iris.datay = iris.target# Split the dataset into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)# Initialize the classifier (Logistic Regression)model = LogisticRegression()# Fit the model on the training setmodel.fit(X_train, y_train)# Make predictions on the test sety_pred = model.predict(X_test)# Evaluate the modelaccuracy = accuracy_score(y_test, y_pred)print(f"Accuracy: {accuracy:.2f}")
The above code produces the following output:
Accuracy: 1.00
Codebyte Example
The following codebyte example demonstrates how to perform classification using Decision Tree in Scikit-learn:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn AI on Codecademy
- Skill path
Intermediate Machine Learning
Level up your machine learning skills with tuning methods, advanced models, and dimensionality reduction.Includes 5 CoursesWith CertificateIntermediate8 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours