Error while importing scipy.spatial.distance in python - python

While I am running this code in ubuntu 14.04, I want to calculate the cosine distance of an array with scipy.spatial.distance. But it is throwing me an error in importing scipy.spatial.distance.
import numpy as np
import scipy
from scipy.spatial.distance import pdist
x=np.array([[[-0.22008298, 0.28819615, 0.09895946, 0.16789497, 0.02379252,
-0.25700486, 0.1628186, 0.12296761, 0.01750283]],
[[ 0.29891527, 0.17249978, -0.10084602, -0.16522293, 0.06811064,
0.07434326, -0.19697316, -0.22199543, 0.07647966]],
[[-0.04112297, 0.13392878, -0.06575392, 0.0893533, 0.22577047,
0.00289264, 0.09996213, 0.17324321, 0.08673526]]])
B =x.transpose(1,2,0).reshape(-1,x.shape[0])
print(B.shape)
print (B.ndim)
#array.astype(int)
print(B)
y=1-(pdist(B, metric='cosine'))
print(y)
It is throwing this error,
Traceback (most recent call last):
File "cos1.py", line 3, in <module>
from scipy.spatial.distance import pdist
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/spatial/__init__.py", line 94, in <module>
from .kdtree import *
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/spatial/kdtree.py", line 8, in <module>
import scipy.sparse
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/sparse/__init__.py", line 229, in <module>
from .csr import *
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/sparse/csr.py", line 20, in <module>
from .compressed import _cs_matrix
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/sparse/compressed.py", line 11, in <module>
from scipy._lib._util import _prune_array
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/_lib/_util.py", line 9, in <module>
import inspect
File "/usr/lib/python2.7/inspect.py", line 37, in <module>
import dis
File "/home/mayesha/dis.py", line 2, in <module>
from scipy.spatial.distance import pdist
File "/home/mayesha/venv/local/lib/python2.7/site-packages/scipy/spatial/distance.py", line 119, in <module>
from scipy._lib._util import _asarray_validated
ImportError: cannot import name _asarray_validated
I am using virtual environment with pip.
Can someone help me. Thanks in advance.

pip install -U scipy
This will solve your issue. I have tried and it did for me. You might get an error that scikit-learn is not installed properly. If you get that error, you can first run:
pip install -U scikit-learn
and then rerun the above code.

Related

Import numpy not going good?

When I import numpy it sends a bunch of errors
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.local/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/user/.local/lib/python2.7/site-packages/numpy/core/__init__.py", line 59, in <module>
from . import numeric
File "/home/user/.local/lib/python2.7/site-packages/numpy/core/numeric.py", line 3093, in <module>
from . import fromnumeric
File "/home/user/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 17, in <module>
from . import _methods
File "/home/user/.local/lib/python2.7/site-packages/numpy/core/_methods.py", line 158, in <module>
_NDARRAY_ARRAY_FUNCTION = mu.ndarray.__array_function__
AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'
The easiest solution would be to reinstall numpy.
Open command prompt and type:
pip3 uninstall numpy
or
pip uninstall numpy
for python 2,
then,
pip install numpy

SVM import failed after installing scikit-neuralnetwork

I just installed Python's scikit-neuralnetwork using the procedure mentioned, that is;
pip install scikit-neuralnetwork
Now I can not import SVM. This line;
from sklearn import svm
gives this error;
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "//anaconda/lib/python2.7/site-packages/sklearn/svm/__init__.py", line 13, in <module>
from .classes import SVC, NuSVC, SVR, NuSVR, OneClassSVM, LinearSVC, \
File "//anaconda/lib/python2.7/site-packages/sklearn/svm/classes.py", line 4, in <module>
from .base import _fit_liblinear, BaseSVC, BaseLibSVM
File "//anaconda/lib/python2.7/site-packages/sklearn/svm/base.py", line 12, in <module>
from ..multiclass import _ovr_decision_function
File "//anaconda/lib/python2.7/site-packages/sklearn/multiclass.py", line 44, in <module>
from .metrics.pairwise import euclidean_distances
File "//anaconda/lib/python2.7/site-packages/sklearn/metrics/__init__.py", line 33, in <module>
from . import cluster
File "//anaconda/lib/python2.7/site-packages/sklearn/metrics/cluster/__init__.py", line 21, in <module>
from .bicluster import consensus_score
File "//anaconda/lib/python2.7/site-packages/sklearn/metrics/cluster/bicluster/__init__.py", line 1, in <module>
from .bicluster_metrics import consensus_score
File "//anaconda/lib/python2.7/site-packages/sklearn/metrics/cluster/bicluster/bicluster_metrics.py", line 6, in <module>
from sklearn.utils.validation import check_arrays
ImportError: cannot import name check_arrays
May I know what went wrong? I'll try using different environments next time, if that helps. Is there any way to fix this?
I am using Python 2.7 and sklearn 0.17.
open sklearn.utils.validation and delete "import check_arrays", or use other package for example anaconda3. i check now - no problems. Or install anaconda3.5 and copy check_arrays to anaconda2.7 sklearn.utils.validation.

pip install of statsmodels is not working. DLL load failed: The specified module could not be found

I am really new to python and I am trying to install the statsmodels module. It seems that since I am on a 64 bits I need to use the following unofficial website http://www.lfd.uci.edu/ I did all I know about how to download a whl and I did download it and used the pip afterwards and all work fine. now when I do pip install statsmodels it work fine. However, in my shell after I try import the stats models I am getting
import statsmodels
Traceback (most recent call last): File "<pyshell#7>", line 1, in <module>
import statsmodels File "D:\Python34\lib\site-packages\statsmodels\__init__.py", line 8, in <module>
from .tools.sm_exceptions import (ConvergenceWarning, CacheWriteWarning, File "D:\Python34\lib\site-packages\statsmodels\tools\__init__.py", line 1, in <module>
from .tools import add_constant, categorical File "D:\Python34\lib\site-packages\statsmodels\tools\tools.py", line 8, in <module>
from scipy.linalg import svdvals File "D:\Python34\lib\site-packages\scipy\linalg\__init__.py", line 174, in <module>
from .misc import * File "D:\Python34\lib\site-packages\scipy\linalg\misc.py", line 5, in <module>
from .blas import get_blas_funcs File "D:\Python34\lib\site-packages\scipy\linalg\blas.py", line 155, in <module>
from scipy.linalg import _fblas ImportError: DLL load failed: The specified module could not be found.
Any help would be much appreciated.
Thank you in advance

Script fails in WinPython, attempting to import lapack_til

I'm trying to run a script my client has provided, but I can't get it to start; it seems certainly a setup question on my machine, as it works for them. I started by installing WinPython from http://scipy.org/, but I'm now getting the following traceback:
Traceback (most recent call last):
File "pyconsole.py", line 18, in <module>
import cocomo
File "pyconsole\cocomo.py", line 13, in <module>
import grupple
File "pyconsole\grupple.py", line 4, in <module>
import matplotlib.mlab as mlab
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\matplotlib\__init__.py", line 134, in <module>
from matplotlib.rcsetup import (defaultParams,
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\matplotlib\rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\matplotlib\colors.py", line 52, in <module>
import numpy as np
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\__init__.py", line 137, in <module>
import add_newdocs
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\lib\__init__.py", line 13, in <module>
from polynomial import *
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\lib\polynomial.py", line 17, in <module>
from numpy.linalg import eigvals, lstsq, inv
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\linalg\__init__.py", line 48, in <module>
from linalg import *
File "C:\Users\lab\AppData\Roaming\Python\Python27\site-packages\numpy\linalg\linalg.py", line 23, in <module>
from numpy.linalg import lapack_lite
ImportError: DLL load failed: The specified module could not be found.
I'm not confident in interpreting Python tracebacks, but it seems that "lapack_lite" is missing? Searching around a bit seems surprisingly unhelpful; the best I've seemed to determine is that it is part of numpy? But that's odd, as the reason I started with the WinPython distribution is that the numpy stack is pre-installed, and that is the easiest way to get started with that.
What am I missing?
Thanks!
i think the easiest way under Windows would be to simply install Anaconda (this takes care of all the dependencies of Numpy and Scipy). You can download it from here:
https://www.continuum.io/downloads
This way you dont have to scroll through pages of Documentation on how to get all dependencies right.
Kind regards
Johann

What module is missing when I try to import skimage? Python 3.5

When I run the line from skimage import io I get the following:
Traceback (most recent call last):
File "C:\Users\Dilshad\Desktop\project_7-8-2015\8_bands\Program_camera.py", line 16, in <module>
from Functions_8bands import *
File "C:\Users\Dilshad\Desktop\project_7-8-2015\8_bands\Functions_8bands.py", line 5, in <module>
from skimage import io
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\io\__init__.py", line 11, in <module>
from ._io import *
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\io\_io.py", line 8, in <module>
from ..color import rgb2grey
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\color\__init__.py", line 1, in <module>
from .colorconv import (convert_colorspace,
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\color\colorconv.py", line 58, in <module>
from scipy import linalg
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\linalg\__init__.py", line 172, in <module>
from .misc import *
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\linalg\misc.py", line 5, in <module>
from .blas import get_blas_funcs
File "C:\Users\Dilshad\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\linalg\blas.py", line 155, in <module>
from scipy.linalg import _fblas
ImportError: DLL load failed: The specified module could not be found.
I've searched through the site-package file and I see that io exists so I'm not really sure whats's missing.
Not sure why it worked but installing numpy +mkl instead of numpy vanilla from the unofficial binary site on UCI got around this for me.

Categories

Resources