![]() |
VOOZH | about |
Machine learning is used in solving real-world problems including medical diagnostics. One such application is classifying cancer cells based on their features and determining whether they are 'malignant' or 'benign'. In this article, we will use Scikit-learn to build a classifier for cancer cell detection.
The Breast Cancer Wisconsin (Diagnostic) dataset consists of:
We will use these features to train and evaluate our machine learning model.
Below is the step-by-step implementation:
We will use numpy, matplotlib and scikit learn for this.
For this project, we will use the Breast Cancer Wisconsin (Diagnostic) dataset which is available in Scikit-learnβs datasets module. We use the load_breast_cancer() function to load the dataset.
Before training the model let's examine the dataset. This helps us understand how the data is structured and labeled. We will use pandas module to create a dataframe to simplify this process. We will use df.sample() function to fetch some random records from the data.
Output:
To explore the data types of the columns in our dataset we will use the df.info() function. It will help us to understand the categorical and numerical columns in our dataset.
Output:
To investigate the numerical columns we will use the df.describe() function. This function provides key summary statistics such as the mean, standard deviation, minimum and maximum values for each numerical column. It helps us understand the distribution and scale of the data which is crucial for preprocessing and model performance.
Output:
We must also analyze data.target to understand the distribution of malignant and benign cases as class imbalance can affect model performance.
Output:
Plotting an pie chart will help us understand the distribution of the target values.
Output:
Usually this type of dataset is considered imbalanced. A common threshold is when the minority class constitutes less than 30% of the total samples. However in this case its almost 38% which is acceptable. Incases of imbalances we can use techniques like oversampling, undersampling or class weighting.
To evaluate our classifier we split the dataset into training and test sets using train_test_split(). Here 33% of the data is used for testing while the remaining 67% is used for training.
We use Naive Bayes algorithm which is effective for binary classification tasks. The fit() function trains the model on the training dataset.
Output:
Now we use our trained model to predict the classification of cancer cells in the test set. The output is an array of 0s and 1s representing predicted tumor classifications.
Output:
[1, 0, 0, 1, 1, 0, 0, 0, 1, 1]
To measure how well our model performs we will compare its predictions with the actual labels to calculate its accuracy. We will use accuracy_score from the sklearn.metrics library.
Output:
Model Accuracy: 94.15%
This means our Naive Bayes classifier is 94.15% accurate in predicting whether a tumor is malignant or benign meaning our model is working fine and can be used for medical diagnostics.