Prepare Data for a Machine Learning Model
Who it is for
What you will achieve
Prerequisites
Curriculum
- Step 1: Train-Test Split and Data Leakage: Split First, Learn After
Why the train-test split comes before everything else: an 80/20 split with stratify and random_state, a scaler that leaks test rows, and a column that secretly holds the answer. Shown on Rain in Australia.
- Step 2: Handling Missing Data: Count the Blanks, Then Fill Them with SimpleImputer
Count the blanks with isnull().sum(), see why dropna() would throw away 60% of the rows, and fill the gaps with SimpleImputer: median for numbers, most frequent for text, learned from the train rows only. Shown on Rain in Australia.
- Step 3: Encoding Categorical Data: OrdinalEncoder, OneHotEncoder and LabelEncoder
Models only understand numbers. Encode T-shirt sizes with OrdinalEncoder, colours with OneHotEncoder, avoid the dummy variable trap, handle a colour the encoder never saw, tame a date with 3,436 values, and encode the answer with LabelEncoder.
- Step 4: Feature Scaling: StandardScaler, MinMaxScaler and Which Models Care
Put features on the same scale so each one gets a fair say. StandardScaler and MinMaxScaler fitted on the train rows, the formula checked by hand, and a wine test where KNN jumps from 0.806 to 0.972 while a decision tree stays at 0.944.
- Step 5: Pipelines and ColumnTransformer: All Preprocessing and the Model in One Object
Pack imputing, encoding, scaling and the model into one scikit-learn Pipeline with two ColumnTransformer lanes. One fit on the train rows, 21 columns in, 115 out, 0.849 test accuracy, and a raw row with a blank predicted directly.
- Step 6: Feature Selection and the Curse of Dimensionality: Keep the Columns That Matter
More columns can hurt. See the curse of dimensionality in numbers (farthest over nearest falls from 46.78 to 1.10), then keep the best columns with SelectKBest inside a pipeline: 5 of 115 columns score 0.83 against 0.849 for all.
- Step 7: Project: Preprocess Rain in Australia from Raw CSV to Model-Ready Data
A hands-on project: take the raw weatherAUS.csv through a health check, a new Month column, leak removal, a stratified split and a two-lane ColumnTransformer. 22 columns become 127 numeric inputs with zero blanks, and a 0.776 baseline to beat.