votes up 5

'n_components' should be inferior to 4 for the barnes_hut algorithm as it relies on quad-tree or oct-tree.

Package:
Exception Class:
ValueError

Raise code

if self.method == "exact" and issparse(X):
                raise TypeError(
                    'TSNE with method="exact" does not accept sparse '
                    'precomputed distance matrix. Use method="barnes_hut" '
                    'or provide the dense distance matrix.')

        if self.method == 'barnes_hut' and self.n_components > 3:
            raise ValueError("'n_components' should be inferior to 4 for the "
                             "barnes_hut algorithm as it relies on "
                             "quad-tree or oct-tree.")
        random_state = check_random_state(self.random_state)

        if self.early_exaggeration < 1.0:
            raise ValueError("early_exaggeration must be at least 1, but is {}"
                             .format(self.early_exaggeration))

        if 
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

Summary:

This exception is thrown when creating an instance of TSNE. The init function of this class has loads of optional parameters, and there are 2 contribute to getting this exception: method and n_components. By default n_components is set to 2 and method is set to "barnes_hut". Therefore if the value of n_components is altered and set to a value of something larger than 3, you will get this exception because it must be less than 4. This is only the case if method is unaltered. If the method is set to a different acceptable value, then the value of n-components can exceed 3.

Code to Reproduce the Error (Wrong):

from sklearn.manifold._t_sne import TSNE
import numpy as np

x = np.array([[1,2], [3,4]])
comps = 4
method = 'barnes_hut'
t = TSNE(n_components=comps, method=method)
t._fit(x)

Error Message:

ValueError                                Traceback (most recent call last)
<ipython-input-12-36d5cbec3f1a> in <module>()
      6 method = 'barnes_hut'
      7 t = TSNE(n_components=comps, method=method)
----> 8 t._fit(x)

/usr/local/lib/python3.7/dist-packages/sklearn/manifold/_t_sne.py in _fit(self, X, skip_num_points)
    685 
    686         if self.method == 'barnes_hut' and self.n_components > 3:
--> 687             raise ValueError("'n_components' should be inferior to 4 for the "
    688                              "barnes_hut algorithm as it relies on "
    689                              "quad-tree or oct-tree.")

ValueError: 'n_components' should be inferior to 4 for the barnes_hut algorithm as it relies on quad-tree or oct-tree.

Working Version (Right):

from sklearn.manifold._t_sne import TSNE
import numpy as np

x = np.array([[1,2], [3,4]])
comps = 2
method = 'barnes_hut'
t = TSNE(n_components=comps, method=method)
t._fit(x)

Success Message:

array([[ 5812.693 , -2521.9004],
       [-5812.693 ,  2521.9004]], dtype=float32)
Jul 16, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix