top of page

Breast Cancer Classification with Naive Bayes and Scikit-Learn

Introduction:

Machine learning serves as a crucial ally in the realm of healthcare, aiding in the detection and classification of diseases. In this blog post, we'll embark on an exploration of a Python code snippet that harnesses the power of Naive Bayes, a probabilistic algorithm, to classify breast cancer data. Our journey will take us through scikit-learn, a popular machine learning library in Python.


Libraries Used:

The code utilizes various modules from scikit-learn, with a particular focus on the Gaussian Naive Bayes classifier.

1. scikit-learn: A versatile machine learning library, scikit-learn offers an array of tools for data analysis and model building.

2. Naive Bayes Classifier: Naive Bayes is a probabilistic algorithm based on Bayes' theorem, assuming independence between features.

3. Breast Cancer Dataset: The dataset used in this code pertains to breast cancer and is accessible through scikit-learn. It is commonly employed for binary classification tasks.


Code Explanation:


# Import necessary modules
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import GaussianNB
# Load the breast cancer dataset
bc = load_breast_cancer()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Initialize a Gaussian Naive Bayes classifier
clf = GaussianNB()
# Train the classifier on the training data
clf.fit(X_train, y_train)
# Make predictions on the test data
y_pred = clf.predict(X_test)
# Print the accuracy score of the classifier
print(accuracy_score(y_test, y_pred))

Explanation:

1. Loading the Dataset: Our journey begins with the loading of the breast cancer dataset using the `load_breast_cancer` function from scikit-learn. This dataset contains features related to breast cancer tumors, and the task is to predict whether a tumor is malignant or benign.

2. Data Splitting: The dataset is then divided into training and testing sets using the `train_test_split` function. This ensures the model is trained on a subset of the data and evaluated on a separate, unseen subset.

3. Naive Bayes Classifier Initialization: An instance of the Gaussian Naive Bayes classifier is initialized using the `GaussianNB` class from scikit-learn.

4. Training the Classifier: The classifier is trained on the training data using the `fit` method.

5. Making Predictions: Predictions are made on the test data using the `predict` method.

6. Accuracy Calculation and Output: The accuracy score, representing the percentage of correctly predicted instances, is computed using the `accuracy_score` function from scikit-learn. The result is then printed to the console.


Conclusion:

In this exploration, we've navigated through a concise yet powerful machine learning code snippet that employs the Naive Bayes classifier to classify breast cancer data. The Gaussian Naive Bayes algorithm, based on probabilistic principles, is just one of the many tools in scikit-learn's extensive arsenal. Experimenting with diverse algorithms and datasets not only broadens your understanding of machine learning concepts but also equips you with the skills to tackle real-world challenges in the realm of healthcare and beyond.


The link to the github repo is here.

22 views

Related Posts

How to Install and Run Ollama on macOS

Ollama is a powerful tool that allows you to run large language models locally on your Mac. This guide will walk you through the steps to...

bottom of page