Source code for lale.lib.sklearn.random_forest_classifier

# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sklearn.ensemble.forest
import lale.helpers
import lale.operators

[docs]class RandomForestClassifierImpl(): def __init__(self, n_estimators=10, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight=None): self._hyperparams = { 'n_estimators': n_estimators, 'criterion': criterion, 'max_depth': max_depth, 'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf, 'min_weight_fraction_leaf': min_weight_fraction_leaf, 'max_features': max_features, 'max_leaf_nodes': max_leaf_nodes, 'min_impurity_decrease': min_impurity_decrease, 'min_impurity_split': min_impurity_split, 'bootstrap': bootstrap, 'oob_score': oob_score, 'n_jobs': n_jobs, 'random_state': random_state, 'verbose': verbose, 'warm_start': warm_start, 'class_weight': class_weight}
[docs] def fit(self, X, y, **fit_params): self._sklearn_model = sklearn.ensemble.forest.RandomForestClassifier(**self._hyperparams) if fit_params is None: self._sklearn_model.fit(X, y) else: self._sklearn_model.fit(X, y, **fit_params) return self
[docs] def predict(self, X): return self._sklearn_model.predict(X)
[docs] def predict_proba(self, X): return self._sklearn_model.predict_proba(X)
_hyperparams_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'A random forest classifier.', 'allOf': [{ 'type': 'object', 'required': ['class_weight'], 'relevantToOptimizer': ['n_estimators', 'criterion', 'max_depth', 'min_samples_split', 'min_samples_leaf', 'max_features', 'bootstrap'], 'additionalProperties': False, 'properties': { 'n_estimators': { 'type': 'integer', 'minimumForOptimizer': 10, 'maximumForOptimizer': 100, 'default': 10, 'description': 'The number of trees in the forest.'}, 'criterion': { 'enum': ['gini', 'entropy'], 'default': 'gini', 'description': 'The function to measure the quality of a split. Supported criteria are'}, 'max_depth': { 'anyOf': [{ 'type': 'integer', 'minimumForOptimizer': 3, 'maximumForOptimizer': 5}, { 'enum': [None]}], 'default': None, 'description': 'The maximum depth of the tree. If None, then nodes are expanded until'}, 'min_samples_split': { 'anyOf': [{ 'type': 'integer', 'minimumForOptimizer': 2, 'maximumForOptimizer': 20, 'distribution': 'uniform'}, { 'type': 'number', 'minimumForOptimizer': 0.01, 'maximumForOptimizer': 0.5}], 'default': 2, 'description': 'The minimum number of samples required to split an internal node:'}, 'min_samples_leaf': { 'anyOf': [{ 'type': 'integer', 'minimumForOptimizer': 1, 'maximumForOptimizer': 20, 'distribution': 'uniform'}, { 'type': 'number', 'minimumForOptimizer': 0.01, 'maximumForOptimizer': 0.5}], 'default': 1, 'description': 'The minimum number of samples required to be at a leaf node.'}, 'min_weight_fraction_leaf': { 'type': 'number', 'default': 0.0, 'description': 'The minimum weighted fraction of the sum total of weights (of all'}, 'max_features': { 'anyOf': [{ 'type': 'integer', 'forOptimizer': False}, { 'type': 'number', 'minimum': 0.0, 'exclusiveMinimum': True, 'minimumForOptimizer': 0.0, 'maximumForOptimizer': 1.0, 'distribution': 'uniform'}, { 'enum': ['auto', 'sqrt', 'log2', None]}], 'default': 'auto', 'description': 'The number of features to consider when looking for the best split:'}, 'max_leaf_nodes': { 'anyOf': [{ 'type': 'integer'}, { 'enum': [None]}], 'default': None, 'description': 'Grow trees with ``max_leaf_nodes`` in best-first fashion.'}, 'min_impurity_decrease': { 'type': 'number', 'default': 0.0, 'description': 'A node will be split if this split induces a decrease of the impurity'}, 'min_impurity_split': { 'anyOf': [{ 'type': 'number'}, { 'enum': [None]}], 'default': None, 'description': 'Threshold for early stopping in tree growth. A node will split'}, 'bootstrap': { 'type': 'boolean', 'default': True, 'description': 'Whether bootstrap samples are used when building trees. If False, the'}, 'oob_score': { 'type': 'boolean', 'default': False, 'description': 'Whether to use out-of-bag samples to estimate'}, 'n_jobs': { 'anyOf': [{ 'type': 'integer'}, { 'enum': [None]}], 'default': None, 'description': 'The number of jobs to run in parallel for both `fit` and `predict`.'}, 'random_state': { 'anyOf': [{ 'type': 'integer'}, { 'type': 'object'}, { 'enum': [None]}], 'default': None, 'description': 'If int, random_state is the seed used by the random number generator;'}, 'verbose': { 'type': 'integer', 'default': 0, 'description': 'Controls the verbosity when fitting and predicting.'}, 'warm_start': { 'type': 'boolean', 'default': False, 'description': 'When set to ``True``, reuse the solution of the previous call to fit'}, 'class_weight': { 'anyOf': [{ 'type': 'object'}, #dict, list of dicts, {'enum': ['balanced', 'balanced_subsample', None]}], 'description': 'Weights associated with classes in the form ``{class_label: weight}``.', 'default': None}, }}] } _input_fit_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Build a forest of trees from the training set (X, y).', 'type': 'object', 'properties': { 'X': { 'anyOf': [{ 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, }}], 'description': 'The training input samples. Internally, its dtype will be converted'}, 'y': { 'type': 'array', 'items': { 'anyOf':[ {'type': 'number'}, {'type': 'string'}]}, 'description': 'The target values (class labels in classification, real numbers in'}, 'sample_weight': { 'anyOf': [{ 'type': 'array', 'items': { 'type': 'number'}, }, { 'enum': [None]}], 'description': 'Sample weights. If None, then samples are equally weighted. Splits'}, }, } _input_predict_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Predict class for X.', 'type': 'object', 'properties': { 'X': { 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, }, 'description': 'The input samples. Internally, its dtype will be converted to'}, }, } _output_predict_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'The predicted classes.', 'type': 'array', 'items': { 'anyOf':[ {'type': 'number'}, {'type': 'string'}]}, } _input_predict_proba_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Predict class probabilities for X.', 'type': 'object', 'properties': { 'X': { 'anyOf': [{ 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, }}], 'description': 'The input samples. Internally, its dtype will be converted to'}, }, } _output_predict_proba_schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'array of shape = [n_samples, n_classes], or a list of n_outputs such arrays if n_outputs > 1.', 'type': 'array', 'items': { 'type': 'array', 'items': { 'type': 'number'}, } } _combined_schemas = { '$schema': 'http://json-schema.org/draft-04/schema#', 'description': 'Combined schema for expected data and hyperparameters.', 'documentation_url': 'https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html', 'type': 'object', 'tags': { 'pre': [], 'op': ['estimator'], 'post': []}, 'properties': { 'hyperparams': _hyperparams_schema, 'input_fit': _input_fit_schema, 'input_predict': _input_predict_schema, 'output': _output_predict_schema, 'input_predict_proba': _input_predict_proba_schema, 'output_predict_proba': _output_predict_proba_schema}, } if (__name__ == '__main__'): lale.helpers.validate_is_schema(_combined_schemas) RandomForestClassifier = lale.operators.make_operator(RandomForestClassifierImpl, _combined_schemas)