Skip to content

sklearn.cluster.OPTICS uses all cores when minkowski's p=2 #21786

Open

Description

@szperajacyzolw

Describe the bug

I have specified n_jobs=2. When p=1, joblib backend is working as specified. But when p=2 it ignores n_jobs and goes full power.

Steps/Code to Reproduce

optics_oh = OPTICS(min_samples=3, p=2, n_jobs=2)
optics_oh_predict = optics_oh.fit_predict(X_oh)

Expected Results

Two cores loaded

Actual Results

All cores loaded
Screenshot from 2021-11-25 15-23-14

Versions

System:
python: 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
executable: /home/some_folder_structure/env/bin/python
machine: Linux-5.11.0-40-generic-x86_64-with-glibc2.29

Python dependencies:
pip: 21.3.1
setuptools: 58.2.0
sklearn: 0.24.2
numpy: 1.21.4
scipy: 1.5.4
Cython: 0.29.22
pandas: 1.2.2
matplotlib: 3.3.4
joblib: 1.0.1
threadpoolctl: 2.1.0

Built with OpenMP: True

Activity

  1. adrinjalali commented on Nov 25, 2021

    @adrinjalali
    Member

    Could you please reproduce with scikit-learn 1.0.1, and also paste a reproducible code, which can be copy pasted to reproduce the issue?

  2. szperajacyzolw commented on Nov 25, 2021

    @szperajacyzolw
    Author
    import pandas as pd
    import os
    import numpy as np
    from category_encoders.one_hot import OneHotEncoder
    from sklearn.cluster import OPTICS
    
    seed = 33
    
    df = pd.read_csv('some_csv_with_many_categorical_features.csv')
    
    oh_encoder = OneHotEncoder(verbose=1, use_cat_names=True, cols=["some", "cols"])
    X_oh = oh_encoder.fit_transform(df)
    
    optics_oh = OPTICS(min_samples=3, p=2, n_jobs=2)
    optics_oh_predict = optics_oh.fit_predict(X_oh)
    
    

    Running it with scikit-learn 1.0.1 gives the same result in terms of cores usage

    Below version works just fine:

    
    optics_oh = OPTICS(min_samples=3, p=1, n_jobs=2)
    optics_oh_predict = optics_oh.fit_predict(X_oh)
    
    
  3. adrinjalali commented on Nov 25, 2021

    @adrinjalali
    Member

    Could you please instead of a loading a csv, use a synthetic dataset, or a dataset fetched by fetch_openml, so that we can run and test it?

  4. szperajacyzolw commented on Nov 26, 2021

    @szperajacyzolw
    Author

    Here I have created synthetic set similar to what I have used in real scenario.

    import pandas as pd
    import numpy as np
    from category_encoders.one_hot import OneHotEncoder
    from sklearn.cluster import OPTICS
    from sklearn.datasets import make_classification
    
    seed = 33
    
    def make_class(x: float) -> str:
        if x <-1:
            return 'c0'
        if -1 <= x < 1:
            return 'c1'
        else:
            return 'c2'
    
    arr, _ = make_classification(n_samples=50000, n_features=25, n_informative=15, n_redundant=3, scale=1)
    arr = np.vectorize(make_class)(arr)
    
    df = pd.DataFrame(arr, dtype=str)
    oh_encoder = OneHotEncoder(verbose=1, use_cat_names=True, cols=df.columns)
    X_oh = oh_encoder.fit_transform(df)
    
    optics_oh = OPTICS(min_samples=3, p=2, n_jobs=2)
    optics_oh_predict = optics_oh.fit_predict(X_oh)
    

    Screenshot from 2021-11-26 09-16-48

  5. adrinjalali commented on Nov 29, 2021

    @adrinjalali
    Member

    maybe @jeremiedbb would have an idea?

  6. jeremiedbb commented on Nov 29, 2021

    @jeremiedbb
    Member

    minkowski with p=2 relies on BLAS functions to compute the distances which is multithreaded.

    n_jobs only controls joblib based parallelism. Other sources of parallelism will likely try to use as many cores as possible. It also happens when we call numpy or scipy functions that rely on BLAS: parallelism is handled by the BLAS library linked.

    So I don't think it's a bug, but maybe a lack of documentation.

    If you really want to use at most 2 cores, you need to set the env var OPENBLAS_NUM_THREADS or MKL_NUM_THREADS depending on your blas. You can find out what blas is linked by running python -m threadpoolctl -i sklearn

    You can also limit the number of threads programmatically with threadpoolctl:

    from threadpoolctl import threadpool_limits
    with threadpool_limits(limits=2, user_api="blas"):
        # your code here
  7. adrinjalali commented on Nov 29, 2021

    @adrinjalali
    Member

    Thanks @jeremiedbb , I feel like we need a clear documentation about these with links to them from virtually everywhere, so that people can figure out how to limit/use their resources as they want.

  8. szperajacyzolw commented on Nov 29, 2021

    @szperajacyzolw
    Author

    Thank you @jeremiedbb for insightful explanation!

  9. glemaitre commented on Nov 29, 2021

    @glemaitre
    Member

    I would retag this issue as documentation. I was going to propose to have an entry in the FAQ but we already have it:

    https://scikit-learn.org/stable/faq.html#why-does-my-job-use-more-cores-than-specified-with-n-jobs

    So, the part of the documentation that could be improved would be the following:
    https://scikit-learn.org/stable/computing/parallelism.html#parallelism

    where we could have some usage of threadpooltctl.

  10. added
    Moderate
    on Nov 29, 2021
  11. adrinjalali commented on Nov 29, 2021

    @adrinjalali
    Member

    I think our n_jobs explanation can also be improved. We make no explicit mention of BLAS or openmp threads there, and only have a link to glossary, which then has a link to our parallelism docs. I think this information should be available in a bit more direct and explicit way to the users.

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

    DocumentationModeratemodule:cluster

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions