'Problem with importing softmax from scipy and using it once imported from sklearn
I have installed scipy using conda.
When I try to import softmax from scipy I get an error:
from scipy.special import softmax
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-34-35eed14e1f88> in <module>
----> 1 from scipy.special import softmax
ImportError: cannot import name 'softmax' from 'scipy.special' (C:\Users\Alienware\Anaconda3\envs\tf2\lib\site-packages\scipy\special\__init__.py)
On the other hand I can import softmax from sklearn but then I get an exception when I try to put it in use:
from sklearn.utils.extmath import softmax
X = np.array([[2, 3], [4,5]])
softmax(X)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-781ae2561cff> in <module>
1 X = np.array([[2, 3], [4,5]])
----> 2 softmax(X)
~\Anaconda3\envs\tf2\lib\site-packages\sklearn\utils\extmath.py in softmax(X, copy)
597 max_prob = np.max(X, axis=1).reshape((-1, 1))
598 X -= max_prob
--> 599 np.exp(X, X)
600 sum_prob = np.sum(X, axis=1).reshape((-1, 1))
601 X /= sum_prob
TypeError: ufunc 'exp' output (typecode 'd') could not be coerced to provided output parameter (typecode 'l') according to the casting rule ''same_kind''
Solution 1:[1]
The first part of your question is probably answered by the comment and boils down to the version of SciPy you're using simply being one that doesn't include softmax. For the second part, the error message suggests that it's failing to convert a double to a long; you can get around this by simply using only doubles in your input:
In [13]: softmax(X.astype(np.double))
Out[13]:
array([[0.26894142, 0.73105858],
[0.26894142, 0.73105858]])
That softmax does not work with integers is also apparent from the documentation and is by design:
Parameters
X : array-like of floats, shape (M, N) Argument to the logistic function
Solution 2:[2]
I solved this problem by upgrading scikit-learn and scipy to the newest version:
pip install --upgrade scikit-learn scipy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Stanis?aw Matuszewski |
