Skip to main content

How to Load the Diabetes Dataset in Python with scikit-learn

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

The diabetes dataset is scikit-learn's small built-in dataset for regression, predicting a number rather than a class. Each of its 442 rows is a patient with 10 measurements, and the target measures how the disease progressed one year later. This guide loads it with load_diabetes, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
from sklearn.datasets import load_diabetes

<dataset> = load_diabetes(as_frame=True)
X, y = load_diabetes(return_X_y=True, as_frame=True, scaled=<True or False>)
PartWhat it means
as_frame=TrueReturns pandas objects instead of NumPy arrays
scaled=True by default: the features come already centered and scaled. False gives the raw values
return_X_y=TrueReturns just the features and the target

The scikit-learn reference: "If True, the feature variables are mean centered and scaled by the standard deviation times the square root of n_samples. If False, raw data is returned for the feature variables."

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_diabetes_demo.py
from sklearn.datasets import load_diabetes

diabetes = load_diabetes(as_frame=True)

print(diabetes.frame.shape)
print(diabetes.feature_names)
print(diabetes.frame[["age", "bmi", "target"]].head(3))

The load_diabetes_demo.py script in a macOS-style code window: load_diabetes(as_frame=True), then print of the shape, the feature names, and the age, bmi and target columns of the first three rows.

Step 2: Run It​

Terminal
uv run python load_diabetes_demo.py
Output
(442, 11)
['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
age bmi target
0 0.038076 0.061696 151.0
1 -0.001882 -0.051474 75.0
2 0.085299 0.044451 141.0

The script's output in a terminal: shape (442, 11), the ten feature names age, sex, bmi, bp and s1 to s6, and the first three rows of age, bmi and target.

The ages aren't years: by default the features come centered and scaled. The target is in its own units; it ranged from 25.0 to 346.0.

Common Mistakes​

MistakeWhat happensDo this instead
Reading age as yearsThe default values are scaled, such as 0.038076Load with scaled=False for the raw values: the first three ages were then 59.0, 48.0 and 72.0
Treating it as a classification datasetThe target is a number to predict, not a classUse regression models, such as linear regression

Troubleshooting​

SymptomLikely causeFix
Every feature's mean is about 0That's the default scaling: the mean of age came out as -0.0 and its sum of squares as 1.0Pass scaled=False if you want to scale it yourself

FAQ​

What do s1 to s6 mean?​

They're blood measurements. The toy dataset description lists them as total serum cholesterol, LDL, HDL, total cholesterol / HDL, "possibly log of serum triglycerides level" and blood sugar level.

How do I get X and y?​

X, y = load_diabetes(return_X_y=True, as_frame=True) gives the 10 features as a DataFrame and the target as a Series.

Conclusion​

load_diabetes(as_frame=True) loaded 442 patients with 10 scaled features and a number to predict. Pass scaled=False when you need the original values.

Additional Resources​