跳到正文

第 15 课 · Machine Learning

Machine Learning in KNIME: Learner, Predictor, Split

Understand the learner and predictor pattern in KNIME, split data with Table Partitioner, normalize safely, and avoid data leakage that inflates model scores.

Machine learning means building a model from examples, then using it on new cases. KNIME makes the steps visible, which is a good way to learn the logic. This lesson covers the pattern that every supervised model follows and the mistakes that make results look better than they are.

The vocabulary

  • Features are the input columns, such as tenure and monthly fee.
  • The target is the column you want to predict, such as whether a customer left.
  • Classification predicts a category. Regression predicts a number.
  • Training is when the model learns from examples with known targets.

The learner and predictor pattern

KNIME usually uses two nodes per algorithm: a learner that builds a model from training data, and a predictor that applies the model to other data. The learner produces a model port, which connects to the predictor.

flowchart LR
  A["Prepared data"] --> B["Table Partitioner"]
  B -->|training rows| C["Learner"]
  B -->|test rows| D["Predictor"]
  C -->|model| D
  D --> E["Scorer"]

The model is learned from training rows only, and judged on test rows it has not seen.

Splitting the data

The Table Partitioner node divides a table into two parts, for example 80% for training and 20% for testing. Useful settings are:

  • Relative size or an absolute number of rows.
  • Random sampling, usually with a fixed random seed so that reruns give the same split.
  • Stratified sampling on the target, which keeps the share of each class similar in both parts. This matters when one class is rare.

For time-based data, such as predicting next month from past months, a random split lets the model peek at the future. Sort by date and split by time instead.

Python Stand-in: a seeded, stratified 80/20 split

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both parts keep the same share of leavers.

Data leakage

Leakage happens when information that would not be available at prediction time sneaks into training. The scores look excellent and the model fails in real use. Common forms:

  1. A feature that is recorded after the outcome, such as "cancellation fee charged" when predicting cancellation.
  2. Computing a fill value or scale from all data, including the test set.
  3. The same customer appearing in both training and test parts.

Ask of every column: would I know this at the moment I make the prediction?

Normalizing safely

Some methods, especially distance-based ones, are sensitive to scale: a salary in the thousands can dominate age. The Normalizer node rescales numeric columns. The safe order is:

  1. Split the data.
  2. Fit the normalizer on the training part.
  3. Apply the same transformation to the test part with the Normalizer (Apply) node, using the model output from the first.

Do the same with missing-value filling and other learned preparation.

Python Stand-in: fit scaling on training data only

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A test value above the training maximum scales above 1. That is correct and honest: the test data is allowed to be different.

Cross-validation

A single split can be lucky. Cross-validation splits data into several folds, trains on all but one, tests on the remaining one, and rotates. KNIME offers loop nodes for this, usually called X-Partitioner and X-Aggregator. Look at both the average score and how much it varies between folds.

Baselines first

Before any model, compute a baseline, such as always predicting the most common class. If 90% of customers stay, that rule is 90% accurate while catching nobody who leaves. Your model must beat the baseline in a way that matters.

Models that affect people, for example credit, hiring, or insurance, need care about fairness, privacy, and legal rules. A model can learn unfair patterns from historical data. Involve the right experts and keep a human responsible for decisions.

Preparing the table

Learners need a clean, consistent table. Before training, check these items:

  1. The target has the expected categories, with no variants such as Yes and yes treated as two classes.
  2. Identifiers such as customer numbers are removed from the features, since a model can memorize them.
  3. Categorical columns have a manageable number of distinct values.
  4. Missing values are handled in a way the algorithm accepts, and learned fills come from training rows.
  5. Data types match what the learner expects, with numbers stored as numbers.

Read each learner's description, because some accept text categories directly while others need numeric inputs. When you must convert categories into numbers, remember that arbitrary codes can imply an order that does not exist, such as treating red as smaller than blue. Encoding each category as its own yes or no column avoids that for methods that depend on numbers.

Reproducibility

If you rerun the workflow tomorrow, you should get the same result, or know why not. Fix random seeds in the split and in algorithms that use randomness. Save the exact input file or record how it was extracted, with the date. Write the KNIME version and the important settings in an annotation. Without this record, you cannot tell whether a changed score comes from new data, a changed setting, or chance.

Practice task

Take any small public or synthetic dataset with a two-class target. Split it with and without stratification using a few different seeds, and watch how the share of the rare class changes in the test part. This shows why a single lucky split should not decide anything.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. In the usual KNIME pattern, what does a predictor node do?

    The learner builds the model and the predictor applies it.
  2. 2. How should you normalize for a train and test split?

    Learning anything from test data leaks information.
  3. 3. Why use stratified sampling when one class is rare?

    Without it, a rare class might be badly represented in one part.
  4. 4. Which is an example of leakage?

    Such a feature would not exist at prediction time.

Summary

  • Learner nodes build models and predictor nodes apply them.
  • Use Table Partitioner with a seed and stratification where useful.
  • Fit preparation steps on training data only.
  • Watch for leakage and compare with a baseline.
  • Cross-validation shows how stable results are.

Next: Classification and Model Evaluation

你的学习进度保存在此浏览器中,无需账号。