Skip to main content

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

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

The wine dataset is a small classification dataset with more columns than iris: 178 wines, 13 chemical measurements each, and three classes of wine. It ships with scikit-learn and loads with load_wine. This guide loads it and counts the classes, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
from sklearn.datasets import load_wine

<dataset> = load_wine(as_frame=True)
X, y = load_wine(return_X_y=True, as_frame=True)
PartWhat it means
as_frame=TrueReturns pandas objects instead of NumPy arrays
<dataset>.frameOne DataFrame: 13 measurements and a target column
return_X_y=TrueReturns just the measurements and the class codes

The scikit-learn reference: "Load and return the wine dataset (classification)." And: "The wine dataset is a classic and very easy multi-class 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_wine_demo.py
from sklearn.datasets import load_wine

wine = load_wine(as_frame=True)

print(wine.frame.shape)
print(wine.target_names.tolist())
print(wine.frame["target"].value_counts().sort_index().to_dict())
print(wine.feature_names[:4])

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

Step 2: Run It​

Terminal
uv run python load_wine_demo.py
Output
(178, 14)
['class_0', 'class_1', 'class_2']
{0: 59, 1: 71, 2: 48}
['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash']

The script's output in a terminal: shape (178, 14), the classes class_0, class_1 and class_2, the counts 59, 71 and 48, and the first four feature names.

  • 178 rows, 14 columns: 13 measurements and the target.
  • Three classes, not equal in size: 59, 71 and 48 wines.

Common Mistakes​

MistakeWhat happensDo this instead
Training a distance- or gradient-based model on the raw columnsThe columns sit on very different scales: proline ran from 278.0 to 1680.0, hue from 0.48 to 1.71Scale them first, for example with StandardScaler
Splitting without keeping the class balanceA small test set's classes drift from the data's: a 10% split gave {0: 7, 1: 7, 2: 4}stratify=y gave {0: 6, 1: 7, 2: 5}, close to the 59 : 71 : 48 of the whole set

Troubleshooting​

SymptomLikely causeFix
wine.frame is NoneLoaded without as_frame=True, as with the other loadersLoad it again with as_frame=True

FAQ​

What do the classes mean?​

The toy dataset description: "The data is the results of a chemical analysis of wines grown in the same region in Italy by three different cultivators." The classes are those three, named only class_0, class_1 and class_2.

How do I get X and y?​

X, y = load_wine(return_X_y=True, as_frame=True): a DataFrame of the 13 measurements and a Series of class codes.

Conclusion​

load_wine(as_frame=True) loaded 178 wines with 13 measurements and three classes of 59, 71 and 48 wines. Scale the columns before models that care about scale.

Additional Resources​