@Override public DecisionTree train(double[][] x, int[] y) { return new DecisionTree(attributes, x, y, maxNodes, nodeSize, rule); } }

/** * Returns the maximum depth" of the tree -- the number of * nodes along the longest path from the root node * down to the farthest leaf node.*/ public int maxDepth() { return maxDepth(root); }

trees[t] = new DecisionTree(attributes, x, y, maxNodes, 1, x[0].length, DecisionTree.SplitRule.GINI, samples, order); err[i] = trees[t].predict(x[i]) != y[i]; double[] imp = tree.importance(); for (int i = 0; i < imp.length; i++) { importance[i] += imp[i];

DecisionTree tree = new DecisionTree(attributes, x, y, maxNodes, nodeSize, mtry, rule, samples.clone(), order); if (samples[i] == 0) { oob++; int p = tree.predict(x[i]); if (p == y[i]) correct++; synchronized (prediction[i]) {

double[] imp = tree.tree.importance(); for (int i = 0; i < imp.length; i++) { importance[i] += imp[i];

private DecisionTree(int maxNodes, int[] classArray, NumericColumn... columns) { double[][] data = DoubleArrays.to2dArray(columns); this.classifierModel = new smile.classification.DecisionTree(data, classArray, maxNodes); }

@Override public int predict(double[] x) { double[] y = new double[k]; for (int i = 0; i < trees.length; i++) { y[trees[i].predict(x)] += alpha[i]; } return Math.whichMax(y); }

@Override public int predict(double[] x) { int[] y = new int[k]; for (Tree tree : trees) { y[tree.tree.predict(x)]++; } return Math.whichMax(y); }

@Override public int predict(double[] x, double[] posteriori) { if (posteriori.length != k) { throw new IllegalArgumentException(String.format("Invalid posteriori vector size: %d, expected: %d", posteriori.length, k)); } Arrays.fill(posteriori, 0.0); int[] y = new int[k]; double[] pos = new double[k]; for (Tree tree : trees) { y[tree.tree.predict(x, pos)]++; for (int i = 0; i < k; i++) { posteriori[i] += tree.weight * pos[i]; } } Math.unitize1(posteriori); return Math.whichMax(y); }

/** * Predicts the class label of an instance and also calculate a posteriori * probabilities. Not supported. */ @Override public int predict(double[] x, double[] posteriori) { Arrays.fill(posteriori, 0.0); for (int i = 0; i < trees.length; i++) { posteriori[trees[i].predict(x)] += alpha[i]; } double sum = Math.sum(posteriori); for (int i = 0; i < k; i++) { posteriori[i] /= sum; } return Math.whichMax(posteriori); }

/** * Test the model on a validation dataset. * * @param x the test data set. * @param y the test data labels. * @param measures the performance measures of classification. * @return performance measures with first 1, 2, ..., decision trees. */ public double[][] test(double[][] x, int[] y, ClassificationMeasure[] measures) { int T = trees.size(); int m = measures.length; double[][] results = new double[T][m]; int n = x.length; int[] label = new int[n]; double[][] prediction = new double[n][k]; for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j][trees.get(i).tree.predict(x[j])]++; label[j] = Math.whichMax(prediction[j]); } for (int j = 0; j < m; j++) { results[i][j] = measures[j].measure(y, label); } } return results; }

/** * Test the model on a validation dataset. * * @param x the test data set. * @param y the test data response values. * @return accuracies with first 1, 2, ..., decision trees. */ public double[] test(double[][] x, int[] y) { int T = trees.size(); double[] accuracy = new double[T]; int n = x.length; int[] label = new int[n]; int[][] prediction = new int[n][k]; Accuracy measure = new Accuracy(); for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j][trees.get(i).tree.predict(x[j])]++; label[j] = Math.whichMax(prediction[j]); } accuracy[i] = measure.measure(y, label); } return accuracy; }

for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j] += alpha[i] * trees[i].predict(x[j]); label[j] = prediction[j] > 0 ? 1 : 0; for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j][trees[i].predict(x[j])] += alpha[i]; label[j] = Math.whichMax(prediction[j]);

for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j] += alpha[i] * trees[i].predict(x[j]); label[j] = prediction[j] > 0 ? 1 : 0; for (int i = 0; i < T; i++) { for (int j = 0; j < n; j++) { prediction[j][trees[i].predict(x[j])] += alpha[i]; label[j] = Math.whichMax(prediction[j]);

## Javadoc

The algorithms that are used for constructing decision trees usually work top-down by choosing a variable at each step that is the next best variable to use in splitting the set of items. "Best" is defined by how well the variable splits the set into homogeneous subsets that have the same value of the target variable. Different algorithms use different formulae for measuring "best". Used by the CART algorithm, Gini impurity is a measure of how often a randomly chosen element from the set would be incorrectly labeled if it were randomly labeled according to the distribution of labels in the subset. Gini impurity can be computed by summing the probability of each item being chosen times the probability of a mistake in categorizing that item. It reaches its minimum (zero) when all cases in the node fall into a single target category. Information gain is another popular measure, used by the ID3, C4.5 and C5.0 algorithms. Information gain is based on the concept of entropy used in information theory. For categorical variables with different number of levels, however, information gain are biased in favor of those attributes with more levels. Instead, one may employ the information gain ratio, which solves the drawback of information gain.

Classification and Regression Tree techniques have a number of advantages over many of those alternative techniques. Simple to understand and interpret. In most cases, the interpretation of results summarized in a tree is very simple. This simplicity is useful not only for purposes of rapid classification of new observations, but can also often yield a much simpler "model" for explaining why observations are classified or predicted in a particular manner. Able to handle both numerical and categorical data. Other techniques are usually specialized in analyzing datasets that have only one type of variable. Tree methods are nonparametric and nonlinear. The final results of using tree methods for classification or regression can be summarized in a series of (usually few) logical if-then conditions (tree nodes). Therefore, there is no implicit assumption that the underlying relationships between the predictor variables and the dependent variable are linear, follow some specific non-linear link function, or that they are even monotonic in nature. Thus, tree methods are particularly well suited for data mining tasks, where there is often little a priori knowledge nor any coherent set of theories or predictions regarding which variables are related and how. In those types of data analytics, tree methods can often reveal simple relationships between just a few variables that could have easily gone unnoticed using other analytic techniques. One major problem with classification and regression trees is their high variance. Often a small change in the data can result in a very different series of splits, making interpretation somewhat precarious. Besides, decision-tree learners can create over-complex trees that cause over-fitting. Mechanisms such as pruning are necessary to avoid this problem. Another limitation of trees is the lack of smoothness of the prediction surface.

Some techniques such as bagging, boosting, and random forest use more than one decision tree for their analysis.

## Most used methods

- <init>Constructor. Learns a classification tree with (most) given number of leaves. All attributes are ass
- predictPredicts the class label of an instance and also calculate a posteriori probabilities. The posterior
- importanceReturns the variable importance. Every time a split of a node is made on variable the (GINI, informa
- maxDepth

## Popular in Java

- Making http post requests using okhttp
- runOnUiThread (Activity)
- setScale (BigDecimal)
- getSystemService (Context)
- MalformedURLException (java.net)This exception is thrown when a program attempts to create an URL from an incorrect specification.
- ServerSocket (java.net)This class represents a server-side socket that waits for incoming client connections. A ServerSocke
- Format (java.text)The base class for all formats. This is an abstract base class which specifies the protocol for clas
- Semaphore (java.util.concurrent)A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
- JarFile (java.util.jar)JarFile is used to read jar entries and their associated data from jar files.
- JOptionPane (javax.swing)
- Top plugins for Android Studio