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

MAINT use composition in TableVectorizer contn'd #761

Merged
merged 16 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ Major changes
Minor changes
-------------

* :class:`TableVectorizer` is now able to apply parallelism at the column level rather than the transformer level. This is the default for univariate transformers, like :class:`MinHashEncoder`, and :class:`GapEncoder`.
:pr:`592` by :user:`Leo Grinsztajn <LeoGrin>`
* :class:`TableVectorizer` propagate the `n_jobs` parameter to the underlying
transformers except if the underlying transformer already set explicitly `n_jobs`.
:pr:`761` by :user:`Leo Grinsztajn <LeoGrin>`, :user:`Guillaume Lemaitre <glemaitre>`,
and :user:`Jerome Dockes <jeromedockes>`.

* Parallelized the :func:`deduplicate` function. Parameter `n_jobs`
added to the signature. :pr:`618` by :user:`Jovan Stojanovic <jovan-stojanovic>`
Expand Down
39 changes: 1 addition & 38 deletions skrub/_gap_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from numpy.random import RandomState
from numpy.typing import ArrayLike, NDArray
from scipy import sparse
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.cluster import KMeans, kmeans_plusplus
from sklearn.decomposition._nmf import _beta_divergence
from sklearn.feature_extraction.text import CountVectorizer, HashingVectorizer
Expand Down Expand Up @@ -740,43 +740,6 @@ class GapEncoder(TransformerMixin, BaseEstimator):
fitted_models_: list[GapEncoderColumn]
column_names_: list[str]

@classmethod
def _merge(cls, transformers_list: list[GapEncoder]):
"""
Merge GapEncoders fitted on different columns
into a single GapEncoder. This is useful for parallelization
over columns in the TableVectorizer.
"""
full_transformer = clone(transformers_list[0])
# assert rho_ is the same for all transformers
rho_ = transformers_list[0].rho_
full_transformer.rho_ = rho_
full_transformer.fitted_models_ = []
for transformers in transformers_list:
full_transformer.fitted_models_.extend(transformers.fitted_models_)
if hasattr(transformers_list[0], "column_names_"):
full_transformer.column_names_ = []
for transformers in transformers_list:
full_transformer.column_names_.extend(transformers.column_names_)
return full_transformer

def _split(self):
"""
Split a GapEncoder fitted on multiple columns
into a list of GapEncoders fitted on one column each.
This is useful for parallelizing transform over columns
in the TableVectorizer.
"""
check_is_fitted(self)
transformers_list = []
for i, model in enumerate(self.fitted_models_):
transformer = clone(self)
transformer.rho_ = model.rho_
transformer.fitted_models_ = [model]
transformer.column_names_ = [self.column_names_[i]]
transformers_list.append(transformer)
return transformers_list

def __init__(
self,
*,
Expand Down
45 changes: 2 additions & 43 deletions skrub/_minhash_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import numpy as np
from joblib import Parallel, delayed, effective_n_jobs
from numpy.typing import ArrayLike, NDArray
from sklearn.base import BaseEstimator, TransformerMixin, clone
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils import gen_even_slices, murmurhash3_32
from sklearn.utils.validation import _check_feature_names_in, check_is_fitted

from ._fast_hash import ngram_min_hash
from ._string_distances import get_unique_ngrams
from ._utils import LRUDict, check_input, combine_lru_dicts
from ._utils import LRUDict, check_input

NoneType = type(None)

Expand Down Expand Up @@ -119,47 +119,6 @@ class MinHashEncoder(TransformerMixin, BaseEstimator):

_capacity: int = 2**10

@classmethod
def _merge(cls, transformers_list: list[MinHashEncoder]):
"""
Merge MinHashEncoders fitted on different columns
into a single MinHashEncoder. This is useful for parallelization
over columns in the TableVectorizer.
"""
full_transformer = clone(transformers_list[0])
capacity = transformers_list[0]._capacity
full_transformer.hash_dict_ = combine_lru_dicts(
capacity, *[transformer.hash_dict_ for transformer in transformers_list]
)
full_transformer.n_features_in_ = sum(
transformer.n_features_in_ for transformer in transformers_list
)
full_transformer.feature_names_in_ = np.concatenate(
[transformer.feature_names_in_ for transformer in transformers_list]
)
return full_transformer

def _split(self):
"""
Split a MinHashEncoder fitted on multiple columns
into a list of MinHashEncoders (one for each column).
This is useful for parallelizing transform over columns
in the TableVectorizer.
"""
check_is_fitted(self)
transformer_list = []
for i in range(self.n_features_in_):
trans = clone(self)
attributes = ["hash_dict_", "_capacity"]
for a in attributes:
if hasattr(self, a):
setattr(trans, a, getattr(self, a))
# TODO; do we want to deepcopy hash_dict_
trans.n_features_in_ = 1
trans.feature_names_in_ = np.array([self.feature_names_in_[i]])
transformer_list.append(trans)
return transformer_list

def __init__(
self,
*,
Expand Down
Loading
Loading