# Path: gbdt/_forest.py
# class Forest:
#     def __init__(self, forest):
#         if isinstance(forest, six.text_type):
#             self._forest = libgbdt.Forest(forest)
#         elif isinstance(forest, libgbdt.Forest):
#             self._forest = forest
#         else:
#             raise TypeError('Unsupported forest type: {0}'.format(type(forest)))
# 
#     def predict(self, data_store):
#         """Computes prediction scores for data_store."""
#         return self._forest.predict(data_store._data_store)
# 
#     def predict_at_checkpoints(self, data_store, checkpoints):
#         """Computes prediction scores for data_store at different checkpoints. At each checkpoint n,
#            We compute prediction scores for the sub forest from the first tree to nth tree.
#         """
#         import tempfile
# 
#         output_dir = tempfile.gettempdir()
# 
#         self._forest.predict_and_output(data_store._data_store, checkpoints, output_dir)
#         for p in checkpoints:
#             yield p, [float(line) for line in open(output_dir + '/forest.{}.score'.format(p))
#                       if line.strip()]
# 
#     def feature_importance(self):
#         """Outputs list of feature importances in descending order."""
#         return self._forest.feature_importance()
# 
#     def features(self):
#         """Outputs list of features."""
#         return [f for (f, imp) in self._forest.feature_importance()]
# 
#     def feature_importance_bar_chart(self, color=None):
#         try:
#             from matplotlib import pyplot as plt
#             import numpy
#         except ImportError:
#             raise ImportError('Please install matplotlib and numpy.')
# 
#         fimps = self.feature_importance()
#         importances = [v for _, v in fimps]
#         features = [f for f,_ in fimps]
#         ind = -numpy.arange(len(fimps))
# 
#         _, ax = plt.subplots()
#         plt.barh(ind, importances, align='center', color=color)
#         ax.set_yticks(ind)
#         ax.set_yticklabels(features)
#         ax.set_xlabel('Feature importance')
# 
#     def __str__(self):
#         return self._forest.as_json()
# 
# Path: gbdt/_libgbdt.py



from ._forest import Forest
from ._libgbdt import libgbdt 
        import json
    """
    Trains a gbdt model.
    Inputs: data_store: The Data Store.
            config: the config params. See https://github.com/yarny/gbdt/blob/master/src/proto/config.proto for all params.
            y: The target
            features: the list of features.
            w: The sampling weight.
            base_forest: The base forest upon which the training will continue.
            random_seed: the random seed.
            num_threads: The number of threads to run training with.
    Outputs: The forest.
    """
    try:
    except ImportError:
        raise ImportError('Please install json.')

    feature_set = set(features)
    float_features = [col for col in data_store.bucketized_float_cols() if str(col) in feature_set]
    cat_features = [col for col in data_store.string_cols() if col in feature_set]
    unfound_features = feature_set - (set(float_features) | set(cat_features))
    if len(unfound_features) > 0:
        raise ValueError('Failed to find feature {} in data store.'.format(unfound_features))

    config['float_feature'] = float_features
    config['categorical_feature'] = cat_features
    if 'example_sampling_rate' not in config:
        config['example_sampling_rate'] = 1
    if 'feature_sampling_rate' not in config:
        config['feature_sampling_rate'] = 1


# Path: gbdt/_forest.py
# class Forest:
#     def __init__(self, forest):
#         if isinstance(forest, six.text_type):
#             self._forest = libgbdt.Forest(forest)
#         elif isinstance(forest, libgbdt.Forest):
#             self._forest = forest
#         else:
#             raise TypeError('Unsupported forest type: {0}'.format(type(forest)))
# 
#     def predict(self, data_store):
#         """Computes prediction scores for data_store."""
#         return self._forest.predict(data_store._data_store)
# 
#     def predict_at_checkpoints(self, data_store, checkpoints):
#         """Computes prediction scores for data_store at different checkpoints. At each checkpoint n,
#            We compute prediction scores for the sub forest from the first tree to nth tree.
#         """
#         import tempfile
# 
#         output_dir = tempfile.gettempdir()
# 
#         self._forest.predict_and_output(data_store._data_store, checkpoints, output_dir)
#         for p in checkpoints:
#             yield p, [float(line) for line in open(output_dir + '/forest.{}.score'.format(p))
#                       if line.strip()]
# 
#     def feature_importance(self):
#         """Outputs list of feature importances in descending order."""
#         return self._forest.feature_importance()
# 
#     def features(self):
#         """Outputs list of features."""
#         return [f for (f, imp) in self._forest.feature_importance()]
# 
#     def feature_importance_bar_chart(self, color=None):
#         try:
#             from matplotlib import pyplot as plt
#             import numpy
#         except ImportError:
#             raise ImportError('Please install matplotlib and numpy.')
# 
#         fimps = self.feature_importance()
#         importances = [v for _, v in fimps]
#         features = [f for f,_ in fimps]
#         ind = -numpy.arange(len(fimps))
# 
#         _, ax = plt.subplots()
#         plt.barh(ind, importances, align='center', color=color)
#         ax.set_yticks(ind)
#         ax.set_yticklabels(features)
#         ax.set_xlabel('Feature importance')
# 
#     def __str__(self):
#         return self._forest.as_json()
# 
# Path: gbdt/_libgbdt.py



# Path: gbdt/_gbdt.py
from ._forest import Forest
from ._libgbdt import libgbdt 
        import json

    """
    Trains a gbdt model.
    Inputs: data_store: The Data Store.
            config: the config params. See https://github.com/yarny/gbdt/blob/master/src/proto/config.proto for all params.
            y: The target
            features: the list of features.
            w: The sampling weight.
            base_forest: The base forest upon which the training will continue.
            random_seed: the random seed.
            num_threads: The number of threads to run training with.
    Outputs: The forest.
    """
    try:
    except ImportError:
        raise ImportError('Please install json.')

    feature_set = set(features)
    float_features = [col for col in data_store.bucketized_float_cols() if str(col) in feature_set]
    cat_features = [col for col in data_store.string_cols() if col in feature_set]
    unfound_features = feature_set - (set(float_features) | set(cat_features))
    if len(unfound_features) > 0:
        raise ValueError('Failed to find feature {} in data store.'.format(unfound_features))

    config['float_feature'] = float_features
    config['categorical_feature'] = cat_features
    if 'example_sampling_rate' not in config:
        config['example_sampling_rate'] = 1
    if 'feature_sampling_rate' not in config:
        config['feature_sampling_rate'] = 1


    return Forest(libgbdt.train(data_store._data_store,