How to Load the Breast Cancer Dataset in Python with scikit-learn
The breast cancer Wisconsin dataset is scikit-learn's built-in dataset for binary classification: 569 samples, 30 measurements each, and two classes, malignant or benign. It loads with load_breast_cancer, no download needed. This guide loads it and checks what the two codes mean, with real output from scikit-learn 1.9.1.
Syntax
from sklearn.datasets import load_breast_cancer
<dataset> = load_breast_cancer(as_frame=True)
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
| Part | What it means |
|---|---|
as_frame=True | Returns pandas objects instead of NumPy arrays |
<dataset>.target_names | The class names, in the order of their codes 0 and 1 |
return_X_y=True | Returns just the 30 features and the class codes |
The scikit-learn reference: "Load and return the breast cancer Wisconsin dataset (classification). The breast cancer dataset is a classic and very easy binary classification dataset."
Prerequisites
- scikit-learn and pandas in your project, for example with
uv add scikit-learn pandas. This guide uses scikit-learn 1.9.1 and pandas 3.1.1 on Python 3.15.2.
Step 1: Write the Script
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer(as_frame=True)
print(cancer.frame.shape)
print(cancer.target_names.tolist())
print(cancer.frame["target"].value_counts().sort_index().to_dict())
print(cancer.feature_names[:3].tolist())

Step 2: Run It
uv run python load_breast_cancer_demo.py
(569, 31)
['malignant', 'benign']
{0: 212, 1: 357}
['mean radius', 'mean texture', 'mean perimeter']

- 569 rows, 31 columns: 30 measurements and the
target. 0is malignant and1benign, in the order oftarget_names: 212 malignant, 357 benign.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Assuming 1 means cancer | Here 1 is benign: printing each code with its name gave 0 malignant and 1 benign | Check target_names before you read predictions or choose a positive class |
| Ignoring the class balance | The classes are 212 and 357, not equal | Keep it in mind when you split and when you pick a metric |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cancer.frame is None | Loaded without as_frame=True: load_breast_cancer().frame printed None | Load it again with as_frame=True |
FAQ
What are the 30 features?
Ten measurements of cell nuclei, each as a mean, a standard error and a "worst" value, such as mean radius. The toy dataset description lists them.
How do I get X and y?
X, y = load_breast_cancer(return_X_y=True, as_frame=True) gives the 30 features as a DataFrame and the codes as a Series.
Conclusion
load_breast_cancer(as_frame=True) loaded 569 samples with 30 features, 212 malignant (0) and 357 benign (1). Check target_names so you read the codes the right way round.
Additional Resources
- load_breast_cancer — the function.
- Toy datasets — the dataset's description and its features.
