Skip to content

feat(core): implement nep13 and nep18 to allow sklearn integration - #415

Open
maartenbreddels wants to merge 3 commits into
masterfrom
feat_nep13_nep18_sklearn
Open

maartenbreddels wants to merge 3 commits into
masterfrom
feat_nep13_nep18_sklearn

Conversation

@maartenbreddels

Copy link
Copy Markdown
Member

This is a WIP on having the vaex dataframe and expressions behave more like 2d and 1d numpy arrays. This allows us to feed sklearn's fit a vaex dataframe, and have transform also return a dataframe with virtual columns. This gives us

  • A better API for vaex dataframes, since it's more like numpy
  • Leverage the sklearn algorithms, and turn them into vaex expressions/virtual columns
  • JIT the result of sklearn transforms, so we can use CUDA or numba to optimize them.
  • Out of core support for sklearn, since we don't keep anything in memory, it's all streaming algos (at least for the transformers tested here).

Numpy NEPs:

Example usage (5x faster):
image

TODO

  • increase coverage of numpy function (for instance to allow PCA?)
  • Test coverage
  • See if we can get sklearn's check_array to allow a vaex dataframe to pass through.

@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from e19e72f to 86bfcc3 Compare October 18, 2019 14:26
@JovanVeljanoski
JovanVeljanoski force-pushed the feat_nep13_nep18_sklearn branch from e60e43c to 5f90b49 Compare October 20, 2019 13:29
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch 2 times, most recently from bce691c to b159db9 Compare October 21, 2019 15:34
@JovanVeljanoski JovanVeljanoski added this to the 3.0 milestone Dec 24, 2019
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from f571cfd to 84f0999 Compare January 6, 2020 11:44
@maartenbreddels maartenbreddels changed the title WIP feat(core): implement nep13 and nep18 to allow sklearn integration feat(core): implement nep13 and nep18 to allow sklearn integration Jan 6, 2020
@maartenbreddels
maartenbreddels marked this pull request as ready for review January 6, 2020 16:23
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from 408c444 to f85c0c2 Compare January 6, 2020 16:24
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from fec686e to 6fbd934 Compare January 28, 2020 15:59
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from 6fbd934 to 68c2142 Compare January 31, 2020 08:25
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch 3 times, most recently from da09f05 to 4955df2 Compare April 15, 2020 17:34
@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch 2 times, most recently from dff413a to 4a753a7 Compare April 26, 2020 20:05
@JovanVeljanoski

Copy link
Copy Markdown
Member

Something that is probably missing is right now is how strings are handled in this scenario, especially with interaction to scikit-learn. For instance:

import vaex
from sklearn.preprocessing import LabelEncoder, OrdinalEncoder

df = vaex.from_arrays(x=['dog', 'dog', 'cat'], 
                      y=['white', 'golden', 'black'])

with sklearn_patch(), df.array_casting_disabled():
    enc = OrdinalEncoder()
    enc.fit_transform(df[['x', 'y']])

The same happens when using LabelEncoder. Can we support this at all?

I was gonna write this as a unit-test but I was not sure how to fold it in with the rest of the tests..

@JovanVeljanoski

Copy link
Copy Markdown
Member

As this PR is heavily geared towards scikit-learn compatibility, currently the ColumnTransformer does not seem to work (but the Pipline does!). Here is a simple example:

import vaex
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import PolynomialFeatures

x = np.random.random_sample(size=10)
y = np.random.randint(0, 15, size=10)
df = vaex.from_arrays(x=x, y=y)

pca = PCA(n_components=2, svd_solver='full', random_state=42)
poly = PolynomialFeatures(degree=2)

# transformers -> List of (name, transformer, column(s)
# Option 1: specify columns by names
col_trans = ColumnTransformer(transformers=[
    ('pca', pca, ['x', 'y']),
    ('polynomials', poly, ['x', 'y']),
])

with sklearn_patch(), df.array_casting_disabled():
    trans = col_trans.fit_transform(df)

So this will not work because ColumnTransformer expects a pandas DataFrame if the columns are specified with names.

Alternatively one can use column indices:

col_trans = ColumnTransformer(transformers=[
    ('pca', pca, [0, 1]),
    ('polynomials', poly, [0, 1]),
])

with sklearn_patch(), df.array_casting_disabled():
    trans = col_trans.fit_transform(df)

And this will fail for a different reason.

Or perhaps you have imagined the usage to be bit different and we should not rely on ColumnTransformer. This is absolutely fine with me! Just that the intended usage should be described in some detail somewhere.

For reference, creating a pipeline like this works just fine! :

pca = PCA(n_components=2, svd_solver='full', random_state=42)
poly = PolynomialFeatures(degree=2)

pca_ply_pipe = Pipeline(steps=[('pca', pca),
                               ('polynomial_features', poly
                              ])
with sklearn_patch(), df.array_casting_disabled():
    trans = pca_ply_pipe.fit_transform(df)

@JovanVeljanoski

Copy link
Copy Markdown
Member

But overall i really really like the direction this is going :)

@maartenbreddels
maartenbreddels force-pushed the feat_nep13_nep18_sklearn branch from 4a753a7 to 8e84ec3 Compare April 29, 2020 07:21
@maartenbreddels maartenbreddels removed this from the 3.0 milestone May 22, 2020
@maartenbreddels maartenbreddels added this to the 3.1 milestone May 22, 2020

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants