Skip to content

About

A Machine Learning project that classifies cancer cases as malignant or benign using Logistic Regression with data normalization and accuracy evaluation.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

🩺 Breast Cancer Classification using Machine Learning

A Machine Learning project containing two implementations of breast cancer classification using the same dataset: a Logistic Regression model in Python and a Jupyter Notebook comparing six different classification algorithms.

Overview

This repository contains two related implementations for classifying breast cancer cases as malignant or benign.

Both implementations use the same Breast Cancer Wisconsin Diagnostic dataset, but the dataset is loaded in two different ways.

The first implementation is a Python script that uses a downloaded cancer.csv file. It performs data preprocessing, feature normalization, and uses Logistic Regression for classification.

The second implementation is a Jupyter Notebook that loads the same dataset directly from Scikit-learn using load_breast_cancer(). It then compares six different Machine Learning classification algorithms.

The purpose of having both implementations in the same repository is to demonstrate different ways of working with the same dataset and comparing different Machine Learning approaches.

Projects

1. Logistic Regression

The first implementation is written in Python:

cancer_classification.py

This project uses the downloaded dataset:

cancer.csv

The main steps include:

  • Loading the dataset from a CSV file
  • Removing unnecessary columns
  • Converting diagnosis labels into numerical values
  • Separating features and target
  • Normalizing the input features
  • Splitting the dataset into training and testing sets
  • Training a Logistic Regression model
  • Predicting the diagnosis of test samples
  • Calculating classification accuracy

2. Machine Learning Model Comparison

The second implementation is provided as a Jupyter Notebook:

breast_cancer_classification.ipynb

Instead of reading a local CSV file, the notebook loads the dataset directly from Scikit-learn:

from sklearn import datasets

cancer = datasets.load_breast_cancer()

The notebook then trains and compares six different classification algorithms:

  • Support Vector Machine (SVM)
  • Gaussian Naive Bayes
  • Decision Tree
  • Random Forest
  • XGBoost
  • K-Nearest Neighbors (KNN)

Each model is evaluated using the same training and testing data.

Features

  • Breast cancer classification
  • Malignant and benign classification
  • Data preprocessing
  • Feature normalization
  • Train/test data splitting
  • Logistic Regression
  • Support Vector Machine
  • Gaussian Naive Bayes
  • Decision Tree
  • Random Forest
  • XGBoost
  • K-Nearest Neighbors
  • Accuracy evaluation
  • Machine Learning model comparison

Technologies Used

  • Python
  • Pandas
  • Scikit-learn
  • XGBoost
  • Jupyter Notebook

Dataset

Both projects use the same Breast Cancer Wisconsin Diagnostic dataset.

The dataset contains numerical features describing characteristics of breast cancer cell nuclei and is used to classify cases into two categories:

Malignant
Benign

Python Dataset

The Python implementation uses a downloaded copy of the dataset:

cancer.csv

The diagnosis column is converted into numerical labels:

M → 1
B → 0

The columns id and Unnamed: 32 are removed before training.

Jupyter Notebook Dataset

The notebook loads the dataset directly from Scikit-learn:

cancer = datasets.load_breast_cancer()

This avoids manually storing the dataset as a CSV file and provides the dataset directly through the Scikit-learn library.

Therefore, both implementations work with the same dataset, but the source of the data in the code is different.

Data Preprocessing

Python Implementation

The first implementation performs the following preprocessing steps:

Load cancer.csv
      ↓
Remove unnecessary columns
      ↓
Convert diagnosis labels
      ↓
Separate features and target
      ↓
Normalize features
      ↓
Train/Test Split

The features are normalized using Min-Max normalization:

x_data = (x_data - x_data.min()) / (x_data.max() - x_data.min())

Jupyter Notebook

The notebook loads the dataset directly from Scikit-learn and separates the features and target:

cancer.data
cancer.target

The dataset is then divided into training and testing sets using a 70/30 split.

Machine Learning Workflow

Python Project

Load Dataset
      ↓
Data Preprocessing
      ↓
Feature Normalization
      ↓
Train/Test Split
      ↓
Logistic Regression
      ↓
Prediction
      ↓
Accuracy Evaluation

Jupyter Notebook

Downloaded Dataset
      ↓
Data Preprocessing
      ↓
Scikit-learn Dataset
      ↓
Train/Test Split
      ↓
Train Multiple Classifiers
      ↓
Make Predictions
      ↓
Calculate Accuracy
      ↓
Compare Models

Models

Logistic Regression

The Python implementation uses Logistic Regression as its classification algorithm.

model = LogisticRegression()
model.fit(x_train, y_train)

The trained model is used to predict the diagnosis of the test samples.

Support Vector Machine

The notebook uses an SVM classifier with a linear kernel:

model_SVM = svm.SVC(kernel='linear')
model_SVM.fit(X_train, y_train)

Gaussian Naive Bayes

Gaussian Naive Bayes is used as another classification model:

model_GNB = GaussianNB()
model_GNB.fit(X_train, y_train)

Decision Tree

The notebook uses a Decision Tree classifier:

model_DT = DecisionTreeClassifier()
model_DT.fit(X_train, y_train)

Random Forest

Random Forest is used as an ensemble classification algorithm:

model_RF = RandomForestClassifier()
model_RF.fit(X_train, y_train)

XGBoost

XGBoost is included as another ensemble learning algorithm:

model_XGB = xg.XGBClassifier()
model_XGB.fit(X_train, y_train)

K-Nearest Neighbors

K-Nearest Neighbors is used as the final classifier:

model_KN = KNeighborsClassifier()
model_KN.fit(X_train, y_train)

Model Training

The Python project trains a Logistic Regression model using the locally loaded dataset.

The Jupyter Notebook trains six different classification models using the dataset loaded from Scikit-learn.

Each model generates predictions for the test dataset, and the results are evaluated using accuracy.

Evaluation

The projects use accuracy to evaluate classification performance.

Python Project

The Python script compares the predicted values with the actual test labels and calculates the percentage of correct predictions.

Jupyter Notebook

The notebook uses:

metrics.accuracy_score(y_test, y_pred)

to calculate the accuracy of each classifier.

Example Output

Python Project:

XX / XXX
XX.XX


Jupyter Notebook:

SVM Accuracy: XX.XX
Naive Bayes Accuracy: XX.XX
Decision Tree Accuracy: XX.XX
Random Forest Accuracy: XX.XX
XGBoost Accuracy: XX.XX
KNN Accuracy: XX.XX

The exact results may vary depending on the model configuration and train/test split.

Model Comparison

The two implementations demonstrate different approaches to the same classification problem.

Implementation Data Loading Models
Python Script Local cancer.csv Logistic Regression
Jupyter Notebook Scikit-learn load_breast_cancer() SVM, Naive Bayes, Decision Tree, Random Forest, XGBoost, KNN

The dataset itself is the same; only the way it is loaded and processed in the two implementations is different.

Project Structure

Breast-Cancer-Classification/
│
├── cancer.csv
├── cancer_classification.py
├── breast_cancer_classification.ipynb
├── requirements.txt
├── LICENSE
└── README.md

Installation

Install the required libraries using:

pip install pandas scikit-learn xgboost jupyter

You can also install all dependencies using:

pip install -r requirements.txt

How to Run

Python Project

Run the Python implementation:

python cancer_classification.py

The program will load cancer.csv, preprocess the data, train the Logistic Regression model, make predictions, and display the final accuracy.

Jupyter Notebook

Start Jupyter Notebook:

jupyter notebook

Then open:

breast_cancer_classification.ipynb

Run the notebook cells from top to bottom to train the six classification models and compare their accuracy.

Requirements

The main dependencies are:

pandas
scikit-learn
xgboost
jupyter

Future Improvements

  • Add confusion matrices
  • Add precision, recall, and F1-score
  • Add cross-validation
  • Apply feature scaling to the notebook models
  • Tune model hyperparameters
  • Add a model comparison visualization
  • Add feature importance analysis
  • Improve reproducibility with fixed random states
  • Add predictions for new data

Contributing

Contributions are welcome.

You can improve the preprocessing, add new classification algorithms, improve the evaluation process, or add new visualizations to the project.

License

This project is licensed under the MIT License.

Author

Mohammad Reza Bakhshandeh

Electrical Engineering (Electronics) Graduate

Interested in Industrial Automation, Embedded Systems, PLC Programming, Python Development, Computer Vision, Machine Learning, and Artificial Intelligence.

About

A Machine Learning project that classifies cancer cases as malignant or benign using Logistic Regression with data normalization and accuracy evaluation.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages