Skip to main content

How to Load the Breast Cancer Dataset in Python with scikit-learn

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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)
PartWhat it means
as_frame=TrueReturns pandas objects instead of NumPy arrays
<dataset>.target_namesThe class names, in the order of their codes 0 and 1
return_X_y=TrueReturns 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​

load_breast_cancer_demo.py
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())

The load_breast_cancer_demo.py script in a macOS-style code window: load_breast_cancer(as_frame=True), then print of the shape, the target names, the count of each target value and the first three feature names.

Step 2: Run It​

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

The script's output in a terminal: shape (569, 31), the classes malignant and benign, the counts 212 and 357, and the first three feature names.

  • 569 rows, 31 columns: 30 measurements and the target.
  • 0 is malignant and 1 benign, in the order of target_names: 212 malignant, 357 benign.

Common Mistakes​

MistakeWhat happensDo this instead
Assuming 1 means cancerHere 1 is benign: printing each code with its name gave 0 malignant and 1 benignCheck target_names before you read predictions or choose a positive class
Ignoring the class balanceThe classes are 212 and 357, not equalKeep it in mind when you split and when you pick a metric

Troubleshooting​

SymptomLikely causeFix
cancer.frame is NoneLoaded without as_frame=True: load_breast_cancer().frame printed NoneLoad 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​