Skip to content

Distributed TFIDF #5

Open
Open

Description

@dsevero

Greetings!

I recently used dask to implement a distributed version of tfidf. I want to contribute to the dask project by putting it somewhere.

Would this be the correct repo.?

I thought maybe a feature_extraction directory would be appropriate.

Activity

  1. TomAugspurger commented on Sep 25, 2017

    @TomAugspurger
    Member

    Things are still a bit in flux, but I think this would be a good home. This would be a great addition!

    I thought maybe a feature_extraction directory would be appropriate.

    Perfect. I'm trying to follow scikit-learn's layout as closely as possible.

  2. dsevero commented on Sep 26, 2017

    @dsevero
    ContributorAuthor

    I see.

    What's the difference between this repo. and dask-glm? Can't the later be a subset of the former?

  3. TomAugspurger commented on Sep 26, 2017

    @TomAugspurger
    Member

    What's the difference between this repo. and dask-glm?

    I'm probably going to just import the dask-glm estimators into dask-ml namespace (likewise with dask-searchcv, dask-patternsearch). For the user, it'd be nice to have a single place to go for all dask-related ML things.

    Development will probably still continue in those other repositories.

  4. bendruitt commented on Oct 20, 2017

    @bendruitt

    I'd be interested in seeing this code. How was this achieved?

  5. dsevero commented on Oct 20, 2017

    @dsevero
    ContributorAuthor

    The way I first implemented it was something like this: (I changed it around so that I removed company private code and this was way before I started contributing to dask so my knowledge was far less than it is today)

    import numpy as np
    import dask.bag as db
    
    from toolz.curried import frequencies
    from toolz.curried import valmap
    from toolz.curried import merge_with
    from toolz.curried import unique
    from toolz import merge_with as _merge_with
    
    
    def normalize(_dict):
        return {k:v/sum(_dict.values()) for k,v in _dict.items()}
    
    corpus = 2*[
        "a a b b b",
        "a a a a c",
        "c b a d e f g",
        "a b b b c c c d d d a b",
        "c b a d e f g",
        "a b b b c c c d d d a b"
    ]
    
    
    base = (
        db
        .from_sequence(corpus, npartitions=4)
        .map(str.split)
    )
    
    tf = (
        base
        .map(frequencies)
        .map(normalize)
    )
    
    idf = (
        base
        .map(unique)
        .map(frequencies)
        .reduction(merge_with(sum), merge_with(sum), split_every=2)
        .apply(normalize)
        .apply(valmap(np.log10))
        .apply(valmap(np.negative))
    )

    This has a draw-back, since it must pass over the entire dataset once in order to calculate the idf step.

    To me a better solution (which is what I want to implement here) would be first to implement the hashing trick and then use the tfidf.

  6. bendruitt commented on Oct 20, 2017

    @bendruitt
  7. dsevero commented on Oct 20, 2017

    @dsevero
    ContributorAuthor

    Cool.

    The problem I faced when I decided to implement this was similar to yours. Our implementation originally used the get_dummies function in pandas on the text columns.

  8. bendruitt commented on Oct 20, 2017

    @bendruitt

    Yep. I've used the get_dummies function to process my categorical columns using map_partitions function on a dask dataframe since marking the input columns as categorical handles the schema merging per partition. What strategy does your original problem implement to form partitions with consistent partition schemas?

  9. rth commented on Jan 24, 2018

    @rth
    Contributor

    An alternative approach to using dask bag could be to apply scikit-learn CountVectorizer or HashingVectorizer on chunks of the dataset, merge the results and then apply IDF weighting (see FreeDiscovery/FreeDiscovery#152). It might require somewhat less work since different vectorization options are already implemented in scikit-learn, and it should be possible to keep a fairly compatible API.

  10. mrocklin commented on Jan 24, 2018

    @mrocklin
    Member
  11. added
    Algorithm
    on May 22, 2018
  12. mrocklin commented on Sep 24, 2020

    @mrocklin
    Member
  13. cakiki commented on Jun 2, 2022

    @cakiki

    Sorry to revive an old thread, but was this implemented anywhere?

  14. pr38 commented on Mar 4, 2025

    @pr38

    I have written a dask native implementation of tf_idf here: https://github.com/pr38/dask_tfidf, using only dask's array api. There are a couple of silly things I did, like filter on none-0 on a sparse array. I am also unsure if its worth keeping the norm stuff that is found in the scikit learn implementation.

    I have thought about folding it into dask-ml. I may need some guidance on how to do that.

  15. stsievert commented on Mar 5, 2025

    @stsievert
    Member

    There's a contribution guide here: https://ml.dask.org/contributing.html

    Let us know if you have questions or run into difficulties.

  16. pr38 commented on Mar 12, 2025

    @pr38

    ok, my pr to add tfidf is here: #1014

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Algorithm

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions