Error in importing module of python library scipy - python

I am working on clustering in python. The package I want to import is
from scipy.spatial.distance import dist
but this is showing the following error.
ImportError: cannot import name 'dist' from 'scipy.spatial.distance' (C:\Users\majid\Anaconda3\lib\site-packages\scipy\spatial\distance.py)

Instead of importing dist you need to import pdist or cdist based on your requirement as per the SciPy documentation:
pdist(X[, metric])
Pairwise distances between observations in n-dimensional space.
cdist(XA, XB[, metric])
Compute distance between each pair of the two collections of inputs.

Related

Pyamg don't work (No module named 'pyamg.amg_core.evolution_strength')

I want to use PyAMG (https://pyamg.readthedocs.io/en/latest/#).
I install it using (pip install pyamg)
I use spyder in windows. It gives error when I want to import it
I run it's example:
import pyamg
import numpy as np
A = pyamg.gallery.poisson((500,500), format='csr') # 2D Poisson problem on 500x500 grid
ml = pyamg.ruge_stuben_solver(A) # construct the multigrid hierarchy
print(ml) # print hierarchy information
b = np.random.rand(A.shape[0]) # pick a random right hand side
x = ml.solve(b, tol=1e-10) # solve Ax=b to a tolerance of 1e-10
print("residual: ", np.linalg.norm(b-A*x)) # compute norm of residual vector
The error is :
import pyamg.amg_core
File "C:\Users\Admin\AppData\Local\Programs\Spyder\pkgs\pyamg\amg_core_init_.py", line 5, in
from .evolution_strength import *
ModuleNotFoundError: No module named 'pyamg.amg_core.evolution_strength'
How can I resolve this issue?
Thanks in advance

How to import special.hermite from scipy?

I'm looking to import a hermite polynomial. When I check the documentation on the scipy website it's available. However, when I try to import it there is no module found
import scipy.special
p = scipy.special.hermite(63)
should work.

Python - cannot import `linalg`

I have this code
import scipy.sparse as sparse
import numpy as np
id = np.eye(13)
vals, vecs = sparse.linalg.eigsh(id, k=6)
vals
which is just the example code from the documentation here.
I am running it in a Python 2.7 console and I get the following error message:
AttributeError: 'module' object has no attribute 'linalg'
Does anyone know why this happens?
Try this code
import scipy.sparse.linalg as sp
import numpy as np
id = np.eye(13)
vals, vecs = sp.eigsh(id, k=6)
vals
This happens because linalg is a directory and not source code i.e it is a sub-package. And I guess this causes the issue because some of the Scipy sub modules do not have __init__.py, Maybe the devs did this to reduce loading times of top-level packages. You can find this information in Scipy Organization section in this link

wavedec does not returning any coefficients in python using pywt library

i used wavelet decomposition command in python using pywt library but it does not return any coefficients. my code is given below .
import numpy as np
import pywt as pywt
(e,f)=pywt.wavedec(y,'db12' ,level=2)
print("e:"+str(e))
print("f:"+str(f))
I also tried with pywt.dwt(y,' db12', level=2) it is also not returning any coefficients
it returns a null output, where y is a matrix contains my input
I tried reproducing your results with a random (discrete) signal like so:
import numpy as np
import pyw
x = np.random.randint(0,100,500)
y = pywt.wavedec(x, 'db12', level=2)
(e,f) = pywt.dwt(x, 'db12')
I noticed two things: For a 1D signal, wavedec returns more than two coefficient arrays, as also mentioned in the docs. Similarly, the dwt function does not know the keyword level=, but works just fine with the command specified above.
Hope that helps

Can not access ConvexHull in Scipy 0.16.1

I was able to use the ConvexHull function in previous versions of Scipy. The function, in the version I had, would not provide the Volume of the convex hull so I decided to install the new version of Scipy. But after installing Scipy 0.16.1 I noticed that the Spatial library is rearranged and now I get an error when trying to call ConvexHull function.
This code would work in previous versions of Scipy:
import numpy as np
import scipy as sp
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull
Now, for the code above, I get the error:
AttribureError: 'module' object has no attribute 'spatial'.
It seems there is nothing inside .qhull.
I have checked this in VS,Spider,Ubuntu
You have to explicitly import the spatial subpackage. This should work:
import numpy as np
import scipy as sp
import scipy.spatial
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull

Categories