Preparing data
- Machine Learning course
- Beginner
- Lesson 71/112
- 7 min
Prepare data for classification.
Project: wine classification
In this project you'll predict which of three producers a wine comes from using chemical measurements. The load_wine dataset in scikit-learn has 178 examples and 13 features:
from sklearn.datasets import load_wine
d = load_wine()
print(d.data.shape) # (178, 13)
print(d.feature_names[:3]) # ['alcohol', 'malic_acid', 'ash']
print(d.target_names) # ['class_0' 'class_1' 'class_2']The first step is always getting to know the data: dimensions, class distribution and feature ranges.
Class distribution and ranges
np.bincount counts how many examples each class has. The minimum and maximum of the features show whether scaling is needed:
print(np.bincount(y)) # [59 71 48]
print(X.min(axis=0)[:3])
print(X.max(axis=0)[-1]) # proline ~1680, others much smallerThe classes are roughly balanced but not equal; we'll use stratify=y in the split. Feature scales differ greatly; a distance-based model needs scaling.
Practice Stratified split
Split the data with test_size=0.25, random_state=42, stratify=y. Print the row counts of the training and test sets on one line, then the class distribution of the training set with np.bincount.
Starter code
import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
X, y = load_wine(return_X_y=True)
# buraya yazSample solution
import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
print(len(X_train), len(X_test))
print(np.bincount(y_train))Expected output
133 45 [44 53 36]
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many features does the dataset have?
- 3
- 14
- 13 Correct answer
- 178
Explanation: shape (178, 13): 178 examples, 13 features; 3 is the class count.
2 Count the examples of each class.
print(np.____(y))
print(np.bincount(y))
Explanation: bincount gives the frequency of integer labels starting from 0.
3 Match the facts.
| Term | Meaning |
|---|---|
178 |
Number of examples |
13 |
Number of features |
3 |
Number of classes |
stratify=y |
Keep class proportion |