dislib.trees.distributed#

class dislib.trees.distributed.DecisionTreeClassifier(n_classes, try_features, max_depth, distr_depth, sklearn_max, bootstrap, random_state, range_max=None, range_min=None, n_split_points='auto', split_computation='raw', sync_after_fit=True)[source]#

Bases: BaseDecisionTree

A distributed decision tree classifier.

Parameters:
  • try_features (int) – The number of features to consider when looking for the best split.

    Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than try_features features.

  • max_depth (int) – The maximum depth of the tree. If np.inf, then nodes are expanded until all leaves are pure.

  • distr_depth (int) – Number of levels of the tree in which the nodes are split in a distributed way.

  • bootstrap (bool) – Randomly select n_instances samples with repetition (used in random forests).

  • random_state (RandomState instance) – The random number generator.

  • n_classes (int) – Number of classes that appear on the dataset.

  • range_min (ds-array or np.array) – Contains the minimum values of the different attributes of the dataset

  • range_max (ds-array or np.array) – Contains the maximum values of the different attributes of the dataset

  • n_split_points (String or int) – Number of split points to evaluate. “auto”, “sqrt” or integer value.

  • split_computation (String) – “raw”, “gaussian_approximation” or “uniform_approximation” distribution of the values followed by the split points selected.

  • sync_after_fit (bool) – Synchronize or not after the training.

Variables:
  • n_features (int) – The number of features of the dataset. It can be a pycompss.runtime.Future object.

  • tree (None or _Node) – The root node of the tree after the tree is fitted.

  • nodes_info (None or list of _InnerNodeInfo, _LeafInfo and _Node) – List of the node information for the nodes of the tree in the same order as obtained in the fit() method, up to distr_depth depth. After fit(), it is a pycompss.runtime.Future object.

fit(x, y)[source]#

Fits the DecisionTree.

Parameters:
  • x (ds-array) – Samples of the dataset.

  • y (ds-array) – Labels of the dataset.

predict(x, collect=False)[source]#

Predicts target values or classes for the given samples using a fitted tree.

Parameters:

x_row (ds-array) – A row block of samples.

Returns:

predicted – An array with the predicted classes or values for the given samples. For classification, the values are codes of the fitted dislib.classification.rf.data.RfDataset. The returned object can be a pycompss.runtime.Future object.

Return type:

ndarray

predict_proba(x, collect=False)[source]#

Predicts class probabilities for a row block using a fitted tree.

Parameters:

x_row (ds-array) – A row block of samples.

Returns:

predicted_proba – A list with the predicted probabilities for the given samples. It contains a numpy array (if collect=True) or Future object (if collect=False) for each of the blocks in the ds-array to predict. Thus the length of the list is the same as the number of blocks the ds-array contains. The shape inside each prediction is (len(x.reg_shape[0]), self.n_classes). The returned object can be a pycompss.runtime. Future object.

Return type:

list

class dislib.trees.distributed.DecisionTreeRegressor(try_features, max_depth, distr_depth, sklearn_max, bootstrap, random_state, range_max=None, range_min=None, n_split_points='auto', split_computation='raw', sync_after_fit=True)[source]#

Bases: BaseDecisionTree

A distributed decision tree regressor.

Parameters:
  • try_features (int) – The number of features to consider when looking for the best split.

    Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than try_features features.

  • max_depth (int) – The maximum depth of the tree. If np.inf, then nodes are expanded until all leaves are pure.

  • distr_depth (int) – Number of levels of the tree in which the nodes are split in a distributed way.

  • bootstrap (bool) – Randomly select n_instances samples with repetition (used in random forests).

  • random_state (RandomState instance) – The random number generator.

  • range_min (ds-array or np.array) – Contains the minimum values of the different attributes of the dataset

  • range_max (ds-array or np.array) – Contains the maximum values of the different attributes of the dataset

  • n_split_points (String or int) – Number of split points to evaluate. “auto”, “sqrt” or integer value.

  • split_computation (String) – “raw”, “gaussian_approximation” or “uniform_approximation” distribution of the values followed by the split points selected.

  • sync_after_fit (bool) – Synchronize or not after the training.

Variables:
  • n_features (int) – The number of features of the dataset. It can be a pycompss.runtime.Future object.

  • tree (None or _Node) – The root node of the tree after the tree is fitted.

  • nodes_info (None or list of _InnerNodeInfo, _Node and _LeafInfo) – List of the node information for the nodes of the tree in the same order as obtained in the fit() method, up to distr_depth depth. After fit(), it is a pycompss.runtime.Future object.

fit(x, y)[source]#

Fits the DecisionTreeRegressor.

Parameters:
  • x (ds-array) – Samples of the dataset.

  • y (ds-array) – Labels of the dataset.

predict(x, collect=False)[source]#

Predicts target values or classes for the given samples using a fitted tree.

Parameters:

x_row (ds-array) – A row block of samples.

Returns:

predicted – An array with the predicted classes or values for the given samples. For classification, the values are codes of the fitted dislib.classification.rf.data.RfDataset. The returned object can be a pycompss.runtime.Future object.

Return type:

ndarray

class dislib.trees.distributed.RandomForestClassifier(n_classes, n_estimators=10, try_features='sqrt', max_depth=inf, distr_depth='auto', sklearn_max=100000000.0, hard_vote=False, random_state=None, range_max=None, range_min=None, bootstrap=True, n_split_points='auto', split_computation='raw', sync_after_fit=True)[source]#

Bases: BaseRandomForest

A distributed random forest classifier.

Parameters:
  • n_estimators (int, optional (default=10)) – Number of trees to fit.

  • try_features (int, str or None, optional (default=’sqrt’)) – The number of features to consider when looking for the best split:

    • If “sqrt”, then try_features=sqrt(n_features).

    • If “third”, then try_features=n_features // 3.

    • If None, then try_features=n_features.

    Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than try_features features.

  • max_depth (int or np.inf, optional (default=np.inf)) – The maximum depth of the tree. If np.inf, then nodes are expanded until all leaves are pure.

  • distr_depth (int or str, optional (default=’auto’)) – Number of levels of the tree in which the nodes are split in a distributed way.

  • sklearn_max (int or float, optional (default=1e8)) – Maximum size (len(subsample)*n_features) of the arrays passed to sklearn’s DecisionTreeClassifier.fit(), which is called to fit subtrees (subsamples) of our DecisionTreeClassifier. sklearn fit() is used because it’s faster, but requires loading the data to memory, which can cause memory problems for large datasets. This parameter can be adjusted to fit the hardware capabilities.

  • hard_vote (bool, optional (default=False)) – If True, it uses majority voting over the predict() result of the decision tree predictions. If False, it takes the class with the higher probability given by predict_proba(), which is an average of the probabilities given by the decision trees.

  • random_state (int, RandomState instance or None, optional (default=None)) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • range_min (ds-array or np.array) – Contains the minimum values of the different attributes of the dataset

  • range_max (ds-array or np.array) – Contains the maximum values of the different attributes of the dataset

  • n_split_points (String or int) – Number of split points to evaluate. “auto”, “sqrt” or integer value.

  • split_computation (String) – “raw”, “gaussian_approximation” or “uniform_approximation” distribution of the values followed by the split points selected.

  • sync_after_fit (bool) – Synchronize or not after the training.

Variables:
  • nodes_info (None or dictionary of lists)

  • _LeafInfo (of _InnerNodeInfo, _Node and) – List of the node information for the nodes of the different trees (each key represents a tree) in the same order as obtained in the fit() method, up to distr_depth depth. After fit(), it is a pycompss.runtime.Future object.

fit(x, y)[source]#

Fits a RandomForest.

Parameters:
  • x (ds-array, shape=(n_samples, n_features)) – The training input samples. Internally, its dtype will be converted to dtype=np.float32.

  • y (ds-array, shape=(n_samples, 1)) – The target values.

Returns:

self

Return type:

RandomForest

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

load_model(filepath, load_format='json')[source]#

Loads a model from a file. The model is reinstantiated in the exact same state in which it was saved, without any of the code used for model definition or fitting.

Parameters:
  • filepath (str) – Path of the saved the model

  • load_format (str, optional (default=’json’)) – Format used to load the model.

Examples

>>> from dislib.trees import RandomForestClassifier
>>> import numpy as np
>>> import dislib as ds
>>> x = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
>>> y = np.array([1, 1, 2, 2, 2, 1])
>>> x_train = ds.array(x, (2, 2))
>>> y_train = ds.array(y, (2, 1))
>>> model = RandomForestClassifier(n_estimators=2, random_state=0)
>>> model.fit(x_train, y_train)
>>> save_model(model, '/tmp/model')
>>> loaded_model = load_model('/tmp/model')
>>> x_test = ds.array(np.array([[0, 0], [4, 4]]), (2, 2))
>>> model_pred = model.predict(x_test)
>>> loaded_model_pred = loaded_model.predict(x_test)
>>> assert np.allclose(model_pred.collect(),
predict(x)[source]#

Predicts target values using a fitted forest.

Parameters:

x (ds-array, shape=(n_samples, n_features)) – The input samples.

Returns:

y_pred – Predicted values for x.

Return type:

ds-array, shape=(n_samples, 1)

predict_proba(x)[source]#

Predicts class probabilities using a fitted forest.

The probabilities are obtained as an average of the probabilities of each decision tree.

Parameters:

x (ds-array, shape=(n_samples, n_features)) – The input samples.

Returns:

probabilities – Predicted probabilities for the samples to belong to each class. The columns of the array correspond to the classes given at self.classes.

Return type:

ds-array, shape=(n_samples, n_classes)

save_model(filepath, overwrite=True, save_format='json')[source]#

Saves a model to a file. The model is synchronized before saving and can be reinstantiated in the exact same state, without any of the code used for model definition or fitting.

Parameters:
  • filepath (str) – Path where to save the model

  • overwrite (bool, optional (default=True)) – Whether any existing model at the target location should be overwritten.

  • save_format (str, optional (default=’json)) – Format used to save the models.

Examples

>>> from dislib.trees import RandomForestClassifier
>>> import numpy as np
>>> import dislib as ds
>>> x = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
>>> y = np.array([1, 1, 2, 2, 2, 1])
>>> x_train = ds.array(x, (2, 2))
>>> y_train = ds.array(y, (2, 1))
>>> model = RandomForestClassifier(n_estimators=2, random_state=0)
>>> model.fit(x_train, y_train)
>>> model.save_model('/tmp/model')
>>> model.save_model('/tmp/model')
>>> loaded_model = RandomForestClassifier()
>>> loaded_model.load_model('/tmp/model')
>>> x_test = ds.array(np.array([[0, 0], [4, 4]]), (2, 2))
>>> model_pred = model.predict(x_test)
>>> loaded_model_pred = loaded_model.predict(x_test)
>>> assert np.allclose(model_pred.collect(),
>>> loaded_model_pred.collect())
score(x, y, collect=False)[source]#

Accuracy classification score.

Returns the mean accuracy of the predictions on the given test data.

Parameters:
  • x (ds-array, shape=(n_samples, n_features)) – The training input samples.

  • y (ds-array, shape (n_samples, 1)) – The true labels.

  • collect (bool, optional (default=False)) – When True, a synchronized result is returned.

Returns:

score – Fraction of correctly classified samples.

Return type:

float (as future object)

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

class dislib.trees.distributed.RandomForestRegressor(n_estimators=10, try_features='sqrt', max_depth=inf, distr_depth='auto', sklearn_max=100000000.0, random_state=None, range_max=None, range_min=None, bootstrap=True, n_split_points='auto', split_computation='raw', sync_after_fit=True)[source]#

Bases: BaseRandomForest

A distributed random forest regressor.

Parameters:
  • n_estimators (int, optional (default=10)) – Number of trees to fit.

  • try_features (int, str or None, optional (default=’sqrt’)) – The number of features to consider when looking for the best split:

    • If “sqrt”, then try_features=sqrt(n_features).

    • If “third”, then try_features=n_features // 3.

    • If None, then try_features=n_features.

    Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than try_features features.

  • max_depth (int or np.inf, optional (default=np.inf)) – The maximum depth of the tree. If np.inf, then nodes are expanded until all leaves are pure.

  • distr_depth (int or str, optional (default=’auto’)) – Number of levels of the tree in which the nodes are split in a distributed way.

  • sklearn_max (int or float, optional (default=1e8)) – Maximum size (len(subsample)*n_features) of the arrays passed to sklearn’s DecisionTreeRegressor.fit(), which is called to fit subtrees (subsamples) of our DecisionTreeRegressor. sklearn fit() is used because it’s faster, but requires loading the data to memory, which can cause memory problems for large datasets. This parameter can be adjusted to fit the hardware capabilities.

  • random_state (int, RandomState instance or None, optional)

  • (default=None) – If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.

  • range_min (ds-array or np.array) – Contains the minimum values of the different attributes of the dataset

  • range_max (ds-array or np.array) – Contains the maximum values of the different attributes of the dataset

  • n_split_points (String or int) – Number of split points to evaluate. “auto”, “sqrt” or integer value.

  • split_computation (String) – “raw”, “gaussian_approximation” or “uniform_approximation” distribution of the values followed by the split points selected.

  • sync_after_fit (bool) – Synchronize or not after the training.

Variables:
  • nodes_info (None or dictionary of lists)

  • _LeafInfo (of _InnerNodeInfo, _Node and) – List of the node information for the nodes of the different trees (each key represents a tree) in the same order as obtained in the fit() method, up to distr_depth depth. After fit(), it is a pycompss.runtime.Future object.

fit(x, y)[source]#

Fits a RandomForest.

Parameters:
  • x (ds-array, shape=(n_samples, n_features)) – The training input samples. Internally, its dtype will be converted to dtype=np.float32.

  • y (ds-array, shape=(n_samples, 1)) – The target values.

Returns:

self

Return type:

RandomForest

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

load_model(filepath, load_format='json')[source]#

Loads a model from a file. The model is reinstantiated in the exact same state in which it was saved, without any of the code used for model definition or fitting.

Parameters:
  • filepath (str) – Path of the saved the model

  • load_format (str, optional (default=’json’)) – Format used to load the model.

Examples

>>> from dislib.trees import RandomForestRegressor
>>> import numpy as np
>>> import dislib as ds
>>> x = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
>>> y = np.array([1.5, 1.2, 2.7, 2.1, 0.2, 0.6])
>>> x_train = ds.array(x, (2, 2))
>>> y_train = ds.array(y, (2, 1))
>>> model = RandomForestRegressor(n_estimators=2, random_state=0)
>>> model.fit(x_train, y_train)
>>> model.save_model('/tmp/model')
>>> loaded_model = RandomForestRegressor()
>>> loaded_model.load_model('/tmp/model')
>>> x_test = ds.array(np.array([[0, 0], [4, 4]]), (2, 2))
>>> model_pred = model.predict(x_test)
>>> loaded_model_pred = loaded_model.predict(x_test)
>>> assert np.allclose(model_pred.collect(),
predict(x)[source]#

Predicts target values using a fitted forest.

Parameters:

x (ds-array, shape=(n_samples, n_features)) – The input samples.

Returns:

y_pred – Predicted values for x.

Return type:

ds-array, shape=(n_samples, 1)

save_model(filepath, overwrite=True, save_format='json')[source]#

Saves a model to a file. The model is synchronized before saving and can be reinstantiated in the exact same state, without any of the code used for model definition or fitting.

Parameters:
  • filepath (str) – Path where to save the model

  • overwrite (bool, optional (default=True)) – Whether any existing model at the target location should be overwritten.

  • save_format (str, optional (default=’json)) – Format used to save the models.

Examples

>>> from dislib.trees import RandomForestRegressor
>>> import numpy as np
>>> import dislib as ds
>>> x = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
>>> y = np.array([1.5, 1.2, 2.7, 2.1, 0.2, 0.6])
>>> x_train = ds.array(x, (2, 2))
>>> y_train = ds.array(y, (2, 1))
>>> model = RandomForestRegressor(n_estimators=2, random_state=0)
>>> model.fit(x_train, y_train)
>>> model.save_model('/tmp/model')
>>> loaded_model = RandomForestRegressor()
>>> loaded_model.load_model('/tmp/model')
>>> x_test = ds.array(np.array([[0, 0], [4, 4]]), (2, 2))
>>> model_pred = model.predict(x_test)
>>> loaded_model_pred = loaded_model.predict(x_test)
>>> assert np.allclose(model_pred.collect(),
>>> loaded_model_pred.collect())
score(x, y, collect=False)[source]#

R2 regression score.

Returns the coefficient of determination $R^2$ of the prediction. The coefficient $R^2$ is defined as $(1-u/v)$, where $u$ is the residual sum of squares ((y_true - y_pred) ** 2).sum() and $v$ is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative if the model is arbitrarily worse. A constant model that always predicts the expected value of y, disregarding the input features, would get a $R^2$ score of 0.0.

Parameters:
  • x (ds-array, shape=(n_samples, n_features)) – The training input samples.

  • y (ds-array, shape (n_samples, 1)) – The true values.

  • collect (bool, optional (default=False)) – When True, a synchronized result is returned.

Returns:

score – Coefficient of determination $R^2$.

Return type:

float (as future object)

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance