Menu
CoddyTech

Wine Chemistry

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 63 of 64.

A wine importer tested 178 wines from three grape cultivars, 13 chemical readings each: load_wine. Before a model learns the cultivars, the lab hunts for instrument slips.

Box plots of three raw wine readings on one axis: magnesium spreads from about 70 to 162, while alcohol near 13 and hue near 1 are squashed into flat lines at the bottom

The readings live on very different rulers, so no single outlier rule fits them all. Standardized, every reading counts standard deviations from its mean.

On that ruler, more than 3 away, on either side, earns a second look. A box per cultivar then shows where each one typically sits.

challenge icon

Challenge

Hard

The program reads a feature's name.

  1. Store its z-scores in df["z"], using the pandas .std().
  2. Print how many wines lie beyond 3, either side, then each one in table order: index, reading and z.
  3. Print each cultivar's mean z.
  4. Draw one box of z-scores per cultivar, named with wine.target_names, the feature as title and z-score as y label.

Round to 2 decimals. For color_intensity, it prints:

Outliers in color_intensity: 1
Wine 158: 13.0 (z = 3.43)
class_0: 0.2
class_1: -0.85
class_2: 1.01

and draws:

Three box plots of color_intensity z-scores titled color_intensity: class_0 around 0.15, class_1 low around -0.9 with a few dots above it, class_2 high around 1.1 with a whisker reaching 3.43

Then Coddy describes your boxes.

Try it yourself

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine

feature = input()
wine = load_wine(as_frame=True)
df = wine.frame

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler