Converter tool

Decision Tree Classifier and Regressor for CSV

Fit a CART tree to a CSV with Gini or entropy splits, cost-complexity pruning chosen by cross-validation, a seeded test split and an optional random forest, then read the rules, export SQL and score new rows.

One row per case, one column per feature plus a target

1. Your data

Or paste the table

Decision Tree Classifier and Regressor for CSV

Operator: Abdulla Al Fahad. How tools are built and checked

A decision tree turns a table into a handful of if-then rules, which is why it is still the first model people reach for when they must explain a prediction to someone who will not read coefficients. Each split asks one question about one column, and each leaf gives an answer.

This page fits CART trees, for a class or for a number, to any CSV with a target column, prunes them by cost-complexity with the strength chosen by cross-validation, and exports the rules as text and as a SQL CASE expression.

Key features

  • Classification trees with Gini or entropy splits, and regression trees on squared error
  • Numeric thresholds and categorical subset splits, with up to 60 levels per text column
  • Maximum depth up to 30, minimum samples to split and per leaf, and a seeded stratified train and test split of up to half the rows
  • Cost-complexity pruning with the strength set by hand or chosen by k-fold cross-validation, with an optional one-standard-error rule
  • A collapsible SVG tree, plus accuracy, precision, recall, F1, ROC AUC for two classes, confusion matrix and feature importance
  • A random forest of 5 to 500 trees with out-of-bag error and importance, grown in a worker with a time budget and a cancel button
  • Prediction of new rows from a second CSV, with a probability and the leaf for each row

Feature types, missing values, pruning and what the exports contain

A column whose non-missing values are all numbers is split by thresholds midway between observed values; any other column is categorical, split into two groups of levels: every grouping is tried for up to 8 levels with more than two classes, and otherwise the levels are ordered by target mean or class share and cut at each point. Missing is an empty cell, NA, N/A, null, none, a dash or a question mark. Rows with a missing target are dropped, and empty, constant and over-60-level columns are dropped and listed after the fit. Up to 200,000 rows and 200 features are read.

Missing feature values follow one of two stated strategies. In the first, each split remembers which side the missing rows fit better in training and sends them there, in the tree, the rules and the SQL. In the second, missing values are filled before fitting with the median (numeric) or most common level (categorical); the fill values are listed and reused for new rows.

How it works

Splits are chosen greedily by the largest drop in impurity: Gini 1 minus the sum of squared class shares, entropy minus the sum of p log2 p, or variance for a number. Growth stops at the depth limit, the minimum sizes or when a node is pure.

Cost-complexity pruning repeatedly cuts the subtree whose removal costs the least impurity per leaf removed, giving a path of nested trees indexed by a strength alpha. Cross-validation grows a tree on each fold's training part, scores every alpha on its held-out part, and picks the alpha with the lowest mean error, or the smallest tree within one standard error of it.

The forest grows each tree on a bootstrap sample, tries the square root of the feature count (or a third for regression) at each split, and averages the leaf probabilities. A training row is scored out of bag only by the trees that did not see it.

Importance adds, over the splits of a tree, each split's gain times the share of rows that reached it, and rescales to 100%.

Common use cases

  • Briefing the retention team, a churn analyst fits a depth-4 tree on 12 customer columns and reads the rules out.
  • A risk team exporting the SQL CASE expression to score a table in the warehouse without deploying a model.
  • A data analyst checking whether a regression tree on prices beats a flat mean, using RMSE on a 30% held-out set.
  • Gini against entropy on the same table: a student checks whether the two trees differ.

How to use this tool

  1. Drop the training table, or load Iris, then pick the target column and the feature columns.
  2. Choose the criterion, depth, minimum sizes, missing-value strategy, test share and pruning rule.
  3. Press Fit the tree and read the diagram, accuracy and rules.

Example

A botany student sorting 150 iris flowers into three species, maximum depth 2

sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
7.0,3.2,4.7,1.4,versicolor
... 150 rows in all, 50 each of setosa, versicolor and virginica. Fisher's 1936 measurements as distributed by UCI, licensed CC BY 4.0

Fitted rules, leaf counts and accuracy

Rule 1: IF petal_length <= 2.45 THEN setosa (n=50, 100% of the leaf)
Rule 2: IF petal_length > 2.45 AND petal_width <= 1.75 THEN versicolor (n=54: 0 setosa, 49 versicolor, 5 virginica; 90.7% of the leaf; Gini 0.168)
Rule 3: IF petal_length > 2.45 AND petal_width > 1.75 THEN virginica (n=46: 0, 1 and 45; 97.8% of the leaf; Gini 0.0425)
Training accuracy 96.0%: 6 flowers misclassified. At the default depth of 5 the tree has 9 leaves and 100% training accuracy; with cross-validated pruning and the one-standard-error rule it keeps 4 leaves and 97.3%.

Common pitfalls

Training accuracy is optimistic. In the Iris example the unpruned tree reaches 100% on its own rows, while 5-fold cross-validation puts its error near 6%.

Single trees are unstable: dropping a few rows can change the root split. The random forest's importance is steadier than one tree's.

Identifier columns, such as a row number or customer id, can be split on and memorised. Untick them.

A regression tree predicts a flat value in each leaf and cannot extrapolate beyond the range of the training target.

On a wider scale the same data gives the random forest an out-of-bag accuracy of 94.7% over 100 trees, so a single depth-2 tree at 96.0% training accuracy is not far behind for this easy problem.

The ROC AUC is only reported for two classes, and treats the second class in alphabetical or numeric order as positive.

  • Use a test share or cross-validated pruning before quoting accuracy: a fully grown tree scores 100% on its own training rows and says nothing about new ones.
  • Read the leaf counts as well as the percentage. A leaf of 54 flowers at 90.7% is a sturdier rule than a leaf of 6 at 100%.

Common errors and how to fix them

The target has 42 classes. A classification tree supports up to 30.
Group rare classes in the file, or choose regression for a numeric target.
A regression tree needs a numeric target column.
Check for text such as "N/A" or a unit in the target column.
The time budget ended before the tree was finished.
Lower the depth, turn off cross-validated pruning, use fewer features, or raise the budget.

Best practices

  • Keep a test share of 0.2 to 0.3 and cross-validated pruning together: pruning chooses the tree size on the training rows, the test rows score it once.
  • Start at depth 3 or 4 so the diagram stays readable, and raise it only if the test error falls.
  • Compare the single tree's test accuracy with the forest's before choosing which to explain.

Choosing the pruning strength

On Iris, the weakest-link path has 7 candidate strengths, from the full tree of 9 leaves down to a single leaf. With 5 folds the lowest mean error, 6.0%, is at the full tree, while the smallest tree within one standard error has 4 leaves at alpha 0.0197 and an error of 6.7%. The difference is within the noise of 150 rows, so the page lets you take the simpler tree and says what rule picked it.

Folds depend on the seed. A different seed can move the minimum to a smaller tree, which is another reason to prefer the one-standard-error rule when you want a tree you can explain.

What the SQL contains

The CASE expression has one WHEN branch per leaf, joining the conditions on the path to it. A column name is double-quoted as it appears in your file, text levels are single-quoted with embedded quotes doubled, and each condition that could be reached by a NULL carries an OR column IS NULL term when the tree routes missing values. Paste it into a SELECT to score a table.

Frequently asked questions

Gini or entropy?

They almost always choose the same splits. Entropy penalises mixed nodes slightly more; try both and keep the one with the better test error.

How are ties between equal splits broken?

The feature that comes first in the file wins, then the lowest threshold, so refitting the same file gives the same tree.

Can I score a file with different columns?

The new file needs every feature column under the same name. A new category level that the tree never saw is treated as missing and counted in a warning.

Does the SQL handle missing values the same way?

Yes for the route strategy: each split carries the NULL side it learned. With the fill strategy the SQL assumes you fill the same values before scoring.

Why is the forest grown in a worker?

Hundreds of deep trees over many rows can take tens of seconds. A worker keeps the page responsive, honours the time budget and can be cancelled.