Ir para o conteúdo

Lição 17 · Machine Learning

Regression, Clustering, and Reusing Models in KNIME

Build linear regression and k-means clustering workflows in KNIME, read regression errors, and save, reload, and deploy a model with its preparation steps.

Not every question is a yes or no. Sometimes you predict a number, and sometimes you have no target at all and want to find natural groups. This lesson covers both, then shows how to reuse a trained model without breaking it.

Regression

Regression predicts a number, such as delivery time or monthly spend. KNIME's Linear Regression Learner fits a linear model: you choose the target and the input columns, and it outputs the model and a table of coefficients and statistics. A Regression Predictor applies it.

A coefficient tells how the prediction changes when one input rises by one unit, with the others held fixed. Be careful: it describes the model, not necessarily a cause. Highly related inputs can make individual coefficients unstable.

Measuring regression errors

A numeric scoring node compares true and predicted values. Common measures are:

Measure Meaning
Mean absolute error Average size of the miss, in target units
Root mean squared error Like the average miss but punishes large errors more
R squared Share of variation explained compared with predicting the mean
Python Stand-in: regression error measures

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.

The one large miss of 10 pushes RMSE well above MAE. Also plot predicted against actual values with a scatter plot: a pattern in the errors, such as larger misses for high values, tells you more than one score.

Clustering with k-Means

Clustering groups similar rows without a target. KNIME's k-Means node asks for the number of clusters, k, and the numeric columns. It repeatedly assigns rows to the nearest center and moves the centers. KNIME 5.12 introduced a new k-Means node and marked the previous one as deprecated, so settings and outputs may differ from older tutorials. Read the node description in your version.

k-Means steps

Pick centers k starting points
Assign Nearest center
Move Center of its rows
Repeat Until stable
1 / 2

Three cautions:

  1. Normalize first. Distance-based methods are dominated by large-scale columns.
  2. Choose k with judgment. Try several values and check whether clusters are useful and stable.
  3. Clusters are not facts. The algorithm always returns groups, even in random data. Give them names only after inspecting their typical values.
Python Stand-in: tiny one-dimensional k-means

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.

The single value 200 pulls the second center up. Outliers can change clusters a lot, so inspect them.

Reusing a model

A trained model is a file you can keep. A Model Writer node saves a model and a Model Reader loads it, then a predictor applies it to new data. Some model types also support PMML, an XML exchange format, with its own writer and reader nodes.

The model alone is not enough. New data must go through exactly the same preparation: the same column names, the same normalization values, the same fills, and the same categories. Save the preparation models too.

KNIME's Integrated Deployment feature helps here. You mark a part of your workflow with Capture Workflow Start and Capture Workflow End nodes. KNIME captures that segment as a reusable workflow object, which you can write out with a Workflow Writer or run with a Workflow Executor. This reduces the risk of rebuilding the preparation by hand and getting it slightly wrong.

Monitor after deployment

Data changes over time, which is often called drift. Prices rise, a new product appears, or a form changes. Record predictions and, when the true outcomes arrive, compare them. Define when you will retrain, and keep the old model until the new one proves better.

Load model files only from sources you trust, and record which data and workflow version produced each one. Without that record, nobody can explain a prediction later.

Residuals tell the story

A residual is the actual value minus the predicted value. Plot residuals against the prediction. In a healthy result, the points scatter around zero without an obvious shape. A curve suggests that a straight-line model misses a relationship. A funnel shape, wider at one end, suggests that errors grow with the size of the value, which matters if large values are the ones you care about. Sort rows by the absolute residual and inspect the worst ones: they often reveal data errors or a missing input.

Interpreting clusters

After clustering, join the cluster label back to the original rows if needed and use GroupBy to compare typical values per cluster, along with the number of rows in each. A cluster with three rows is probably not a segment you can act on. Compare the results from different values of k and from different preparations. If rows keep jumping between clusters, the structure is weak, and you should say so.

Do not name clusters with judgments about people, such as "bad customers". Describe the measured features instead, such as "low spend, many support contacts", and avoid using clusters to treat people unfairly.

A scoring workflow checklist

Step Check
Read new data Same column names and types
Apply preparation Saved values, not new ones
Predict Model version recorded
Output Prediction, identifier, and run date
Review Compare with outcomes later

A baseline for regression

Regression needs a baseline too. The simplest one predicts the average of the training target for every row. For time-based data, another good baseline predicts the previous value. Compute the same error measures for the baseline and your model. If the model barely improves on the baseline, the inputs may not carry enough information, and a more complicated algorithm is unlikely to rescue it. Better data usually helps more than a cleverer model.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which measure punishes large errors more strongly?

    Squaring makes large misses weigh more.
  2. 2. Why normalize before k-Means?

    k-Means relies on distance, so scale matters.
  3. 3. What must accompany a saved model when it scores new data?

    Different preparation changes what the model receives.
  4. 4. What can capturing a workflow segment help with?

    Integrated Deployment captures the production steps as a unit.

Summary

  • Linear regression predicts numbers; check errors in target units and plot residual patterns.
  • k-Means finds groups, so normalize, test several k, and interpret cautiously.
  • Save models together with their preparation.
  • Integrated Deployment captures workflow segments for reuse.
  • Monitor for drift and keep track of model versions.

Next: Flow Variables

Seu progresso fica salvo neste navegador. Não precisa de conta.