API Reference

ClassTree

class classtrees.ClassTree(impurity='gini', max_height=None, min_samples_split=2, min_samples_leaf=1, max_features=None, random_state=None)

Bases: object

Decision tree classifier.

The classifier recursively partitions the feature space and assigns a class label to each terminal node (leaf). Splits are selected according to the specified impurity criterion.

Parameters:
impurity{‘gini’, ‘entropy’}, default=’gini’

Impurity criterion used to evaluate candidate splits.

  • 'gini': Gini impurity.

  • 'entropy': entropy.

max_heightint or None, default=None

Maximum height of the tree. The minimal value is 0, then only a root note is created. If None, nodes are expanded until no further valid splits can be made.

min_samples_splitint, default=2

Minimum number of samples required to split a node. Must be at least 2.

min_samples_leafint, default=1

Minimum number of samples required in each leaf node.

max_featuresint or None, default=None

Number of features considered when searching for the best split. If None, all features are considered.

random_stateint or None, default=None

Seed used by the random number generator. If None, an seed is set based on current timestamp.

Methods

fit(X, y)

Build a decision tree classifier from training data.

predict(X)

Predict class labels for samples in X.

predict_proba(X)

Predict class probabilities for samples in X.

Notes

Input features are expected to be provided as a two-dimensional numpy.ndarray of floating-point values. Target labels should be provided as a one-dimensional integer array.

Examples

>>> clf = ClassTree(max_height=3, random_state=42)
>>> clf.fit(X_train, y_train)
>>> y_pred = clf.predict(X_test)
fit(X, y)

Build a decision tree classifier from training data.

Parameters:
Xndarray of shape (n_samples, n_features)

Training feature matrix. Must contain numeric values.

yndarray of shape (n_samples,)

Target class labels encoded as integers.

Returns:
ClassTree

Fitted classifier.

Notes

The number of samples in X and y must match.

Examples

>>> clf = ClassTree()
>>> clf.fit(X, y)
predict(X)

Predict class labels for samples in X.

Parameters:
Xndarray of shape (n_samples, n_features)

Input samples for which predictions are requested.

Returns:
ndarray of shape (n_samples,)

Predicted class labels.

Examples

>>> y_pred = clf.predict(X_test)
predict_proba(X)

Predict class probabilities for samples in X.

Parameters:
Xndarray of shape (n_samples, n_features)

Input samples.

Returns:
ndarray of shape (n_samples, n_classes)

Predicted class probabilities. Each row contains the probability estimates for all classes and sums to 1.

Examples

>>> proba = clf.predict_proba(X_test)
>>> proba.shape
(n_samples, n_classes)

RandomForest

class classtrees.RandomForest(n_estimators=100, impurity='gini', max_height=None, min_samples_split=2, min_samples_leaf=1, max_features=None, random_state=None, n_jobs=1)

Bases: object

Random forest classifier.

A random forest is an ensemble of decision trees trained on bootstrap samples of the training data. Predictions are obtained by aggregating the predictions of individual trees, which typically improves generalization performance and reduces overfitting compared to a single decision tree.

Parameters:
n_estimatorsint, default=100

Number of decision trees in the forest. Must be at least 1.

impurity{‘gini’, ‘entropy’}, default=’gini’

Impurity criterion used to evaluate candidate splits.

  • 'gini': Gini impurity.

  • 'entropy': entropy.

max_heightint or None, default=None

Maximum height of the tree. The minimal value is 0, then only a root note is created. If None, nodes are expanded until no further valid splits can be made.

min_samples_splitint, default=2

Minimum number of samples required to split a node. Must be at least 2.

min_samples_leafint, default=1

Minimum number of samples required in each leaf node.

max_featuresint or None, default=None

Number of features considered when searching for the best split in each tree. If None, all features are considered.

random_stateint or None, default=None

Seed used by the random number generator. If None, an seed is set based on current timestamp.

n_jobsint, default=1

Number of worker threads used during training and prediction.

  • 1: Use a single thread.

  • -1: Use all available CPU cores.

  • n >= 1: Use exactly n threads.

Methods

fit(X, y)

Build a random forest classifier from training data.

predict(X)

Predict class labels for samples in X.

predict_proba(X)

Predict class probabilities for samples in X.

Notes

Input features are expected to be provided as a two-dimensional numpy.ndarray of floating-point values. Target labels should be provided as a one-dimensional integer array.

Examples

>>> clf = RandomForest(
...     n_estimators=200,
...     max_height=10,
...     random_state=42
... )
>>> clf.fit(X_train, y_train)
>>> y_pred = clf.predict(X_test)
fit(X, y)

Build a random forest classifier from training data.

Parameters:
Xndarray of shape (n_samples, n_features)

Training feature matrix.

yndarray of shape (n_samples,)

Target class labels encoded as integers.

Returns:
RandomForest

Fitted classifier.

Notes

The forest consists of n_estimators independently trained decision trees.

Examples

>>> clf = RandomForest()
>>> clf.fit(X, y)
predict(X)

Predict class labels for samples in X.

Parameters:
Xndarray of shape (n_samples, n_features)

Input samples.

Returns:
ndarray of shape (n_samples,)

Predicted class labels obtained by soft voting among the trees in the forest.

Examples

>>> y_pred = clf.predict(X_test)
predict_proba(X)

Predict class probabilities for samples in X.

Parameters:
Xndarray of shape (n_samples, n_features)

Input samples.

Returns:
ndarray of shape (n_samples, n_classes)

Predicted class probabilities. Probabilities are computed by averaging the class probability estimates of all trees in the forest.

Examples

>>> proba = clf.predict_proba(X_test)