Understand the idea
A scaler learns reference values from a table, then uses them to rescale measurements. Each column is scaled separately.
A small example
For [10, 20, 30], MinMaxScaler returns [0, 0.5, 1]. StandardScaler puts the mean at 0 and measures distances in standard deviations.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit_transform(df[["price", "rating"]])What each part does
from sklearn.preprocessing import StandardScaler- Import the standardisation tool.
scaler = StandardScaler()- Create that tool and store it as scaler.
df[["price", "rating"]]- Pass a two-column table in the requested order.
scaler.fit_transform(...)- Learn each column’s mean and spread from this table, then return the scaled numbers.
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 1.2 | 4.1 | A |
| Choco Pop | chocolate | 2.1 | 4.6 | B |
| Mint Bite | mint | 1.5 | 3.8 | A |
| Berry Loop | fruity | 2.8 | 4.4 | B |
| Cocoa Cube | chocolate | 3.4 | 4.9 | A |
| Lemon Drop | fruity | 1.8 | 4 | B |
Your task · Follow
- Using df, standardise price then rating with StandardScaler.
- Display a numeric array with those two columns in that order.
- Use fit_transform().
Hint
The scaler needs a two-dimensional selection, even for a single measurement.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit_transform(df[["price", "rating"]])