Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Clustering and Discovery lessons
A different question · ML-U02 · 12–18 MIN

Distance depends on scale and context

Fit a meaningful distance representation for the exploratory population.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferCompute and inspect the evidence

Understand the idea

In discovery, fitting a scale on the declared population is part of describing that population. If PCA or clustering features later enter a supervised workflow, fit those transformations inside its training folds.

Illustrative data

Click the highlighted code to change the result.

scaler = StandardScaler().fit(example_X)
answer = 
Before
length (km)time (min)
102
204
306
After transform · unitless coordinates
length ztime z
-1.225-1.225
00
1.2251.225

Subtract the fitting mean, then divide by its SD. Means: 20 km and 4 min. SDs: 8.165 km and 1.633 min.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CLUSTER36 · first 8 rows
length_mmwidth_cm
1106.656.36006
1262.6613.292
317.1385.44237
1044.748.89315
994.127.01435
1307.7912.7223
1023.1113.9453
1163.636.99248
Your task
  1. Standardise CLUSTER36’s unequal-unit measurements.
answer
Hints

Think: A large numerical unit can dominate distances without being substantively more important.

Tools: StandardScaler.fit_transform.

Approach: Learn a standardised representation of the declared discovery population.

Explained solution
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
answer=scaler.fit_transform(df)

For this descriptive task the supplied population defines the scale; later supervised use would require a different fitting boundary.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.