Saltar al contenido

Lección 16 · Machine Learning

Classification and Model Evaluation in KNIME

Train decision tree and random forest classifiers in KNIME, read the confusion matrix from the Scorer node, use the ROC Curve, and choose useful metrics.

A classifier answers "which group?": will this customer leave, is this transaction suspicious, which category is this ticket? This lesson builds classifiers in KNIME and, more importantly, shows how to judge them.

A decision tree asks a series of questions, such as "Is tenure under 6 months?", and ends in a predicted class. In KNIME, use Decision Tree Learner and Decision Tree Predictor. A tree is easy to read, so you can sanity check its rules. A deep tree can memorize training data, which is called overfitting. Settings that prune the tree or require a minimum number of rows per node help limit it.

A random forest trains many trees on varied samples of the data and combines their votes. Use Random Forest Learner and Random Forest Predictor. It often generalizes better than one tree, but it is harder to explain. Set a seed for reproducible results.

The workflow is the pattern from the previous lesson:

TEXT
Table Partitioner -> Decision Tree Learner -> Decision Tree Predictor -> Scorer

The confusion matrix

The Scorer node compares the true class with the predicted class and reports a confusion matrix along with measures such as accuracy, F1-score, and Cohen's kappa. For a two-class problem where "leave" is the positive class:

Predicted leave Predicted stay
Actually leave True positive False negative
Actually stay False positive True negative

From these counts:

  • Precision: of those predicted to leave, how many really left?
  • Recall: of those who really left, how many did you find?
  • F1: a balance of precision and recall.
  • Accuracy: the share of all predictions that were right.
Python Stand-in: metrics from a confusion matrix

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 "everyone stays" rule has 90% accuracy and finds nobody. The simple model has slightly lower accuracy (89%) but finds 7 of 10 leavers. With imbalanced classes, accuracy alone misleads.

Choose a metric by cost

Ask what each error costs. Missing a leaving customer might lose their future revenue. A false alarm might only cost one email. Then recall matters more. If a false alarm blocks a legitimate payment, precision matters more. Agree on this with the people who use the results before comparing models.

Probabilities and thresholds

Predictors can output class probabilities as extra columns; enable this in the predictor settings when you need it. By default the more likely class wins, but you can choose a different threshold, for example flagging anyone with a leave probability of at least 0.3. Lowering the threshold usually raises recall and lowers precision.

Python Stand-in: move the threshold

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.

Choose the threshold using validation data, not the final test set, or you tune to the test.

The ROC curve

The ROC Curve node plots the true positive rate against the false positive rate across all thresholds. It needs predicted probabilities and a two-class problem, and you name the positive class. A curve nearer the top left indicates better separation. The area under it summarizes ranking ability. With very rare positives, also examine precision and recall, since ROC can look optimistic.

Check for reasonableness

  • Look at the tree rules or feature importance where available. A surprising top feature often signals leakage.
  • Compare training and test scores. A big gap suggests overfitting.
  • Check performance on important subgroups.
  • Use cross-validation before declaring one model better than another by a tiny margin.

Do not keep adjusting your model until the test score looks good. Every change based on the test set turns it into training information. Keep a final set that you use once.

Compare models fairly

When you compare a tree with a forest, give both the same training rows, the same preparation, and the same evaluation rows. Change one thing at a time. If you tune one model heavily and leave the other at its defaults, the comparison says more about your effort than about the algorithms. Record each experiment in a small table with the settings, the score, and the date.

Run Model Setting changed Validation F1
1 Tree default record yours
2 Tree larger minimum node size record yours
3 Forest default record yours

Explain the result to others

People who use the model rarely care about the name of a metric. Translate it into counts. For example, "of every 100 customers we flag, about this many were really going to leave" is clearer than "precision". Use your own validation numbers, and say how many examples they are based on. A score computed from 15 positive cases is much less certain than one from 1,500.

Also tell them what the model does not do. It cannot explain why a particular person leaves, and it may behave poorly for groups that were rare in training data. These limits belong in the documentation next to the score.

More than two classes

With three or more classes, such as ticket categories, the confusion matrix grows to one row and one column per class. Read it row by row to find which classes are mistaken for each other. Overall accuracy can hide a class that the model almost never predicts. Look at recall for every class, especially small ones. Frequent confusion between two classes may mean the categories themselves are unclear, so check with the people who label the data before you tune the model further.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Of the people predicted to leave, how many really left? Which metric is this?

    Precision measures how correct your positive predictions are.
  2. 2. Why can accuracy mislead when only 10% of customers leave?

    A useless rule can still have high accuracy with imbalanced classes.
  3. 3. What does the ROC Curve node need?

    The curve is built from probability thresholds for a two-class problem.
  4. 4. What usually happens when you lower the threshold for flagging leavers?

    You catch more positives but usually add more false alarms.

Summary

  • Decision trees are readable; random forests combine many trees.
  • The Scorer gives a confusion matrix and summary measures.
  • Pick precision, recall, or F1 according to error costs.
  • Thresholds trade precision for recall, and the ROC Curve shows the trade-off.
  • Keep a final test set untouched and check for leakage and subgroup problems.

Next: Regression, Clustering, and Reusing Models

Tu progreso se guarda en este navegador. No necesitas una cuenta.