'method' must be 'barnes_hut' or 'exact'
Package:
scikit-learn
47032

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
🙏 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.
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.
Links to the raise (1)
https://github.com/scikit-learn/scikit-learn/blob/c67518350f91072f9d37ed09c5ef7edf555b6cf6/sklearn/manifold/_t_sne.py#L717Ways to fix
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)
Add a possible fix
Please authorize to post fix