votes up 6

'method' must be 'barnes_hut' or 'exact'

Package:
Exception Class:
ValueError

Raise code

            self._learning_rate = self.learning_rate

        if isinstance(self._init, str) and self._init == 'pca' and issparse(X):
            raise TypeError("PCA initialization is currently not suported "
                            "with the sparse input matrix. Use "
                            "init=\"random\" instead.")
        if self.method not in ['barnes_hut', 'exact']:
            raise ValueError("'method' must be 'barnes_hut' or 'exact'")
        if self.angle < 0.0 or self.angle > 1.0:
            raise ValueError("'angle' must be between 0.0 - 1.0")
        if self.square_distances not in [True, 'legacy']:
            raise ValueError("'square_distances' must be True or 'legacy'.")
        if self._learning_rate == 'auto':
            # See issue #18018
            self._learning_rate = X.shape[0] / self.early_exaggeration / 4
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 1 votes down

Summary:

This exception occurs when creating an instance of TNSE. There are several optional parameters that can be adjusted in the init function. One of them, in particular, that can lead to this exception is the method parameter. This parameter can only be equal to one of two strings: "barnes_hut" and "exact". By default, it is set "barnes_hut". If it is set to any other string, you will get this exception.

Code to Reproduce the Error (Wrong):

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

x = np.array([[1,2], [3,4]])
methodName = 'ex'
t = TSNE(method=methodName)
t._fit(x)

Error Message:

ValueError                                Traceback (most recent call last)
<ipython-input-35-ccd54bee38a3> in <module>()
      5 methodName = 'ex'
      6 t = TSNE(method=methodName)
----> 7 t._fit(x)

/usr/local/lib/python3.7/dist-packages/sklearn/manifold/_t_sne.py in _fit(self, X, skip_num_points)
    659 
    660         if self.method not in ['barnes_hut', 'exact']:
--> 661             raise ValueError("'method' must be 'barnes_hut' or 'exact'")
    662         if self.angle < 0.0 or self.angle > 1.0:
    663             raise ValueError("'angle' must be between 0.0 - 1.0")

ValueError: 'method' must be 'barnes_hut' or 'exact'

Working Version (Right):

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

x = np.array([[1,2], [3,4]])
methodName = 'exact'
t = TSNE(method=methodName)
t._fit(x)
Jul 18, 2021 codingcomedyig answer

Add a possible fix

Please authorize to post fix