scikit-learn/scikit-learn — pull request #3144 by mjbommar (2014-05-11 14:50:32+00:00)
Title: Adding dense pipeline support to RandomTreesEmbedding by adding a return_dense argument

Hello,
Currently, you can't use RandomTreesEmbedding within a pipeline if the following classes expect dense input. For example, this pipeline will fail:

```
model_pipeline = sklearn.pipeline.Pipeline([
('scale', sklearn.preprocessing.StandardScaler()),
('feature_select', sklearn.feature_selection.SelectPercentile(sklearn.feature_selection.f_classif)),
('dim_red', sklearn.ensemble.RandomTreesEmbedding()),
('classify', sklearn.ensemble.ExtraTreesRegressor())
])
```

With this exception:
```
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.
```

To make this more convenient, I've added a return_dense argument to RandomTreesEmbedding that preserves default sklearn behavior, but allows the pipeline to behave properly with subsequent dense-expecting classes as follows:

```
model_pipeline = sklearn.pipeline.Pipeline([
('scale', sklearn.preprocessing.StandardScaler()),
('feature_select', sklearn.feature_selection.SelectPercentile(sklearn.feature_selection.f_classif)),
('dim_red', sklearn.ensemble.RandomTreesEmbedding(return_dense=True)),
('classify', sklearn.ensemble.ExtraTreesRegressor())
])
```