Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] ENH: Improves speed of one hot encoding #15762

Merged
merged 3 commits into from Dec 4, 2019

Conversation

thomasjpfan
Copy link
Member

This PR improves the time it takes to run one hot encoding by about 24%.

Benchmark script

from itertools import product
import neurtu
from sklearn.preprocessing import OneHotEncoder
import numpy as np


def cases():
    rng = np.random.RandomState(42)

    n_features = [50, 100]
    n_samples = [100_000, 200_000, 500_000]

    for n_samples, n_features in product(n_samples, n_features):
        X = rng.randint(0, 75, size=(n_samples, n_features))
        tags = {'n_samples': n_samples, 'n_features': n_features}
        enc = OneHotEncoder(categories='auto')
        yield neurtu.delayed(enc, tags=tags).fit_transform(X)


bench = neurtu.Benchmark(wall_time=True, cpu_time=True, to_dataframe=True)
df = bench(cases())
print(df)

This PR

                      wall_time  cpu_time
n_samples n_features
100000    50           0.531036  1.808001
          100          1.179558  2.429228
200000    50           1.355601  2.621355
          100          2.950725  4.143948
500000    50           3.646070  4.799419
          100          7.739459  8.745608

Master

                      wall_time   cpu_time
n_samples n_features
100000    50           0.728843   2.058129
          100          1.598936   3.000086
200000    50           1.825290   3.265459
          100          3.882629   5.505839
500000    50           4.915225   6.444236
          100         10.473135  12.773251

CC @jorisvandenbossche

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Copy link
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

indptr[0] = 0
np.sum(X_mask, axis=1, out=indptr[1:])
np.cumsum(indptr[1:], out=indptr[1:])
data = np.ones(indptr[-1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last line is the only significant change, right? Or do the other changes also matter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They all matter. If I revert the last line, I get:

                      wall_time   cpu_time                                                                                                      
n_samples n_features                      
100000    50           0.575167   1.999209
          100          1.244284   2.790776
200000    50           1.424922   2.935314
          100          3.083203   4.711901
500000    50           3.762984   5.473710
          100          7.914155  10.137310

@glemaitre glemaitre merged commit 69884d5 into scikit-learn:master Dec 4, 2019
@glemaitre
Copy link
Contributor

Thanks @thomasjpfan

panpiort8 pushed a commit to panpiort8/scikit-learn that referenced this pull request Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants